【问题标题】:Excel interop MissingMethodExceptionExcel 互操作 MissingMethodException
【发布时间】:2012-09-29 18:17:15
【问题描述】:

在我的 C# 程序中,我使用 Excel 2010 互操作程序集。有了这个,我正在读取和写入数据到 excel 文件。并在开发盒(包含 Office 2010)上执行良好。在客户端计算机上,即使他们有 Office 2010 和 Office PIA,也会看到以下异常,在 WriteToExcel() 方法调用时引发。

Unhandled Exception: System.MissingMethodException: Method not found: 'System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.GUID)'.

下面是我的代码 sn-p。

[STAThread]
static void Main(string[] args){

       // read user input, process and write data to Excel
       WriteToExcel();
 }

[STAThread]
static void WriteToExcel(){
     Application xlsApplication = new Application();
     Workbook xlsWorkbook = xlsApplication.Workbooks.Open(excelFilePath);
     // write data to excel
     // close up            
 }

【问题讨论】:

  • 可以在客户端机器上调试吗? Furqan 的回答也有效吗?
  • 在那台机器上安装 .NET 4.5。
  • @HansPassant 好点,也许 OP 在开发机器上安装了 4.5,并且目标是 4.0(在客户端机器上),这是一个被掩盖的错误,因为 4.5 替换了 4.0 编译器,并且因此掩盖了 4.0 中的错误?
  • 感谢@JMK 的提示。将 .net 版本降低到 4.0 后问题已解决。

标签: c# excel exception interop


【解决方案1】:

将 .net 版本降低到 4.0 后问题已解决。早些时候我的 devbox 有 4.5,应用程序是用这个版本编译的。我的客户端机器有4.0版本,降低.net版本解决了这个问题。

【讨论】:

  • 这对除了我之外的所有人来说似乎都很明显,但只需降低 VS 中应用程序属性页面上的“目标框架”设置即可。我最初认为需要在我的开发环境中卸载 4.5 并安装 4.0。
  • 感谢您发布此答案。我遇到了同样的目标框架问题,直到我读了你的帖子才意识到。
【解决方案2】:

尝试使用以下代码:

[STAThread]
static void WriteToExcel()
{
    Application xlsApplication = new Application();
    var missing = System.Type.Missing;
    Workbook xlsWorkbook = xlsApplication.Workbooks.Open(excelFilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
    // write data to excel
    // close up            
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多