简体   繁体   English

C#在数据表中打开Excel xlsx文件时遇到麻烦

[英]c# trouble opening excel xlsx file in datatable

I can easily open xls file with this code: 我可以使用以下代码轻松打开xls文件:

System.Data.OleDb.OleDbConnection theConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=C:\\Users\\us269229\\Desktop\\workbook.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
theConnection.Open();

however, I cannot open xlsx files. 但是,我无法打开xlsx文件。 With the above code I get the following error: 使用上面的代码,我得到以下错误:

External table is not in the expected format. 外部表格的格式不正确。

so I tried the following if the extension is xlsx: 因此,如果扩展名为xlsx,我尝试了以下操作:

System.Data.OleDb.OleDbConnection theConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\us269229\\Desktop\\workbook.xlsx;" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES\"");

theConnection.Open(); theConnection.Open();

however, with this I get this error: 但是,与此我得到这个错误:

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. “ Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册。

I did download and install AccessDatabaseEngine_X64.exe, but this did not clear my error. 我确实下载并安装了AccessDatabaseEngine_X64.exe,但是并不能清除我的错误。

Any help would be appreciated - thanks in advance 任何帮助将不胜感激-预先感谢

I think you need to Install "The 2007 Office System Driver: Data Connectivity Components.". 我认为您需要安装“ 2007 Office System驱动程序:数据连接组件”。

Source: 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine 来源: “ Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM