示例代碼:
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()
背景顏色和邊框都設定正確,但是字體似乎沒有在檔案本身中設定,如下所示: 結果表
我正在使用 apache poi-ooxml 5.2.0 版,但我嘗試了其他版本但沒有成功。我還將 Microsoft Excel 用于 Microsoft 365 MSO (16.0.14326.20706) 64 位。
注意:我以前沒有遇到過這個問題,它最近才出現,大約是 1 月 5 日。
知道會發生什么嗎?
uj5u.com熱心網友回復:
要設定字體,setCellStyleProperty您需要將字體的索引作為值傳遞,而不是字體本身:
CellUtil.setCellStyleProperty(cell, CellUtil.FONT, font.index)
或者,您可以使用setFont方法,接受Font:
CellUtil.setFont(cell, font)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417853.html
標籤:
