user1473047
user1473047

Reputation: 3

opening an excel protected sheet using c#

I am trying to open an excel file using c# however visual studio is giving this OLEDB exception: "Could not decrypt file." When opening a non prtected excel file the code works fine.

Hereunder is my code:

string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + s + ";Extended Properties='Excel 8.0;HDR=YES'";
        OleDbConnection objConn = new OleDbConnection(conn);
        objConn.Open();

        OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", objConn);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        da.Fill(dt);

I found the following website which suggests a workaround for vb but I couldn't get it to work in C#. VBA excel

Any help will be greatly appreciated.

Upvotes: 0

Views: 8047

Answers (1)

aleroot
aleroot

Reputation: 72636

You could use an OpenSource library like OoXmlCrypto to decrypt the password protected file, otherwise you can still use the Microsoft Interop official library that support password :

using Microsoft.Office.Interop.Excel

WorkbookObject.Password = password;

Otherwise the simplest way in your case, you can add the password property to the connection string of your OLeDb provider :

string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + s + ";Password=password;Extended Properties='Excel 8.0;HDR=YES'";

Upvotes: 3

Related Questions