tags:

views:

206

answers:

3

I want to open an xlsx file, I have tried the below code,but neither does it open nor does it thrown any error.

Can anyone throw any light upon it

string path = "C:\\examples\\file1.xlsx";
string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
OleDbConnection cn = new OleDbConnection(connString);
cn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", cn);
DataTable dt = new DataTable();
adapter.Fill(dt);
+1  A: 

See what this does in your connection string:

Extended Properties=\"Excel 8.0;HDR=YES;\" the rest of your connection string is correct I believe.

Phillip
+1  A: 

Are you running it on 64 bit Windows? Last time I checked the OLE drivers for Excel workbooks did not work with 64 bit Windows.

SpreadsheetGear for .NET will let you read Excel workbooks from .NET and works with .NET 2.0+ - including 64 bit Windows.

You can see live samples here and download the free trial here.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
Joe, you are exactly right.That was it.Because it worked in my office which has 32 bit Windows, whereas in my home i have 64 bit.Thanks Joe
cmrhema
A: 

In addition to Phillip's answer make sure to set the TargetPlatform to x86.

ZippyV