Cell Styles: Fonts, Colors, Borders
Harry
· 24 Sep 2026
· 1 views
Log in to track your progress and mark lessons complete.
Sponsored
Create Styles Once, Reuse Everywhere
CellStyle header = wb.createCellStyle();
Font bold = wb.createFont();
bold.setBold(true);
bold.setColor(IndexedColors.WHITE.getIndex());
header.setFont(bold);
header.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
header.setFillPattern(FillPatternType.SOLID_FOREGROUND);
header.setAlignment(HorizontalAlignment.CENTER);
header.setBorderBottom(BorderStyle.THIN);
headerCell.setCellStyle(header);Practical Tips
- A workbook supports ~64,000 styles — create each style once and reuse the object, never one style per cell.
sheet.autoSizeColumn(i)after filling data; call it sparingly on huge sheets (it is slow).sheet.createFreezePane(0, 1)freezes the header row;sheet.setAutoFilter(...)adds Excel filters.- Dates are numeric cells with a date format:
style.setDataFormat(wb.createDataFormat().getFormat("dd-mmm-yyyy")).
Full runnable version: StylesExample in the poi-examples repo.