2012-09-29 69 views
6

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

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

下面是我的代码片段。

[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    
} 
+0

你可以在客户机上调试吗? Furqan的答案也适用? – JMK

+1

在该机器上安装.NET 4.5。 –

+0

@HansPassant好的一点,也许OP在开发机器上安装了4.5,目标是4.0(在客户端机器上),这是一个被掩盖的bug,因为4.5代替了4.0编译器,因此掩盖了4.0中的错误? – JMK

回答

5

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

+1

这似乎很明显除了我之外的每个人,但只需降低VS中的应用程序属性页上的“目标框架”设置。 我最初认为它需要卸载4.5并在我的开发环境中安装4.0。 – aland

+0

感谢您发表该答案。我有相同的目标框架问题,直到我阅读您的文章才意识到它。 – EMR

0

尝试使用下面的代码:

[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    
} 
+0

我试过了,但没有效果。 – Mahender

+0

不错,你解决了它:) –