vb.net 两台不同计算机上的不同互操作引用不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/224181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different Interop references on two different computers doesn't work
提问by Laurent
When I add a reference to Microsoft.Office.Interop.Excelon my computer, Visual Studio adds this to the project file:
当我在计算机上添加对Microsoft.Office.Interop.Excel的引用时,Visual Studio 会将其添加到项目文件中:
<COMReference Include="Excel">
<Guid>{00020813-0000-0000-C000-000000000046}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>5</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
</COMReference>
There is another developer on the team who gets errors and needs to add a DLL file to the project called Interop.Excel.dll, which replaces the code above with this in the project file:
团队中还有另一个开发人员遇到错误,需要向项目中添加一个名为 Interop.Excel.dll 的 DLL 文件,它将项目文件中的代码替换为上面的代码:
<Reference Include="Interop.Excel, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>My Project\Interop.Excel.dll</HintPath>
</Reference>
This does work on my computer.
这在我的电脑上确实有效。
Could you please explain the differences between the two methods, which is best, and how to get the first one working on other computers?
您能否解释一下这两种方法之间的区别,哪种方法最好,以及如何让第一种方法在其他计算机上工作?
回答by Dirk Vollmar
I don't see a problem with your approach either.
我也不认为你的方法有问题。
Typically VS will generate an interop assembly for COM components automatically when you add a reference to the component. However, when you add a reference to one of the Office components (XP or any later version), a reference to the pregenerated (and optimized) primary interop assembly from Microsoft is added as in your first example. The line
通常,当您添加对组件的引用时,VS 会自动为 COM 组件生成互操作程序集。但是,当您添加对某个 Office 组件(XP 或任何更高版本)的引用时,会添加对 Microsoft 预先生成(和优化)的主互操作程序集的引用,如第一个示例所示。线
<WrapperTool>primary</WrapperTool>
means that this PIA is used.
表示使用此 PIA。
If you correctly added the PIA reference the CopyLocalproperty of this reference should be set to false and the Pathproperty should be something like
如果您正确添加了 PIA 引用,则此引用的CopyLocal属性应设置为 false,并且Path属性应类似于
C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
You will find some more details on interop assemblies in this MSDN article.
您将在这篇 MSDN文章中找到有关互操作程序集的更多详细信息。
To get the first method working it is necessary that the Office Primary Interop Assemblies (PIAs) are installed on the machine. There is a redistributable version available from Microsoft:
要使第一种方法起作用,必须在计算机上安装 Office 主互操作程序集 (PIA)。Microsoft 提供了一个可再发行版本:
AFAIK, these PIAs only get installed by the Office setup when the .NET Framework has already been installed, that's why there is a separate redistributable for the PIAs.
AFAIK,这些 PIA 只有在 .NET Framework 已经安装后才能由 Office 安装程序安装,这就是为什么有一个单独的 PIA 可再发行组件。
Note: Make sure that you reference the version of Office that you are targeting. When targeting several versions of Office, you might get some problems however. A solution in that case might be late binding (if performance is not an issue).
注意:请确保您引用了您所针对的 Office 版本。但是,当针对多个版本的 Office 时,您可能会遇到一些问题。这种情况下的解决方案可能是后期绑定(如果性能不是问题)。
回答by Charles Graham
I've used Excel automation way more than I would like to admitt, and I have never referenced Interop.Excel.dll. I've always referenced the former. Why is he referencing that, and what errors does he get?
我使用 Excel 自动化的方式比我想承认的要多,而且我从未引用过 Interop.Excel.dll。我一直参考前者。他为什么引用那个,他得到了什么错误?
Are you guys referencing the same version of excel (5.0 verses 11.0)? Do you guys have the exact same version of office, service pakcs and all? This could be the differance.
你们引用的是相同版本的 excel(5.0 和 11.0)吗?你们有完全相同版本的 office、service pakcs 吗?这可能就是区别。
回答by Charles Graham
I found the cleanest way to use it, this also allows for multiple versions of the interop, is to create a shared bin\Office Interop\11 or 12\Microsoft.Office.Interop.Excel.dll\ and refeferenced them from the project, works a treat for different version
我找到了最简洁的使用方法,这也允许多个版本的互操作,是创建一个共享 bin\Office Interop\11 或 12\Microsoft.Office.Interop.Excel.dll\ 并从项目中引用它们,适用于不同版本