2

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
Am Ma
  • 21
  • 1
  • 3
    ClosedXML >= 0.100 is FUBAR, use 0.97 until they figure that stuff out. – CodeCaster May 16 '23 at 13:13
  • Wha.... why would it be FUBARed – Am Ma May 16 '23 at 13:23
  • 1
    Because they changed a lot of dependencies in version 0.100. – CodeCaster May 16 '23 at 13:28
  • Anyway what you're doing should just work. Is your server 32-bit (x86) by any chance? What happens if you debug the assembly binding? See https://stackoverflow.com/questions/66443083/how-can-i-trace-failed-assembly-binding-in-a-dotnet-core-web-project-running-in – CodeCaster May 16 '23 at 13:30
  • Has ClosedXML improved since the above posts? (The use of the unreleased SixLabors font was the blocker for us; the fonts package is still unreleased but do they still depend on it?). [Their latest stable release in VS2019 is shown as V102.0.0] – Zeek2 Jul 18 '23 at 11:52
  • [SixLabors.Fonts 1.0.0](https://www.nuget.org/packages/SixLabors.Fonts/1.0.0) was released a few hours ago. – 0xced Aug 11 '23 at 15:01

0 Answers0