I can not open the .xlsx file

I want to open the xlsx file, I tried the code below, but it does not open and does not cause any errors.

Can someone shed light on him

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); 
+4
source share
4 answers

Do you run it on 64-bit Windows? The last time I checked the OLE drivers for Excel workbooks, they did not work with 64-bit Windows.

SpreadsheetGear for.NET will allow you to read Excel workbooks from .NET and work with .NET 2.0+, including 64-bit Windows.

You can see live examples here and download a free trial here .

Disclaimer: I have SpreadsheetGear LLC

+1
source

In December 2010, Microsoft (finally!) Published a 64-bit OLEDB driver for CSV and XLSX files.

You will need the 64-bit Microsoft Access Database Engine 2010 Redistributable . Be sure to download the 64-bit version ( AccessDatabaseEngine_X64.exe ). You will need to uninstall 32-bit Office applications (including Sharepoint Designer!) In order to install it.

If you want CSV, you will need the name of the driver Microsoft Access Text Driver (*.txt, *.csv) , but I could not find the full connection string, but using this driver from OLEDB (although if you have, leave the command and I , I will change this answer). Please note that 64-bit names are different from 32-bit versions.

To read XLSX files, use the connection string as follows:

 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; 

For XLS files (pre-2007 Excel), the connection string is used as follows:

 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; 

Many thanks to this blog post and this answer for pointing me in the right direction when I came across the same problem, and for providing the content that I adapted to write this answer.

+6
source

See what this does in your connection string:

Advanced Properties = \ "Excel 8.0; HDR = YES; \" the rest of the connection string is correct. I suppose.

+1
source

In addition to Phillip's answer, be sure to install TargetPlatform for x86.

0
source

All Articles