【问题标题】:Could not load file or assembly 'Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' [duplicate]无法加载文件或程序集“Microsoft.Office.Interop.Excel,版本=14.0.0.0,文化=中性,PublicKeyToken=71e9bce111e9429c”[重复]
【发布时间】:2017-09-06 09:07:18
【问题描述】:

这不是重复阅读完整问题

我有控制台应用程序。

这里我想将数据集值导出到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;
    }

当我运行我的代码时出现错误

无法加载文件或程序集 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' 或其依赖项之一。系统找不到指定的文件。

我的电脑上没有安装 ms office。

我以为我没有在我的电脑上安装 MSOffice,所以只有我得到了错误。

如果这不是我在代码中出错的错误,谁能告诉我

【问题讨论】:

    标签: c# asp.net dll office-interop excel-interop


    【解决方案1】:

    Excel interop 正在您的计算机上创建一个 excel 实例 - 所以您需要安装它才能使用它。此外,您必须安装正确版本的 Excel。要支持比 X 更新的 Excel 版本,请对 Excel 互操作版本 X 使用后期绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 2021-01-22
      • 2011-07-07
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多