开发者

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

This question already has answers here: Excel "External table is not in the expected format."开发者_JAVA百科; (25 answers) Closed 8 years ago.

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜