Skip to main content

Style

This chapter introduces style settings when writing data.

Annotations​

Overview​

Set cell styles through annotations in entity classes, including font, background color, row height, etc.

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
// Set header background to red
@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10)
// Set header font size to 20
@HeadFontStyle(fontHeightInPoints = 20)
// Set content background to green
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 17)
// Set content font size to 20
@ContentFontStyle(fontHeightInPoints = 20)
public class DemoStyleData {
// Individually set header and content styles for a specific column
@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 14)
@HeadFontStyle(fontHeightInPoints = 30)
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)
@ContentFontStyle(fontHeightInPoints = 30)
@ExcelProperty("String Title")
private String string;

@ExcelProperty("Date Title")
private Date date;

@ExcelProperty("Number Title")
private Double doubleData;
}

Code Example​

@Test
public void annotationStyleWrite() {
String fileName = "annotationStyleWrite" + System.currentTimeMillis() + ".xlsx";

FesodSheet.write(fileName, DemoStyleData.class)
.sheet()
.doWrite(data());
}

Result​

ABC
1String TitleDate TitleNumber Title
2String02026-07-31 20:50:230.56
3String12026-07-31 20:50:230.56
4String22026-07-31 20:50:230.56
⋮………
11String92026-07-31 20:50:230.56

Built-in Interceptors​

Overview​

Use HorizontalCellStyleStrategy to set styles for headers and content separately.

Code Example​

@Test
public void handlerStyleWrite() {
String fileName = "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx";

// Define header style
WriteCellStyle headStyle = new WriteCellStyle();
headStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); // Red background
WriteFont headFont = new WriteFont();
headFont.setFontHeightInPoints((short) 20); // Font size 20
headStyle.setWriteFont(headFont);

// Define content style
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); // Green background
contentStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
WriteFont contentFont = new WriteFont();
contentFont.setFontHeightInPoints((short) 20);
contentStyle.setWriteFont(contentFont);

// Use strategy to set styles
HorizontalCellStyleStrategy styleStrategy =
new HorizontalCellStyleStrategy(headStyle, contentStyle);

FesodSheet.write(fileName, DemoData.class)
.registerWriteHandler(styleStrategy)
.sheet("Style Template")
.doWrite(data());
}

Result​

ABC
1String TitleDate TitleNumber Title
2String02026-07-31 20:50:230.56
3String12026-07-31 20:50:230.56
4String22026-07-31 20:50:230.56
⋮………
11String92026-07-31 20:50:230.56

Custom Interceptors​

Overview​

If existing strategies cannot meet requirements, you can implement the CellWriteHandler interface for complete custom control over styling.

Code Example​

Custom interceptor

@Slf4j
public class CustomCellStyleWriteHandler implements CellWriteHandler {

@Override
public void afterCellDispose(CellWriteHandlerContext context) {
// Only set styles for content cells
if (BooleanUtils.isNotTrue(context.getHead())) {
WriteCellData<?> cellData = context.getFirstCellData();
WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();

// Set background color to yellow
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

// Set font to blue
WriteFont writeFont = new WriteFont();
writeFont.setColor(IndexedColors.BLUE.getIndex());
writeFont.setFontHeightInPoints((short) 14); // Font size 14
writeCellStyle.setWriteFont(writeFont);

log.info("Custom cell style applied: row {}, column {}", context.getRowIndex(), context.getColumnIndex());
}
}
}

Usage

@Test
public void customCellStyleWrite() {
String fileName = "customCellStyleWrite" + System.currentTimeMillis() + ".xlsx";

FesodSheet.write(fileName, DemoData.class)
.registerWriteHandler(new CustomCellStyleWriteHandler())
.sheet("Custom Style")
.doWrite(data());
}

Custom POI Styles​

Overview​

Directly manipulate POI's CellStyle, suitable for precise style control.

Code Example​

@Test
public void poiStyleWrite() {
String fileName = "poiStyleWrite" + System.currentTimeMillis() + ".xlsx";

FesodSheet.write(fileName, DemoData.class)
.registerWriteHandler(new CellWriteHandler() {
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
if (BooleanUtils.isNotTrue(context.getHead())) {
Cell cell = context.getCell();
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();

// Create and set style
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
}
}
})
.sheet("POI Style")
.doWrite(data());
}

Column Width and Row Height​

Overview​

Control column width and row height through annotations, suitable for scenarios with specific table format requirements.

POJO Class​

@Getter
@Setter
@EqualsAndHashCode
@ContentRowHeight(20)
@HeadRowHeight(30)
@ColumnWidth(25) // Default column width
public class WidthAndHeightData {
@ExcelProperty("String Title")
private String string;

@ExcelProperty("Date Title")
private Date date;

@ColumnWidth(50) // Individually set column width
@ExcelProperty("Number Title")
private Double doubleData;
}

Code Example​

@Test
public void widthAndHeightWrite() {
String fileName = "widthAndHeightWrite" + System.currentTimeMillis() + ".xlsx";

FesodSheet.write(fileName, WidthAndHeightData.class)
.sheet()
.doWrite(data());
}

Result​

ABC
1String TitleDate TitleNumber Title
2String02026-07-31 20:50:230.56
3String12026-07-31 20:50:230.56
4String22026-07-31 20:50:230.56
⋮………
11String92026-07-31 20:50:230.56