我有一个 C# WPF (.NET 4.0) 应用程序,它使用 Excel 互操作从 Excel 文件中读取数据.
I have a C# WPF (.NET 4.0) application that uses Excel interop to read data from an Excel file.
当我在装有 Excel 2007 的开发机器上运行此应用程序时,它运行良好.当我在另一台安装了 Excel 2010 的机器上运行它时,它会失败并显示以下错误消息:
When I run this app on my development machine, which has Excel 2007, it works fine. When I run it on another machine that has Excel 2010 installed on it, it fails with the following error message:
System.Runtime.InteropServices.COMException (0x80040154):检索具有 CLSID {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败,原因是以下错误:80040154 未注册类(来自 HRESULT 的异常:0x80040154 (REGDB_E_CLASSNOTREG)).
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)).
尝试创建 Microsoft.Office.Interop.Excel.Application
对象时失败,如:
The failure occurs when attempting to create a Microsoft.Office.Interop.Excel.Application
object, as in:
var app = new Application();
我的项目引用了Microsoft.Office.Interop.Excel.dll
(版本14.0.0.0,运行时版本v2.0.50727),位于:
My project has a reference to Microsoft.Office.Interop.Excel.dll
(version 14.0.0.0, runtime version v2.0.50727), which is located at:
C:Program FilesMicrosoft Visual Studio 10.0Visual Studio Tools for OfficePIAOffice14Microsoft.Office.Interop.Excel.dll
C:Program FilesMicrosoft Visual Studio 10.0Visual Studio Tools for OfficePIAOffice14Microsoft.Office.Interop.Excel.dll
我尝试在引用的 dll 上使用默认设置运行应用程序:
I tried to run the app with the default settings on the referenced dll:
我还尝试在引用的 dll 上使用以下设置运行应用程序:
I also tried to run the app with the following settings on the referenced dll:
但在装有 Office 2010 的机器上,这些尝试都没有奏效.
But on the machine with Office 2010, neither of these attempts worked.
其他说明:装有 Office 2010 的机器是 64 位 w/Windows 7.我的开发机器是装有 Windows XP 的 X86.
Other notes: The machine with Office 2010 is 64-bit w/ Windows 7. My development machine is X86 with Windows XP.
问题
是什么导致了类未注册"异常,我该如何解决?
What is causing the "class not registered" exception and how do I fix it?
编辑
针对 Alex 的回答,我尝试将应用程序构建为 Any CPU、X86 和 X64,但没有任何区别.我仍然收到相同的类未注册"错误.
In response to Alex's answer, I tried building the app as Any CPU, X86, and X64, but it made no difference. I'm still getting the same "class not registered" error.
编辑 2
刚刚在装有 Office 2010 的 32 位计算机上试用了该应用程序.没有错误.因此,可能是特定机器出了问题,或者可能是 64 位和 Office 2010 的组合.
Just tried the app on a 32-bit machine with Office 2010. No errors. So, it could be something wrong with the particular machine, or it could be the combination of 64-bit and Office 2010.
编辑 3
好的,现在我已经在另一台装有 Office 2010 的 64 位计算机上对其进行了测试.再次没有错误.我认为这意味着特定机器有问题,这不是我的软件的错.啊编程.
Okay, so now I've tested it on a different 64-bit machine with Office 2010. Again no errors. I think this means something is wrong with the particular machine and it's not my software's fault. Ahh programming.
初步回答:我的软件很好,但特定机器上的设置有问题.
Preliminary answer: my software was fine and something was wrong with the setup on that particular machine.
编辑
我刚刚发现了一些可能是问题的根源.问题机器有Office,但设置为click-to-run(在互联网上运行)而不是 基于 MSI(从 EXE 运行).这意味着某些注册表设置、DLL、组件等对 Microsoft.Office.Interop.Excel
不可用,因此会失败.
I just found out something that is probably the source of the problem. The problem machine has Office, but it is set up as click-to-run (runs off the internet) rather than MSI-based (runs from an EXE). This means that certain registry settings, DLLs, components, etc. are not available to Microsoft.Office.Interop.Excel
, so it fails.
编辑 2
这绝对是问题所在.刚刚升级到基于 MSI 的完整 Office 安装,一切正常.
That was definitely the problem. Just upgraded to a full MSI-based installation of Office, and everything works great.
编辑 3
将此作为错误添加到 Microsoft Connect:
Added this as a bug to Microsoft Connect:
https://connect.microsoft.com/VisualStudio/feedback/details/672276/excel-interop-fails-on-machine-where-office-is-installed-as-click-运行#details
这篇关于Excel 互操作在装有 Office 2007 的机器上工作,但在装有 Office 2010 的机器上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!