2015-04-23 38 views
1

在我的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 –

回答

1

問題:您已經開發在本地計算機您的應用程序的.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已經安裝在系統中。 –