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

3

我使用以下方法来定义工作表中的单元格样式。然后,我使用 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);
}
2个回答

6
您的问题出现在以下代码片段中:
ueberschrift.getFont().setFontHeightInPoints((short) 25);
ueberschrift.getFont().setBold(true);

你使用一个 getter 并在其结果上进行设置。但实际上,你设置的是对象的属性,而不是对象本身。
相反,你应该尝试下面的代码:
font = ueberschrift.getFont();

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

ueberschrift.setFont(font);

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

2
你应该尝试下面的代码。
 cellc1.setCellStyle(cellStyle);
 XSSFFont  font = wb.createFont();
 font =cellStyle.getFont();
 font.setFontHeightInPoints((short)25);
 font.setColor(IndexedColors.BLUE.getIndex());
 font.setColor(IndexedColors.YELLOW.GOLD.getIndex());
 cellStyle.setFont(font);

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接