2017-09-06 108 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.

相关问题