Search code examples
c#exceloledb

I cannot open .xlsx file


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

Solution

  • 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