EasyExcel动态设置行背景色

自定义处理器

package com.ruoyi.web.part.service.impl;

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.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;

import java.util.List;
import java.util.Map;

/**
 * 功能描述: 动态设置行背景色
 *
 * @version v 1.0
 * @Author:
 * @Date: 2024-06-12 19:08
 */
public class RowBackGroundWriteHandler implements CellWriteHandler {


    /**
     * key:行下标集合,value:背景色
     */
    private final Map<List<Integer>, Short> rowBackColor;

    public RowBackGroundWriteHandler(Map<List<Integer>, Short> rowBackColor) {
        this.rowBackColor = rowBackColor;
    }

    @Override
    public void beforeCellCreate(CellWriteHandlerContext context) {
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        if (BooleanUtils.isNotTrue(context.getHead())) {
            // 当前行号
            Integer currentRowIndex = context.getRowIndex();
            for (Map.Entry<List<Integer>, Short> next : rowBackColor.entrySet()) {
                List<Integer> rowIndex = next.getKey();
                Short colorIndex = next.getValue();
                if (rowIndex.contains(currentRowIndex)) {
                    WriteCellData<?> cellData = context.getFirstCellData();
                    WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
                    // 设置颜色
                    writeCellStyle.setFillForegroundColor(colorIndex);
                    writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
                }
            }

        }
    }

}

导出Excel

public void exportExcel(HttpServletResponse response) {
	// 导出数据
	List<YourExportVo> exportList = Lists.newArrayList;
	ExcelWriter excelWriter = null;
	// 文件名
	String fileName = "Excel名称" + DateUtil.format(new Date(), "yyyy-MM-dd") + ".xls";
	OutputStream out = null;
	try {
		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
		out = response.getOutputStream();
		excelWriter = EasyExcel.write(out).build();
		// 填充行背景色
		Map<List<Integer>, Short> rowBackColor = new HashMap<>();
		// 第一行红色
		rowBackColor.put(Collections.singletonList(1), IndexedColors.RED.index);
		// 第二行黄色
		rowBackColor.put(Collections.singletonList(2), IndexedColors.YELLOW.index);
		// 第三行绿色
		rowBackColor.put(Collections.singletonList(3)owIndex, IndexedColors.GREEN.index);
		// 自定义处理器
		RowBackGroundWriteHandler handler = new RowBackGroundWriteHandler(rowBackColor);
		WriteSheet writeSheet = EasyExcel.writerSheet(0, "sheet名称")
				.registerWriteHandler(handler)
				.head(YourExportVo.class)
				.build();
		excelWriter.write(exportList, writeSheet);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (excelWriter != null) {
			excelWriter.finish();
		}
	}
}
	
posted @ 2024-06-12 19:57  Liberty&  阅读(713)  评论(0)    收藏  举报