Excel interop MissingMethodException

时间:2012-09-29 18:17:16

标签: c# excel exception interop

在我的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)'.

以下是我的代码段。

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分: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            
 }
相关问题