EasyExcel自定义指定单元格的背景颜色(指定RGB值)

88 阅读1分钟

我使用的版本是3.1.5

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.1.5</version>
</dependency>
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.xinscm.project.fms.sass.common.enums.report.ProfitReportRecFeeEnum;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.awt.*;
import java.util.List;
import java.util.Objects;

public class CustomCellColorHandler implements CellWriteHandler {

        // 需要导出的Excel数据.我是用来做条件判断的,如果你不需要可以不加(可选)
	private final List<YourExcelDataEntity> dataList;

	public CustomCellColorHandler(List<YourExcelDataEntity> dataList) {
		this.dataList = dataList;
	}

	private final static Color CT_COLOR = new Color(198, 254, 207);

	private final static Color CW_COLOR = new Color(255, 135, 0);

	@Override
	public void afterCellDispose(CellWriteHandlerContext context) {
		if (context.getHead()) {
			return;
		}
		// 列下标
		Integer columnIndex = context.getColumnIndex();

		// 行下标
		Integer relativeRowIndex = context.getRelativeRowIndex();

		// 校验行数据是否存在
		if (relativeRowIndex < 0 || relativeRowIndex >= dataList.size()) {
		    return;
		}
               
		YourExcelDataEntity vo = dataList.get(relativeRowIndex);

		// 条件判断(省略...)
		String status = null;
                
		// 根据状态设置颜色
		if ("confirm".equals(status)) {
			// 已确认-浅绿色
			setExcelBgColor(context, CT_COLOR);
		} else if ("cancel".equals(status)) {
			// 取消-橙色
			setExcelBgColor(context, CW_COLOR);
		}
	}

        /**
        * 设置单元格背景颜色指定RGB值
        **/
	private static void setExcelBgColor(CellWriteHandlerContext context, Color color) {
		WriteCellData<?> cellData = context.getFirstCellData();
		CellStyle originCellStyle = cellData.getOriginCellStyle();
		if (Objects.isNull(originCellStyle)) {
			originCellStyle = context.getWriteWorkbookHolder().getWorkbook().createCellStyle();
		}
		// 设置背景颜色
		((XSSFCellStyle) originCellStyle).setFillForegroundColor(new XSSFColor(color, new DefaultIndexedColorMap()));
		originCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

		WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();
		writeCellStyle.setFillForegroundColor(null);
		// 设置originalCellStyle
		cellData.setOriginCellStyle(originCellStyle);
	}

}

写Excel时注册handler

/**
* 浏览器下载操作
**/
	public void orderExport(ExportParam order, HttpServletResponse response) {
    
// 获取数据代码省略...

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

EasyExcel.write(response.getOutputStream(), YourExcelDataEntity.class)
// 注册handler
.registerWriteHandler(new CustomCellColorHandler(exportList)) 
.doWrite(exportList);
}