[英]Excel interop works on machine with Office 2007 but fails on machine with Office 2010
I have a C# WPF (.NET 4.0) application that uses Excel interop to read data from an Excel file. 我有一个C#WPF(.NET 4.0)应用程序,它使用Excel互操作从Excel文件中读取数据。
When I run this app on my development machine, which has Excel 2007, it works fine. 当我在具有Excel 2007的开发机器上运行此应用程序时,它工作正常。 When I run it on another machine that has Excel 2010 installed on it, it fails with the following error message: 当我在另一台安装了Excel 2010的计算机上运行它时,它失败并显示以下错误消息:
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)). System.Runtime.InteropServices.COMException(0x80040154):由于以下错误,检索CLSID为{00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG) ))。
The failure occurs when attempting to create a Microsoft.Office.Interop.Excel.Application
object, as in: 尝试创建Microsoft.Office.Interop.Excel.Application
对象时发生故障,如:
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: 我的项目引用了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 用于Office \\ PIA \\ Office14 \\ Microsoft.Office.Interop.Excel.dll的C:\\ Program Files \\ Microsoft Visual Studio 10.0 \\ Visual Studio工具
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: 我还尝试在引用的dll上使用以下设置运行应用程序:
But on the machine with Office 2010, neither of these attempts worked. 但是在使用Office 2010的计算机上,这些尝试都没有奏效。
Other notes: The machine with Office 2010 is 64-bit w/ Windows 7. My development machine is X86 with Windows XP. 其他说明:Office 2010的计算机是64位w / Windows 7.我的开发计算机是带有Windows XP的X86。
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. 为了回应Alex的回答,我尝试将应用程序构建为任何CPU,X86和X64,但它没有任何区别。 I'm still getting the same "class not registered" error. 我仍然得到相同的“类未注册”错误。
Edit 2 编辑2
Just tried the app on a 32-bit machine with Office 2010. No errors. 刚刚使用Office 2010在32位计算机上试用过该应用程序。没有错误。 So, it could be something wrong with the particular machine, or it could be the combination of 64-bit and Office 2010. 因此,特定机器可能出现问题,或者可能是64位和Office 2010的组合。
Edit 3 编辑3
Okay, so now I've tested it on a different 64-bit machine with Office 2010. Again no errors. 好的,现在我已经在Office 2010的另一台64位计算机上测试了它。再次没有错误。 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. 初步答案:我的软件很好,特定机器上的设置出了问题。
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). 问题机器有Office,但它设置为点击运行 (从互联网上运行)而不是基于MSI (从EXE运行)。 This means that certain registry settings, DLLs, components, etc. are not available to Microsoft.Office.Interop.Excel
, so it fails. 这意味着某些注册表设置,DLL,组件等不可用于Microsoft.Office.Interop.Excel
,因此它失败。
Edit 2 编辑2
That was definitely the problem. 这绝对是个问题。 Just upgraded to a full MSI-based installation of Office, and everything works great. 刚升级到完整的基于MSI的Office安装,一切都很好。
Edit 3 编辑3
Added this as a bug to Microsoft Connect: 将此添加为Microsoft Connect的错误:
https://connect.microsoft.com/VisualStudio/feedback/details/672276/excel-interop-fails-on-machine-where-office-is-installed-as-click-to-run#details https://connect.microsoft.com/VisualStudio/feedback/details/672276/excel-interop-fails-on-machine-where-office-is-installed-as-click-to-run#details
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.