Cell Styles: Fonts, Colors, Borders

Harry · 24 Sep 2026 · 1 views
Log in to track your progress and mark lessons complete.

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

Styled header row with colors and borders

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.

Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

or sign in with your account

Already have an account? Log in