Home > Mobile >  Cannot set font with CellUtil.setCellStyleProperties from ApachePOI
Cannot set font with CellUtil.setCellStyleProperties from ApachePOI

Time:01-21

Sample code:

    val workbook = XSSFWorkbook()
    val sheet = workbook.createSheet()

    val properties: MutableMap<String, Any> = mutableMapOf()
    val font = workbook.createFont()
    font.bold = true
    properties[CellUtil.BORDER_BOTTOM] = BorderStyle.DOUBLE
    properties[CellUtil.FILL_BACKGROUND_COLOR] = IndexedColors.GREY_50_PERCENT.index
    properties[CellUtil.FILL_FOREGROUND_COLOR] = IndexedColors.GREY_50_PERCENT.index
    properties[CellUtil.FILL_PATTERN] = FillPatternType.SOLID_FOREGROUND

    val row = sheet.createRow(0)
    val cell = row.createCell(0)
    CellUtil.setCellStyleProperties(cell, properties)
    CellUtil.setCellStyleProperty(cell, CellUtil.FONT, font)
    cell.setCellValue("This should be bold.")

    val fileOut = FileOutputStream("customers.xlsx")
    workbook.write(fileOut)
    fileOut.close()
    workbook.close()

Both the background color and the border are set correctly, the font however does not appear to be set in the document itself, as can be seen here: Resulting table

I am using apache poi-ooxml version 5.2.0, but I've tried other versions with no success. I'm also using Microsoft Excel for Microsoft 365 MSO (16.0.14326.20706) 64-bit.

NOTE: I haven't had this issue before, it appeared only recently, roughly 5th Jan.

Any idea what might be going on?

CodePudding user response:

To set the font with setCellStyleProperty you need to pass the font's index as a value, not a font itself:

CellUtil.setCellStyleProperty(cell, CellUtil.FONT, font.index)

Alternatively, you may use setFont method, accepting Font:

CellUtil.setFont(cell, font)
  •  Tags:  
  • Related