我已经使用.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();
}
}
您链接到的NuGet程序包仅提供代码与Office .dll之间的互操作性。您仍然需要在计算机上安装MS Office并配置代码,以便它可以找到该库。
您还应注意,Microsoft不支持或建议以这种方式使用Office。它可能会导致各种性能问题,甚至会导致您的Web服务器进程挂起。