【问题标题】:Issue using Microsoft.Office.Interop.Excel: Could not load file or assembly 'office, Version=15.0.0.0使用 Microsoft.Office.Interop.Excel 时出现问题:无法加载文件或程序集 'office,版本 = 15.0.0.0
【发布时间】:2020-03-13 11:05:43
【问题描述】:

我使用 .net core 3.1.1 构建了一个 Web 应用程序,并从 Nuget 下载了 Microsoft.Office.Interop.Excel。 我没有使用 ClosedXML 的原因,因为它在编译时不起作用......请参阅 link to stackO post here

Nuget包描述如下:

This the assembly necessary to do Office 2013 Excel interop
Version:  15.0.4795.1000

以下是我遇到的错误:

Could not load file or assembly 'office, Version=15.0.0.0

下面是我的代码

public static void exportToExcel1(DataTable dt, string fileName)
        {
            XL.Application app = new XL.Application();
            app.DisplayAlerts = false;

            try
            {
                XL.Workbook wb = app.Workbooks.Add(1);
                XL.Worksheet ws = (XL.Worksheet)wb.Worksheets[1];

                // export column headers
                for (int colNdx = 0; colNdx < dt.Columns.Count; colNdx++)
                {
                    ws.Cells[1, colNdx + 1] = dt.Columns[colNdx].ColumnName;
                }

                // export data
                for (int rowNdx = 0; rowNdx < dt.Rows.Count; rowNdx++)
                {
                    for (int colNdx = 0; colNdx < dt.Columns.Count; colNdx++)
                    {
                        ws.Cells[rowNdx + 2, colNdx + 1] = (dt.Rows[rowNdx][colNdx]).ToString();
                    }
                }

                //ws.Columns.AutoFit();

                wb.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, XL.XlSaveAsAccessMode.xlNoChange,
                    Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing);
                wb.Close(false, Type.Missing, Type.Missing);
            }
            finally
            {
                app.Quit();
            }
        }

【问题讨论】:

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


    【解决方案1】:

    您链接到的 NuGet 包仅提供您的代码和 Office .dll 之间的互操作性。您仍然需要在计算机上安装 MS Office 并配置您的代码,以便它可以找到库。

    您还应注意,Microsoft 不支持或不建议以这种方式使用 Office。它可能会导致各种性能问题,甚至会导致您的 Web 服务器进程挂起。

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2015-11-30
      • 2014-08-25
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      相关资源
      最近更新 更多