如何更改 XSSFCellStyle 中的字体大小?

Jan*_*kis 2 java apache-poi

我使用以下方法为我的工作表定义单元格样式。然后我cell.setCellStyle(XSSFCellStyle style)用来将它们分配给不同的单元格。然而,虽然对齐方式和背景颜色分配正确,但字体大小和字体强调(粗体、常规)却没有。所有单元格都有 11 点粗体。我想知道我的错误在哪里。

private void createStyles() {
    ueberschrift = workbook.createCellStyle();
    ueberschrift.setAlignment(HorizontalAlignment.LEFT);
    ueberschrift.getFont().setFontHeightInPoints((short) 25);
    ueberschrift.getFont().setBold(true);

    header = workbook.createCellStyle();
    header.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    header.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    header.setAlignment(HorizontalAlignment.CENTER);
    header.getFont().setFontHeightInPoints((short)11);

    standard_text = workbook.createCellStyle();
    standard_text.setAlignment(HorizontalAlignment.RIGHT);
    standard_text.getFont().setFontHeightInPoints((short) 11);

    standard_int = workbook.createCellStyle();
    standard_int.setDataFormat(
            workbook.createDataFormat().getFormat("0.0"));
    standard_int.setAlignment(HorizontalAlignment.RIGHT);
    standard_int.getFont().setFontHeightInPoints((short)11);

    standard_time = workbook.createCellStyle();
    standard_time.setDataFormat(workbook.createDataFormat().getFormat("# ?/?"));
    standard_time.setAlignment(HorizontalAlignment.RIGHT);
}
Run Code Online (Sandbox Code Playgroud)

01_*_*1__ 5

您的问题在于以下代码片段:

ueberschrift.getFont().setFontHeightInPoints((short) 25);
ueberschrift.getFont().setBold(true);
Run Code Online (Sandbox Code Playgroud)

您使用 getter 并在其结果上进行设置。但实际上,您设置的是对象的属性,而不是对象本身。

相反,您应该尝试以下操作:

font = ueberschrift.getFont();

font.setFontHeightInPoints((short) 25);
font.setBold(true);

ueberschrift.setFont(font);
Run Code Online (Sandbox Code Playgroud)

相同的模式适用于您尝试设置字体的位置。