Problem opening Excel 2007 file from OleDbConnection

D

Dave Jenkins

I am attempting to open an Excel 2007 file (test1.xlsx) from my Word 2007
addin using OleDb. The code runs fine as long as I have the Excel 2007
application running with "test1.xlsx" open. But if Excel 2007 is not
running, I get an exception "External table is not in the expected format" on
the myConn.Open() statement. If I replace the Excel 2007 file with the
equivalent Excel 2003 binary file it always works.

Do I need a new JET engine to open Excel 2007 files? Why does the code work
when Excel 2007 has the file open? Does the old JET engine notice that Excel
2007 has the file open and use its code? Thanks for any help.

Here is the code:

string filename = @"c:\test1.xlsx";
if (!File.Exists(filename))
throw new Exception("File not found");

string myConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + filename + ";" + "Extended Properties=\"Excel 8.0;HDR=YES\"";
OleDbConnection myConn = new OleDbConnection(myConnStr);
myConn.Open(); // HERE IS WHERE IT FAILS
myConn.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top