List<Integer> backGroundIndex = new ArrayList<>();
backGroundIndex.add(1);
Set<Integer> yellowRowsSet = new HashSet<>(backGroundIndex);
QualificationsDeclareCellWriteHandler customCellWriteHandler = new QualificationsDeclareCellWriteHandler(yellowRowsSet);
Set<String> excludeColumnFiledNames = new HashSet<String>();
excludeColumnFiledNames.add("pxqz");
try {
fileName = URLEncoder.encode(fileName, "utf-8");
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setContentType("application/octet-stream; charset=UTF-8");
EasyExcel.write(response.getOutputStream(), BatchScoreVO.class)
.registerWriteHandler(customCellWriteHandler)
.sheet("背景色标记").doWrite(resultList);
} catch (IOException e) {
e.printStackTrace();
}
- QualificationsDeclareCellWriteHandler工具类代码如下,通过覆写afterCellDispose方法给行标记背景色
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.*;
import java.util.List;
import java.util.Set;
public class QualificationsDeclareCellWriteHandler implements CellWriteHandler{
private final Set<Integer> colorRowIndex;
public QualificationsDeclareCellWriteHandler(Set<Integer> colorRowIndex) {
this.colorRowIndex = colorRowIndex;
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if (!isHead && cell.getColumnIndex() <= 8 && colorRowIndex.contains(relativeRowIndex)) {
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
Font cellFont = workbook.createFont();
cellFont.setFontName("宋体");
cellStyle.setFont(cellFont);
cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
cellStyle.setFillPattern(FillPatternType.FINE_DOTS);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cell.setCellStyle(cellStyle);
}
}
}