在C#中使用OLEDB读取密码保护的Excel文件 [英] Read password protected excel file using OLEDB in C#

查看:1327
本文介绍了在C#中使用OLEDB读取密码保护的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#应用​​程序,我使用OLEDB连接字符串连接Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\test.xls;扩展属性= \的Excel 8.0; HDR = NO;只读= 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.

推荐答案

下面是不同的方式连接到一个Excel文件,包括OLEDB。根据这一点,你不能打开与标准方法密码保护的文件。你必须使用一种解决方法。

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.

如果Excel工作簿是由
A密码保护,您无法打开它
数据访问,甚至用您的连接
串供应
正确的密码。如果你尝试,你收到
以下错误信息:无法
解密文件

If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: "Could not decrypt file.

<一个。 HREF =htt​​p://www.connectionstrings.com/Articles/Show/how-to-open-password-protected-excel-workbook>这是解决,尽管不是在C#中,但你可以很容易地它适应你的目的。

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:

public void SaveFile()

        {
            this.excelWorkbook.SaveAs(
                this.excelWorkbook.FullName,
                vk_format,
                "",
                vk_write_res_password,
                vk_read_only,
                null,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                null,
                vk_add_to_mru,
                null,null,vk_local);
        }

此处全部细节。

这篇关于在C#中使用OLEDB读取密码保护的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆