Unable to Open an Excel file using System.Data.OleDb.OleDbConnection [duplicate]
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.
Changed the connection string to
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ path + ";Extended Properties=Excel 12.0;";
see Excel "External table is not in the expected format."
.xlsx has different connectionstrings
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="
+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel="select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds,"table1");
return ds;
精彩评论