Excel互操作MissingMethodException

在我的C#程序中,我正在使用Excel 2010 interop程序集。 有了这个我正在读写数据,以Excel文件。 并在开发框(包含Office 2010)上执行得很好。 在客户端计算机上,即使他们具有Office 2010和Office PIA,也会看到下面的exception情况,并引发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 } 

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

尝试使用以下代码:

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