1

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.

0

4 Answers 4

4

Changed the connection string to

public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
    + path + ";Extended Properties=Excel 12.0;";
Sign up to request clarification or add additional context in comments.

Comments

2

see Excel "External table is not in the expected format."

1 Comment

Thanks ..Got the answer from there
2

.xlsx has different connectionstrings

1 Comment

Thanks hallie...Changed the connection string and it started working.
2
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;

1 Comment

This is the same code which works well for Word 2003..Need to change the connection string to make it work for 2007

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.