question

mamtachoudhary-6642 avatar image
0 Votes"
mamtachoudhary-6642 asked David-9140 Suspended edited

Can we protect excel workbook with Password using open XML SDK?

I want to password protect the excel using C#. One approach is using interop libraries, but issue with this is we need to install MS office on our Server which is not allowed in organization. What is the other approach to password protect excel using C#?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

Hi mamtachoudhary-6642,
You could not protect an Excel file with openxml.
Using password protection doesn't not generate an Office Open XML file and you will get a file in binary file format.
The Open XML SDK does not work with the binary file formats.
One suggestion is that you use Workbook.Password property to set the password:
Here are some similar threads you can refer to.
How to protect document with password using C# and openxml
Protecting Excel file created through OpenXML
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Overview of Protected Office Open XML Documents

http://blogs.msdn.com/b/openspecification/archive/2009/07/17/overview-of-protected-office-open-xml-documents.aspx

The above link is not working. Could you please give a correct link?

0 Votes 0 ·

Also , if I go for workbook.password property then I need to install MS office on the server which we don't want.

0 Votes 0 ·

Hi @mamtachoudhary-6642,
It’s a blog link, I can’t access it now.
And about protecting excel workbook with Password, here is a detailed summary you can refer to.
Best Regards,
Daniel Zhang


0 Votes 0 ·
David-9140 avatar image
0 Votes"
David-9140 Suspended answered David-9140 Suspended edited

Alternatively, you may use Spire.XLS for .NET for encryption or decryption of your Excel workbooks. It is a tiny .NET Excel library and does not require MS Excel to be installed on server or end users' machines. Below is the code example for your reference.

 using Spire.Xls;
    
 namespace ProtectExcel
 {
     class Program
     {
         static void Main(string[] args)
         {
             //Load Workbook
             Workbook book = new Workbook();
             book.LoadFromFile(@"E:\Documents\VendorInfo.xlsx");
             //Protect Workbook
             book.Protect("abc-123");
             //Save to file
             book.SaveToFile("ProtectExcel.xlsx", ExcelVersion.Version2013);
         }
     }
 }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.