在C#中使用OLEDB读取受密码保护的Excel文件
在我的 C# 应用程序中,我使用 OLEDB 连接字符串 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended Properties=\"Excel 8.0;HDR=NO;ReadOnly=true; IMEX=1\"
" 读取 Excel 文件。 为了读取受密码保护的文件,我尝试在连接字符串中添加密码字段,但无法读取文件。 我想知道如果我事先知道密码,是否有任何方法可以使用 OLEDB 读取受密码保护的 Excel 文件。
In my c# application I am using OLEDB connection string "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended Properties=\"Excel 8.0;HDR=NO;ReadOnly=true;IMEX=1\"
" to read Excel files.
In order to read a password protected file I tried adding password field in connection string but was unable to read file.
I want to know is there any way to read password protected Excel files using OLEDB if I know its password beforehand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用查询来读取 Excel 文件,则某些工作表是否受到保护并不重要:无论哪种方式都可以。
If you use a query to read the excel file, it doesn't matter if some of the sheets are protected: It works either way.
以下是连接到 Excel 文件的不同方法,包括 OLEDB。据此,您无法使用标准方法打开受密码保护的文件。您必须使用解决方法。
这就是解决方案,尽管不是在 C# 中,但您可以轻松地根据您的目的进行调整。
如果您自己不知道密码,另一种方法是在没有密码的情况下重写文件。您可以使用这个方便的项目并向其中添加以下例程:
完整详细信息请参见此处。
Here are different ways to connect to an Excel file, including OLEDB. According to this, you can't open a password protected file with standard methods. You have to use a workaround.
This is the solution, albeit not in C#, but you could easily adapt it for your purposes.
If you don't KNOW the password yourself, an alternative is to re-write the file without a password. You can use this handy project and add the following routine to it:
Full detail here.
经过我一次又一次的研究,终于我发现了两件事。
1.使用OLEDB,无法读取受密码保护的excel文件。
2.尽管Interop可以读取excel文件,无论是否受密码保护,但其性能不如OLEDB。
因此,我通过组合创建以下代码
1. OLEDB具有非常好的性能
2. Interop,可以读取每个Excel文件。
最后,如果您想在从Excel中检索数据时删除空行,请检查这个链接和下面的代码
After I researched again and again, finally I found 2 things.
1.Using OLEDB , It cannot read excel file which is password protected.
2.Even though Interop can read excel file no matter whether password protected or not, its performance is not as good as OLEDB.
So, I create below code by combining
1. OLEDB which has very nice performance and
2. Interop which can read every excel files.
Finally If you want to remove empty row while retrieving data from excel, please check this link and below code
您可以使用 OoXmlCrypto 流访问 Office 2007 加密文件。开源,包括修改后的 ExcelPackage。
示例代码:
You can use OoXmlCrypto stream to access Office 2007 encrypted files. Open source, includes modified ExcelPackage.
Sample code: