/**
* @Title: ExcelHeadContentUtil
* @Author dxp
* @Date 2024/6/19 10:36
* @description:
*/
public class ExcelHeadContentUtil implements RowWriteHandler {
private Map<Map<Integer, List<Integer>>,byte[]> colorMap;
public ExcelHeadContentUtil(Map<Map<Integer, List<Integer>>,byte[]> colorMap) {
this.colorMap = colorMap;
}
public ExcelHeadContentUtil(){
}
@Override
public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {
}
@Override
public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
}
@Override
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
if(colorMap != null && !colorMap.isEmpty()){
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
short lastCellNum = row.getLastCellNum();
colorMap.forEach((key,value) -> {
key.forEach((a,b) -> {
if(row.getRowNum() == a){
for (int i = 0; i < lastCellNum; i++) {
Cell cell = row.getCell(i);
XSSFCellStyle cellStyle = getCellStyle(workbook);
if(b.contains(i)){
cellStyle.setFillForegroundColor(new XSSFColor(value,null));
cell.setCellStyle(cellStyle);
}
}
}
});
});
}
}
private XSSFCellStyle getCellStyle(Workbook workbook){
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 10);
font.setFontName("微软雅黑");
XSSFCellStyle xssfCellStyle = (XSSFCellStyle) cellStyle;
xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
xssfCellStyle.setFont(font);
return xssfCellStyle;
}
}
02-10
905
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-19
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-28