I have a dotnet 8 C# application running in AWS Lambda. Prior to dotnet 8 I used dotnet 6 and the Excel file creation was working as expected. Now, when the project is upgraded to dotnet 8, I get some errors when an Excel file is being created.
[Error] Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction: Unknown error responding to request: FontException:
SixLabors.Fonts.FontException: No fonts found installed on the machine.
I’m using the NPOI nuget, version 2.7.0.
I tried adding the SixLabels.Fonts nuget (2.0.3) but to no avail.
Anyone knows how to get AWS Lambda to load some default fonts. I’m not using docker and would preferably not switch to docker images.
Code below. Error is thrown at row where worksheet.AutoSizeRow(rowIndex); is called:
ISheet worksheet = workbook.CreateSheet(customerId);
#region styling
IFont boldFont = workbook.CreateFont();
boldFont.IsBold = true;
var cellStyleCategory = workbook.CreateCellStyle();
cellStyleCategory.FillPattern = FillPattern.SolidForeground;
cellStyleCategory.SetFont(boldFont);
((XSSFCellStyle)cellStyleCategory).SetFillForegroundColor(new XSSFColor(IndexedColors.LightGreen));
var cellStyleSubcategory = workbook.CreateCellStyle();
cellStyleSubcategory.FillPattern = FillPattern.SolidForeground;
cellStyleSubcategory.SetFont(boldFont);
((XSSFCellStyle)cellStyleSubcategory).SetFillForegroundColor(new XSSFColor(IndexedColors.LightYellow));
var richTextSupport = workbook.CreateCellStyle();
richTextSupport.WrapText = true;
var cellStyleLogo = workbook.CreateCellStyle();
IFont boldFontSize1 = workbook.CreateFont();
boldFontSize1.IsBold = true;
boldFontSize1.FontHeightInPoints = 20;
cellStyleLogo.SetFont(boldFontSize1);
var cellStyleCustomer = workbook.CreateCellStyle();
var boldFontSize2 = workbook.CreateFont();
boldFontSize2.IsBold = true;
boldFontSize2.FontHeightInPoints = 16;
cellStyleCustomer.SetFont(boldFontSize2);
#endregion styling
var previousRow = -1;
var highestCellCount = 4;
#region header
var LOGOCOLUMN = 0;
IRow firstRow = worksheet.CreateRow(++previousRow);
firstRow.CreateCell(LOGOCOLUMN).SetCellValue("App name");
firstRow.Cells[0].CellStyle = cellStyleLogo;
worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(previousRow, previousRow, LOGOCOLUMN, highestCellCount));
var CUSTOMERCOLUMN = 0;
IRow secondRow = worksheet.CreateRow(++previousRow);
secondRow.CreateCell(CUSTOMERCOLUMN).SetCellValue("Foobar");
secondRow.Cells[0].CellStyle = cellStyleCustomer;
worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(previousRow, previousRow, CUSTOMERCOLUMN, highestCellCount));
worksheet.CreateRow(++previousRow);
worksheet.CreateRow(++previousRow);
var K = 0;
var S = 1;
var L = 2;
var C = 3;
var INFO = 4;
IRow header = worksheet.CreateRow(++previousRow);
header.CreateCell(K).SetCellValue("K");
header.CreateCell(S).SetCellValue("S");
header.CreateCell(L).SetCellValue("E");
header.CreateCell(C).SetCellValue("I");
header.CreateCell(INFO).SetCellValue("I/h");
#endregion header
labelList.Categories.ForEach(c =>
{
c.SubCategories.ForEach(s =>
{
IRow category = worksheet.CreateRow(++previousRow);
category.CreateCell(K).SetCellValue(c.Name);
category.Cells[0].CellStyle = cellStyleCategory;
category.CreateCell(S).SetCellValue(s.Name);
category.Cells[1].CellStyle = cellStyleSubcategory;
s.Labels.ForEach(l =>
{
IRow label = worksheet.CreateRow(++previousRow);
label.CreateCell(L).SetCellValue("Bar");
var components = new StringBuilder();
l.Components.ForEach(lc =>
{
components.Append("Quuxn");
});
label.CreateCell(C).SetCellValue(components.ToString());
label.Cells[1].CellStyle = richTextSupport;
label.CreateCell(INFO).SetCellValue("Monkey");
label.Cells[2].CellStyle = richTextSupport;
});
++previousRow;
});
});
// Make sure row and column content is visible
for (int rowIndex = 0; rowIndex <= previousRow; rowIndex++)
{
worksheet.AutoSizeRow(rowIndex); // <-- error is thrown here
}
for (int columnIndex = 0; columnIndex <= highestCellCount; columnIndex++)
{
worksheet.AutoSizeColumn(columnIndex);
}
worksheet.CreateRow(++previousRow);
worksheet.CreateRow(++previousRow);
using (MemoryStream memoryStream = new())
{
workbook.Write(memoryStream);
byte[] binaryBlob = memoryStream.ToArray();
return (binaryBlob, workbook);
}