简体   繁体   中英

How to open a protected Excel File with ClosedXML?

Is it possible to use ClosedXML to open an Excel file protected with a password? And if so, how?

I tried using Interop but it's very slow.

Using Interop.Excel
Microsoft.Office.Interop.Excel.Application wb = xlApp.Workbooks.Open(Filename: "c:\TEST\MyFileProtected.xlsx", ReadOnly: true, Password: "PASSWORD");

I also tried with ClosedXML with no success

string path = openFileDialog1.FileName.ToString();

var wb = new XLWorkbook(path);
wb.Unprotect("password");

I'm getting the error

"File contains corrupted data" creating the XLWorkbook object.

Password protection to enable/disable the opening of a file is not supported with ClosedXML/OpenXML. See https://blogs.msdn.microsoft.com/openspecification/2009/07/17/overview-of-protected-office-open-xml-documents/

You can only lock the structure and windows of an OpenXML document and that is indeed what the wb.Protect() method does.

That said, ClosedXML supports only the legacy protection methods. Newer protection methods and password hashing algorithms, introduced in Excel 2013, are not supported yet. Refer to https://github.com/ClosedXML/ClosedXML/issues/866

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM