在生产中,我尝试使用 ClosedXML nuget 包将数据库表导出到 Excel 中,但是我在标题中收到此错误,我在本地主机上尝试了它,但它工作得很好。 我检查了生产中的 dll 文件,并且存在 SixLabors.dll。这是我的代码 我正在使用 Asp .Net Core 6
public IActionResult ExportProductsToExcel()
{
const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
string fileName = "Products " + DateTime.UtcNow.ToString().Replace("AM", "").Replace("PM", "") + ".xlsx";
try
{
using var stream = GetProductExcelStream();
var content = stream.ToArray();
return File(content, contentType, fileName);
}
catch (Exception ex)
{
Helpers.LogAPIResponse(_hostingEnvironment.WebRootPath, "ExportProductsToExcel", "Export products Logging", ex);
return View();
}
}
public MemoryStream GetProductExcelStream()
{
using var workbook = new XLWorkbook();
List<VcproductCatalog> vcproductCatalogs = _context.VcproductCatalogs.ToList();
var worksheet = workbook.Worksheets.Add("Products");
// worksheet.Cell(1, 1).Value = "Id";
worksheet.Cell(1, 1).Value = "Name";
worksheet.Cell(1, 2).Value = "SKU";
worksheet.Cell(1, 3).Value = "Description";
worksheet.Cell(1, 4).Value = "Format";
worksheet.Cell(1, 5).Value = "Vintage";
worksheet.Cell(1, 6).Value = "Appelation";
worksheet.Cell(1, 7).Value = "Classification";
worksheet.Cell(1, 8).Value = "Color";
worksheet.Cell(1, 9).Value = "Price";
worksheet.Cell(1, 10).Value = "Active";
worksheet.Cell(1, 11).Value = "Cover Image";
var index = 1;
foreach (VcproductCatalog product in vcproductCatalogs)
{
// worksheet.Cell(index + 1, 1).Value = category.Id;
worksheet.Cell(index + 1, 1).Value = product.Name;
worksheet.Cell(index + 1, 2).Value = product.Sku;
worksheet.Cell(index + 1, 3).Value = product.Description;
worksheet.Cell(index + 1, 4).Value = product.Format;
worksheet.Cell(index + 1, 5).Value = product.Vintage;
worksheet.Cell(index + 1, 6).Value = product.Appelation;
worksheet.Cell(index + 1, 7).Value = product.Classification;
worksheet.Cell(index + 1, 8).Value = product.Color;
worksheet.Cell(index + 1, 9).Value = product.Price;
worksheet.Cell(index + 1, 10).Value = product.Active.ToString();
worksheet.Cell(index + 1, 11).Value = product.CoverImage.Replace("\r\n", "");
index += 1;
}
worksheet.Columns().AdjustToContents();
worksheet.Rows().AdjustToContents();
// Change the first row color
for (int i = 1; i <= 11; i++)
{
worksheet.Cell(1, i).Style.Font.Bold = true;
worksheet.Cell(1, i).Style.Font.FontColor = XLColor.White;
worksheet.Cell(1, i).Style.Fill.BackgroundColor = XLColor.FromHtml("#d74114");
}
MemoryStream stream = new MemoryStream();
stream.Flush();
stream.Position = 0;
workbook.SaveAs(stream);
return stream;
}
这也是完整记录的错误:
Could not load file or assembly 'SixLabors.Fonts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d998eea7b14cab13'. The system cannot find the file specified.
at ClosedXML.Graphics.DefaultGraphicEngine..ctor(String fallbackFont)
at ClosedXML.Graphics.DefaultGraphicEngine.<>c.<.cctor>b__29_0()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
--- End of stack trace from previous location ---
at System.Lazy`1.CreateValue()
at ClosedXML.Excel.XLWorkbook..ctor(LoadOptions loadOptions)
at VCCruise.Controllers.AdminController.GetCruiseExcelStream()
at VCCruise.Controllers.AdminController.ExportCruisesToExcel()
检查您的应用程序二进制文件中是否有 System.Numerics.Vectors.dll 的更新版本