简体   繁体   中英

Is it necessary to keep open a excel file, while importing data to data table, C#

My connection string is:

String excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("abc.xls") + ";Extended Properties='Excel 8.0; HDR=YES;IMEX=1'";

OleDbConnection conn = new OleDbConnection(excelConn);  
Conn.Open();

It gives an error, like “ Could not decrypt file. ” If my Excel file (abc.xls) is close in my client PC. But it is ok when my excel file is open.

you cannot connect to a password protected excel file with OleDbConnection, it won't work by design.

Check here: Open a password-protected Excel workbook

If you try to open a password-protected Excel workbook from Access, you get an error message “Could not decrypt file”. Adding a Password= value to your connection string won't work either. You could open it as an Excel application, but then you'd have to learn about the Excel object model and all you want to do is read it row-by-row as you would any unprotected Excel file.

To get around this problem, first open the Excel workbook using GetObject() and then open it using the connection / recordset approach that you want to use. The GetObject() open prompts your user for the password, and then you are free to open the file using your preferred approach.

I guess your file is password protected. Please confirm the same. The password protected file can not be opened even by supplying password in connection string. Check the link http://www.connectionstrings.com/excel

The workaround is at connection strings website

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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