如何从 C# 连接到不同版本的 Excel?
我有一个程序可以将 Excel 中的数据导入到数据集中。要连接到 Excel,我使用以下代码...
return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
fileName + "; Jet OLEDB:Engine Type=5;"+ "Extended Properties=\"Excel 8.0;\"");
我刚买了一台装有 Excel 2010 的新计算机,现在连接尝试失败并抛出异常,提示 “Microsoft.Jet.OLEDB.4.0”提供程序未注册本地机器
这是因为新版本的Excel需要不同的连接字符串吗?有人遇到过这个吗?
编辑: 实际上,我只是在某处读到,使用 System.Data.OleDbClient 类甚至不需要在计算机上安装 Excel。所以我的问题可能与 Excel 无关,而是与 Microsoft.Jet 工具有关。我的电脑运行的是Windows7 64位。不是应该已经安装了吗?
I have a program that imports data from Excel into a dataset. To connect to Excel I use the following code...
return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
fileName + "; Jet OLEDB:Engine Type=5;"+ "Extended Properties=\"Excel 8.0;\"");
I just got a new computer with Excel 2010 and now the connection attempt fails and throws an exception saying The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine
Is this because the new version of Excel needs a different connection string? Has anyone run into this?
EDIT:
Actually I just read somewhere that using the System.Data.OleDbClient class does not require Excel to even be installed on the computer. So my problem probably has nothing to do with Excel but rather with the Microsoft.Jet tool. My computer is running Windows7 64 bit. Shouldn't that be installed already?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 Microsoft Access 数据库Engine 2010 可再发行。
还存在其他 Excel 版本的可再发行组件。
Check out the Microsoft Access Database Engine 2010 Redistributable.
Redistributables for other Excel versions also exist.
您需要专门为 32 位编译您的程序,以便它将在 WoW 子系统中运行并调用 32 位 Jet 提供程序。如上所述,Jet 提供程序没有 64 位版本,而且可能永远不会有。
在 Visual Studio 中,您可以通过在项目设置中将目标 CPU 类型设置为 x86 而不是 Any 或 64 位来完成此操作。
如果您没有程序的源代码,则可以使用 .NET 框架附带的 corflags.exe 实用程序和 /32bit+ 标志来修改 .exe,但如果它是强签名程序集,则需要 .修改后请 SNK 重新签名。
如果您需要更多详细信息,这里是 WoW64 的参考: http://en.wikipedia.org/wiki/魔兽64
You need to compile your program specifically for 32-bit so it will run in the WoW subsystem and invoke the 32-bit Jet provider. As mentioned above, there is no 64-bit version of the Jet provider and likely never will be.
In Visual Studio, you do this by setting your target CPU type in the project settings to x86 instead of Any or 64-bit.
If you don't have the source for your program, you can modify the .exe using the corflags.exe utility that comes with the .NET framework and the /32bit+ flag, but if it's a strongly signed assembly you'll need the .SNK to re-sign it once you've modified it.
Here is a reference on WoW64 if you need more detail: http://en.wikipedia.org/wiki/WoW64
我发现这个网站在遇到连接字符串问题时很有用。
http://www.connectionstrings.com/excel
另请看一下之前的 SO 问题:
< a href="https://stackoverflow.com/questions/1461053/diagnosing-an-oledb-exception-when-quering-excel-2010">查询 Excel 2010 时诊断 OLEDB 异常
I find this site useful when having connection string questions.
http://www.connectionstrings.com/excel
Also take a look at a previous SO question:
Diagnosing an OLEDB exception when Quering Excel 2010