简体   繁体   English

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

[英]How do I change the font size in XSSFCellStyle?

I use the following method to define the cell styles for my worksheet.我使用以下方法为我的工作表定义单元格样式。 I then use cell.setCellStyle(XSSFCellStyle style) to assign them to different cells.然后我使用cell.setCellStyle(XSSFCellStyle style)将它们分配给不同的单元格。 However, although alignement and background color are assigned correctly, font-size and font emphasis (bold, regular) don´t.然而,尽管对齐方式和背景颜色分配正确,但字体大小和字体强调(粗体、常规)却没有。 All cells have 11 points bold.所有单元格都有 11 点粗体。 I´d like to know where my error lies.我想知道我的错误在哪里。

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);
}

Your problem lies in the below snippet of code:您的问题出在下面的代码片段中:

ueberschrift.getFont().setFontHeightInPoints((short) 25);
ueberschrift.getFont().setBold(true);

You use a getter and on its result you are setting.您使用吸气剂并在您设置的结果上。 But actually, you set the property of the object, but not the object itself.但实际上,您设置的是对象的属性,而不是对象本身。

Instead you should try this below:相反,您应该尝试以下操作:

font = ueberschrift.getFont();

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

ueberschrift.setFont(font);

The same pattern applies on where you try to set the font.相同的模式适用于您尝试设置字体的地方。

You should try this below code.你应该试试下面的代码。

 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);

The same pattern applies on where you try to set the font.相同的模式适用于您尝试设置字体的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM