Search code examples
c#.netexceloledb

Unable to Open an Excel file using System.Data.OleDb.OleDbConnection


Iam trying to open an XLSX file (so that I can create a datatable ) Iam using the the below code.

System.Data.OleDb.OleDbConnection oleDbCon;
System.Data.OleDb.OleDbDataAdapter oleDbDataAd;
oleDbCon = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filePath + "';Extended Properties=Excel 8.0;");

but when file path contains a file with extension xlsx , i get an error "External table is not in the expected format."

Above code works fine when file is of extention xls.

Do I have to change the connection string ?

Any help ?Thanks in Advance.


Solution

  • Changed the connection string to

    public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
        + path + ";Extended Properties=Excel 12.0;";