简体   繁体   English

如何确定使用OLEDB在C#中Excel工作簿是否受密码保护?

[英]How to determine whether excel workbook is password protected or not in C# using OLEDB?

I am reading an excel file using 'OLEDB' . 我正在使用'OLEDB'阅读excel文件。 I need a provision where I can determine whether excel file that I am accessing is password protected or not. 我需要一项条款,可以确定我正在访问的excel文件是否受密码保护。 Please find my code below: 请在下面找到我的代码:

private DataSet LoadExcel(string fileName, string tableName)
{
string path = fileName;
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(connStr);
MyConnection.Open();
DataTable dtExcelSchema;
dtExcelSchema = MyConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + SheetName + "]", MyConnection);
MyCommand.TableMappings.Add("Table", tableName);
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
MyConnection.Close();
return DtSet;
}

My code is breaking up on line "MyConnection.Open();" 我的代码在“ MyConnection.Open();”行中分解 because the excel file which it is accessing here is password-protected. 因为它在这里访问的excel文件受密码保护。 If I could check that, I will bypass the code and display a user-friendly message. 如果可以检查,将跳过代码并显示一条用户友好的消息。 Please help if anyone has any idea on this front. 如果有人在这方面有任何想法,请提供帮助。

Thanks in Advance!! 提前致谢!!

From http://www.connectionstrings.com/excel/ http://www.connectionstrings.com/excel/

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. 如果Excel工作簿受密码保护,则即使为连接字符串提供正确的密码,也无法打开它进行数据访问。 If you try, you receive the following error message: "Could not decrypt file." 如果您尝试执行此操作,则会收到以下错误消息:“无法解密文件”。

OLEDB connection will not work for password protected worksheets. OLEDB连接不适用于受密码保护的工作表。 If there is some exception, you can detect the exact type and catch it. 如果有异常,您可以检测到确切的类型并捕获它。

Other way is to use the Office API and use the HasPassword property. 其他方法是使用Office API并使用HasPassword属性。

Workbook book = '.....xyz''';
if (book.HasPassword) { ... }

Have you tried sth like that? 你有没有试过那样的东西?

    try
    {
        MyConnection.Open();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

Per Aseem's first response, the "Could not decrypt file." 根据Aseem的第一个响应,“无法解密文件”。 problem is a result of the Excel file not being in a trusted location. 问题是Excel文件不在受信任位置的结果。 I had the same problem and solved it by moving the Excel file to a trusted location or adding the folder that the Excel file is in to the Trusted Locations. 我遇到了同样的问题,并通过将Excel文件移动到受信任位置或将Excel文件所在的文件夹添加到受信任位置中来解决了该问题。

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

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