How do I change the font size in XSSFCellStyle?

Viewed 13032

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. However, although alignement and background color are assigned correctly, font-size and font emphasis (bold, regular) don´t. All cells have 11 points bold. 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);
}
2 Answers

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.

Related