Excel 互操作在装有 Office 2007 的计算机上工作,但在装有 Office 2010 的计算机上失败
我有一个 C# WPF (.NET 4.0) 应用程序,它使用 Excel 互操作从 Excel 文件读取数据。
当我在装有 Excel 2007 的开发计算机上运行此应用程序时,它运行良好。当我在另一台安装了 Excel 2010 的计算机上运行它时,它失败并显示以下错误消息:
System.Runtime.InteropServices.COMException (0x80040154):检索 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败,原因如下:80040154 类未注册(来自 HRESULT 的异常: 0x80040154 (REGDB_E_CLASSNOTREG))。
尝试创建 Microsoft.Office.Interop.Excel.Application
对象时发生失败,如下所示:
var app = new Application();
我的项目引用了 Microsoft.Office.Interop.Excel.dll
>(版本14.0.0.0,运行时版本v2.0.50727),位于:
C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
我尝试使用引用的 dll 上的默认设置运行应用程序:
- Copy Local = False
- Embed互操作类型 = True
- 特定版本 = True
我还尝试在引用的 dll 上使用以下设置运行应用程序:
- 复制本地 = True
- 嵌入互操作类型 = False
- 特定版本 = True
但在装有 Office 的计算机上2010年,这些尝试都没有奏效。
其他说明:安装 Office 2010 的机器是 64 位,运行 Windows 7。我的开发机器是 X86,运行 Windows XP。
问题
是什么导致了“类未注册”异常以及如何修复它?
编辑
为了回应 Alex 的回答,我尝试将应用程序构建为任何 CPU、X86 和 X64,但没有任何区别。我仍然收到相同的“类未注册”错误。
编辑 2
刚刚在装有 Office 2010 的 32 位计算机上尝试了该应用。没有错误。因此,这可能是特定机器的问题,也可能是 64 位和 Office 2010 的组合问题。
编辑 3
好的,现在我已经在不同的 64 位上测试了它装有 Office 2010 的计算机。再次没有错误。我认为这意味着特定机器出了问题,而不是我的软件的错。啊编程。
I have a C# WPF (.NET 4.0) application that uses Excel interop to read data from an Excel file.
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): 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)).
The failure occurs when attempting to create a Microsoft.Office.Interop.Excel.Application
object, as in:
var app = new Application();
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 Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
I tried to run the app with the default settings on the referenced dll:
- Copy Local = False
- Embed Interop Types = True
- Specific Version = True
I also tried to run the app with the following settings on the referenced dll:
- Copy Local = True
- Embed Interop Types = False
- Specific Version = True
But on the machine with Office 2010, neither of these attempts worked.
Other notes: The machine with Office 2010 is 64-bit w/ Windows 7. My development machine is X86 with Windows XP.
Question
What is causing the "class not registered" exception and how do I fix it?
Edit
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.
Edit 2
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.
Edit 3
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初步答案:我的软件很好,但该特定机器上的设置有问题。
编辑
我刚刚发现了一些可能是问题根源的东西。有问题的计算机有 Office,但其设置为 点击运行(脱离 Internet 运行)而不是 基于 MSI(从 EXE 运行)。这意味着某些注册表设置、DLL、组件等对 Microsoft.Office.Interop.Excel 不可用,因此会失败。
编辑 2
这绝对是问题所在。刚刚升级到基于 MSI 的 Office 完整安装,一切运行良好。
编辑3
将此作为错误添加到Microsoft Connect:
https://connect.microsoft.com/VisualStudio/feedback/details/672276/excel-interop-fails-on -machine-where-office-is-installed-as-click-to-run#details
Preliminary answer: my software was fine and something was wrong with the setup on that particular machine.
Edit
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.Edit 2
That was definitely the problem. Just upgraded to a full MSI-based installation of Office, and everything works great.
Edit 3
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-to-run#details