Styling Excel Sheets Using Java and Apache POI

Apply styles, fonts, foreground colors, borders, and alignments to Excel cells in Java using Apache POI XSSFCellStyle.

In this JavaAnt guide, we walk through styling spreadsheet cells in Microsoft Excel files using Apache POI and Java.

Styling Cells With XSSFCellStyle

When creating reports, formatting header rows with background fills, bold fonts, and thin borders makes spreadsheets readable. Apache POI provides XSSFCellStyle and XSSFFont for customizing appearances.

StyleExcelExample.java
package com.javaant;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class StyleExcelExample {
    private static XSSFCellStyle createHeaderStyle(XSSFWorkbook wb, IndexedColors bgColor) {
        XSSFCellStyle style = wb.createCellStyle();

        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setColor(IndexedColors.WHITE.getIndex());
        font.setBold(true);
        style.setFont(font);

        style.setFillForegroundColor(bgColor.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        return style;
    }

    public static void main(String[] args) throws IOException {
        try (XSSFWorkbook wb = new XSSFWorkbook();
             FileOutputStream out = new FileOutputStream("styled_report.xlsx")) {

            XSSFSheet sheet = wb.createSheet("Report");
            XSSFCellStyle greenStyle = createHeaderStyle(wb, IndexedColors.GREEN);
            XSSFCellStyle blueStyle = createHeaderStyle(wb, IndexedColors.ROYAL_BLUE);

            XSSFRow row0 = sheet.createRow(0);
            XSSFCell cell0 = row0.createCell(0);
            cell0.setCellValue("Welcome to JavaAnt");
            cell0.setCellStyle(greenStyle);

            XSSFCell cell1 = row0.createCell(1);
            cell1.setCellValue("Report Date: " + new Date());
            cell1.setCellStyle(blueStyle);

            sheet.autoSizeColumn(0);
            sheet.autoSizeColumn(1);
            wb.write(out);
            System.out.println("Excel file created with styled cells.");
        }
    }
}

For reading data back from spreadsheets, see our reading Excel files tutorial. For large-scale streaming generation, see the Apache POI tutorial.

About Glenna Babshire29 Articles
Java & libraries writer. Writes the Java tutorials: the language from Java 21 on, encryption, configuration and the libraries around it.