简体   繁体   English

如何使用 ClosedXML 打开受保护的 Excel 文件?

[英]How to open a protected Excel File with ClosedXML?

Is it possible to use ClosedXML to open an Excel file protected with a password?是否可以使用 ClosedXML 打开受密码保护的 Excel 文件? And if so, how?如果是这样,如何?

I tried using Interop but it's very slow.我尝试使用 Interop,但速度很慢。

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我也尝试过使用 ClosedXML 没有成功

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. “文件包含损坏的数据”创建 XLWorkbook 对象。

Password protection to enable/disable the opening of a file is not supported with ClosedXML/OpenXML. ClosedXML/OpenXML 不支持启用/禁用打开文件的密码保护。 See https://blogs.msdn.microsoft.com/openspecification/2009/07/17/overview-of-protected-office-open-xml-documents/请参阅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.您只能锁定 OpenXML 文档的结构和窗口,而这正是wb.Protect()方法所做的。

That said, ClosedXML supports only the legacy protection methods.也就是说,ClosedXML 仅支持遗留保护方法。 Newer protection methods and password hashing algorithms, introduced in Excel 2013, are not supported yet.尚不支持 Excel 2013 中引入的较新的保护方法和密码哈希算法。 Refer to https://github.com/ClosedXML/ClosedXML/issues/866参考https://github.com/ClosedXML/ClosedXML/issues/866

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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