EasyExcel给指定单元格加样式(CellWriteHandler版)
package com.szc.computing.MechanicalCalculationService.util;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
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 com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import java.util.List;
/**
* 将全表符合要求的(是公式)的设为公式类型(只要是以等于号开头)
* @author zhj
*/
public class Table1StyleUtil implements CellWriteHandler {
public Table1StyleUtil() {
}
@Override
public void beforeCellCreate(CellWriteHandlerContext context) {
CellWriteHandler.super.beforeCellCreate(context);
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
CellWriteHandler.super.beforeCellCreate(writeSheetHolder, writeTableHolder, row, head, columnIndex, relativeRowIndex, isHead);
}
@Override
public void afterCellCreate(CellWriteHandlerContext context) {
CellWriteHandler.super.afterCellCreate(context);
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
CellWriteHandler.super.afterCellCreate(writeSheetHolder, writeTableHolder, cell, head, relativeRowIndex, isHead);
}
@Override
public void afterCellDataConverted(CellWriteHandlerContext context) {
CellWriteHandler.super.afterCellDataConverted(context);
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, WriteCellData<?> cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
CellWriteHandler.super.afterCellDataConverted(writeSheetHolder, writeTableHolder, cellData, cell, head, relativeRowIndex, isHead);
}
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
// (context);
// 当前事件会在 数据设置到poi的cell里面才会回调
// 判断不是头的情况 如果是fill 的情况 这里会==null 所以用not true
if (BooleanUtils.isNotTrue(context.getHead())) {
Cell cell = context.getCell();
if(cell.getColumnIndex()==17&&cell.getStringCellValue().equals("调整")){
// 第一个单元格
// 只要不是头 一定会有数据 当然fill的情况 可能要() ,这个需要看模板,因为一个单元格会有多个 WriteCellData
WriteCellData<?> cellData = context.getFirstCellData();
// 这里需要去cellData 获取样式
// 很重要的一个原因是 WriteCellStyle 和 dataFormatData绑定的 简单的说 比如你加了 DateTimeFormat
// ,已经将writeCellStyle里面的dataFormatData 改了 如果你自己new了一个WriteCellStyle,可能注解的样式就失效了
// 然后 getOrCreateStyle 用于返回一个样式,如果为空,则创建一个后返回
WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 这样样式就设置好了 后面有个FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到 cell里面去 所以可以不用管了
}
}
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// CellType cellType = ();
// if(==cellType){
// StringBuffer stringBuffer = new StringBuffer(());
// if(()>=1){
// char c = (0);
// if (c=='='){
// ( (0,1,"").toString());
// }
// }
//
// }
// j
if(cell.getColumnIndex()==8){
if(cell.getCellType()==CellType.BOOLEAN){
if(cell.getBooleanCellValue()==true){
cell.setCellType(CellType.STRING);
cell.setCellValue("推荐");
}else{
cell.setCellType(CellType.STRING);
cell.setCellValue("不推荐");
}
}
}
// 0设置true为推荐,false为不推荐
if(cell.getColumnIndex()==14){
if(cell.getCellType()==CellType.BOOLEAN){
if(cell.getBooleanCellValue()==true){
cell.setCellType(CellType.STRING);
cell.setCellValue("推荐");
}else{
cell.setCellType(CellType.STRING);
cell.setCellValue("不推荐");
}
}
}
}
@Override
public int order() {
return CellWriteHandler.super.order();
}
}