You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产环境用ClosedXML导出Excel报错:无法加载SixLabors.Fonts程序集

Asp.Net Core 6 + ClosedXML 导出Excel生产环境报错问题

基于Asp.Net Core 6开发的项目,使用ClosedXML NuGet包实现数据库表导出Excel功能,本地localhost运行完全正常,但部署到生产环境后出现报错。已确认生产服务器上存在SixLabors.dll文件,重新安装SixLabors NuGet包也未解决问题,检查代码逻辑未发现异常。

代码

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()    

已排查点

  • 本地和生产服务器均存在报错提示中缺失的DLL文件
  • 重新下载安装SixLabors NuGet包后问题仍未解决
  • 检查导出相关代码逻辑,未发现异常

解决方案建议

  1. 核对DLL版本匹配:报错明确指向SixLabors.Fonts, Version=1.0.0.0,检查生产服务器上的SixLabors.Fonts.dll版本是否为1.0.0.0,ClosedXML对依赖库版本要求严格,版本不匹配会导致加载失败。
  2. 设置依赖项复制本地:在项目的NuGet引用中找到SixLabors.Fonts,右键打开属性,确保复制本地选项设置为True,保证发布时正确版本的DLL会被复制到输出目录。
  3. 禁用图形引擎规避依赖:ClosedXML默认依赖SixLabors.Fonts处理字体渲染,若生产环境加载问题无法快速解决,可尝试禁用图形引擎,修改XLWorkbook初始化代码:
var options = new XLWorkbookOptions { GraphicEngine = new NoOpGraphicEngine() };
using var workbook = new XLWorkbook(options);

注意:禁用后,自动调整列宽等依赖字体计算的功能会受影响,需根据业务场景评估是否可用。
4. 清理后重新发布:清理项目的bin、obj目录,重新生成后再发布到生产环境,避免旧依赖文件残留引发冲突。

内容的提问来源于stack exchange,提问作者Am Ma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 13:35:32