When in production I am trying to export database table into excel using ClosedXML nuget package however I get this error in the title, I tried it locally on localhost but it works perfectly fine. I have checked the dll files in production and the SixLabors.dll is present. Here is my code I am using 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;
}
Here is also the full logged error:
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()
- The DLL file thats missing is found in the local and production server
- Downloaded the SixLabors nuget package but it no success
- Read my code carefully and couldn't see anything wrong with the way i am exporting