Excel interop MissingMethodException在某些系统上

问题描述:

在我的C#程序中,我使用的是Microsoft.Office.Interop.Excel。有了这个,我正在读取&将数据写入excel文件。在一台机器上,即使它具有Office 2007,但在下面的例外情况下,会在GetComponentPath()方法调用时引发。Excel interop MissingMethodException在某些系统上

未处理的异常:System.MissingMethodException:未找到方法:'System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.GUID)'。

这里是我的代码:

public static string GetComponentPath(OfficeComponent _component) 
{ 
    string toReturn = string.Empty; 
    string _key = string.Empty; 
    try 
    { 
     Microsoft.Office.Interop.Excel.Application _excelApp = null; 
     _excelApp = new Microsoft.Office.Interop.Excel.Application(); 
     if (_excelApp != null) 
     { 
      Console.WriteLine("Excel is installed"); 
     } 
     else 
     { 
      Console.WriteLine("Excel not found."); 
     }    
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("Error \n" + ex.ToString()); 
    } 
    return toReturn; 
} 

public enum OfficeComponent 
{ 
    Word, 
    Excel, 
    PowerPoint, 
    Outlook 
} 
+0

您正在使用哪种框架? – sheshadri

+0

我正在使用.net框架4.0 @ user3588674 –

问题:您已经开发在本地计算机您的应用程序的.NET Framework heigher版本,并在具有.NET Framework低版本的远程PC相同。

注意:如果您的应用程序运行在Heigher版本的.NET Framework它不会在较低版本上运行。

解决方案:您需要将其定位到.NET Framework远程PC上的远程PC上可以运行的较低版本。

第1步:右击project - 选择properties

步骤2:.NET Framework x.xTarget Framework改为.NET Framework x.y

注意:其中x.x更高,x.y是远程PC或更低版本上可用的较低版本。

+0

我开发了我的应用程序在.net framework 4.0中,并且有必要让其他机器也应该安装4.0,否则应用程序将无法运行。但在其他系统应用程序正在运行,但excel部分不起作用。 –

+0

您是否安装了ms office ... – sheshadri

+0

办公室2007已经安装在系统中。 –