I cannot open .xlsx file

蹲街弑〆低调 提交于 2019-12-04 10:29:26

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

Justin Grant

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

You'll need the 64-bit Microsoft Access Database Engine 2010 Redistributable. Make sure to download the 64-bit version (AccessDatabaseEngine_X64.exe). You'll need to uninstall any 32-bit Office apps (including Sharepoint Designer!) in order to install it.

If you want CSV, you'll want the Microsoft Access Text Driver (*.txt, *.csv) driver name, but I haven't been able to find a full connection string yet using this driver from OLEDB (although if you have, leave a commend and I'll amend this answer). Note that 64-bit names are different from the 32-bit versions.

For reading XLSX files, use a connection string like this:

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

For XLS (pre-2007 Excel) files use a connection string like this:

"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 ran into the same problem, and for providing content I adapted to write this answer.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!