2017-09-06 79 views
-3

這不是重複閱讀完整的問題無法加載文件或程序集「的Microsoft.Office.Interop.Excel,版本= 14.0.0.0,文化=中性公鑰= 71e9bce111e9429c」

我有控制檯應用程序。

這裏我要出口數據設定值到Excel

的,我將使用這個 我已經使用microsoft.office.interop.excel.dll

,並使用該命名空間

使用Excel = Microsoft.Office.Interop.Excel;

這是我的Excel導出代碼

private static bool ExportDataTableToExcel(DataTable dt, string filepath) 
    { 

     Excel.Application oXL; 
     Excel.Workbook oWB; 
     Excel.Worksheet oSheet; 
     Excel.Range oRange; 

     try 
     {  
      oXL = new Excel.Application(); 
      oXL.Visible = true; 
      oXL.DisplayAlerts = false; 

      oWB = oXL.Workbooks.Add(Missing.Value); 

      oSheet = (Excel.Worksheet)oWB.ActiveSheet; 
      oSheet.Name = "Data"; 

      int rowCount = 1; 
      foreach (DataRow dr in dt.Rows) 
      { 
       rowCount += 1; 
       for (int i = 1; i < dt.Columns.Count + 1; i++) 
       { 
        // Add the header the first time through 
        if (rowCount == 2) 
        { 
         oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName; 
        } 
        oSheet.Cells[rowCount, i] = dr[i - 1].ToString(); 
       } 
      } 


      oRange = oSheet.get_Range(oSheet.Cells[1, 1], 
          oSheet.Cells[rowCount, dt.Columns.Count]); 
      oRange.EntireColumn.AutoFit(); 

      oSheet = null; 
      oRange = null; 
      oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal, 
       Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
       Excel.XlSaveAsAccessMode.xlExclusive, 
       Missing.Value, Missing.Value, Missing.Value, 
       Missing.Value, Missing.Value); 
      oWB.Close(Missing.Value, Missing.Value, Missing.Value); 
      oWB = null; 
      oXL.Quit(); 
     } 
     catch 
     { 
      throw; 
     } 
     finally 
     { 

      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
     } 

     return true; 
    } 

,當我運行我的代碼我收到錯誤

無法加載文件或程序集「辦公室,版本= 14.0.0.0,文化=中性公鑰= 71e9bce111e9429c'或其依賴項之一。該系統找不到指定的文件。

我沒有在我的電腦中安裝ms office。

我以爲我沒有在我的電腦上安裝MSOffice,所以只有我得到錯誤。

如果不這樣的錯誤,我在我的代碼所做的錯誤誰能告訴我

回答

1

Excel的互操作是你的機器上創建一個Excel實例 - 所以你需要把它安裝到能夠使用它。此外,您必須安裝正確版本的Excel。爲了支持比X更新的Excel版本,使用後期綁定來對抗excel互操作版本X.

相關問題