org.apache.poi
poi
4.1.2
org.apache.poi
poi-ooxml
4.1.2
//获取要设置颜色的cell
XSSFCell cell = row.createCell(0);
//创建单元格样式
XSSFCellStyle style = workbook.createCellStyle();
//设置颜色
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//将样式应用到cell
cell.setCellStyle(style);
其中,IndexedColors.RED.getIndex() 可以替换为其他颜色的枚举值。