c#excel dll – 无法添加对指定文件的引用 – regasm

在另一台计算机上部署和注册.Net Excel.dll时,出现错误尝试在VBA编辑器中添加对DLL Can't add a reference to the specified file引用。

我在Visual Studio中使用C#创建了Excel.dll ,在我的机器上使用Windows 7Office 2010运行正常。 在我的计算机上的Excel VBA编辑器中添加对dll的引用没问题。 我的问题是在另一台运行VistaExcel 2007机器上部署。 我将dll复制到此计算机并使用regasm注册dll。

谁能指出我正确的方向? 这是代码和regasm:

C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ regasm.exe excelDll.dll

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace TestDll { [ClassInterface(ClassInterfaceType.AutoDual)] public class Test { public string HelloWorld { get { return "Hello World"; } } public void sayGoodbye1() { MessageBox.Show("Say Goodbye"); } public string sayGoodbye2() { return "Say Goodbye"; } } } 

您需要注册excel的类型库才能在References中查看您的dll。

regasm.exe excelDll.dll /tlb:excelDll.tlb

标记。

我最近遇到并设法解决了这个问题 – 虽然我不能声称理解为什么我的解决方案有效。

我的两个系统都运行Windows 7 x64。 一个是Excel 2010,另一个是Excel 2007。

所有C#程序集都设置为“Platform Target:Any CPU”。 主程序集设置为“Register for COM Interop”。 整个事情是使用Visual Studio Installer项目创建的MSI安装的。

我发现如果我将Visual Studio Installer项目“Target Platform”设置为“x64”,那么它可以在Excel 2010中运行,但不能在Excel 2007中运行。相反,如果我将Visual Studio Installer项目“Target Platform”设置为“x86” “,它适用于Excel 2007,但不适用于Excel 2010。

遗憾的是,我不能同时在同一台机器上测试两个Excel版本 – 但至少这可能会让它适合你!