Excel interop MissingMethodException
问题描述:
在我的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
}
答
尝试使用下面的代码:
[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
不错,你解决了它:) –
你可以在客户机上调试吗? Furqan的答案也适用? – JMK
在该机器上安装.NET 4.5。 –
@HansPassant好的一点,也许OP在开发机器上安装了4.5,目标是4.0(在客户端机器上),这是一个被掩盖的bug,因为4.5代替了4.0编译器,因此掩盖了4.0中的错误? – JMK