EasyExcel 批量设置单元格样式

41 DemoData(数据实体类)

package com.entity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class DemoData {
        @ExcelProperty("字符串标题")
        private String string;
        @ExcelProperty("日期标题")
        private Date date;
        @ExcelProperty("数字标题")
        private Double doubleData;
        /**
         * 忽略这个字段
         */
        @ExcelIgnore
        private String ignore;
    }

2 CellStyleWriteHandler(样式拦截器)

package com.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 样式拦截器
 *
 */
public class CellStyleWriteHandler extends AbstractRowWriteHandler {
    /**
     * 列索引key
     */
    public static final String COLINDEX_NAME = "colIndex";
    /**
     * 行索引key
     */
    public static final String ROWINDEX_NAME = "rowIndex";
    private List<Map<String, Object>> cellStyleList = new ArrayList<>();

    public CellStyleWriteHandler(List<Map<String, Object>> cellStyleList) {
        if (cellStyleList == null || cellStyleList.size() <= 0) {
            return;
        }
        cellStyleList = cellStyleList.stream().filter(x -> x.keySet().contains(COLINDEX_NAME) == true && x.get(COLINDEX_NAME) != null && StrUtil.isNotBlank(x.get(COLINDEX_NAME).toString())
                && x.keySet().contains(ROWINDEX_NAME) == true && x.get(ROWINDEX_NAME) != null && StrUtil.isNotBlank(x.get(ROWINDEX_NAME).toString())
                && x.keySet().contains(STYLETYPE_NAME) == true && x.get(STYLETYPE_NAME) != null && StrUtil.isNotBlank(x.get(STYLETYPE_NAME).toString())).collect(Collectors.toList());
        this.cellStyleList = cellStyleList;
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,Integer relativeRowIndex, Boolean isHead) {
        if (cellStyleList == null || cellStyleList.size() <= 0) {
            return;
        }
        String cellRowIndex = row.getRowNum() + "";
        if (StrUtil.isBlank(cellRowIndex)) {
            return;
        }
        List<Map<String, Object>> cellMapList = cellStyleList.stream().filter(x -> StrUtil.equals(x.get(ROWINDEX_NAME).toString(), cellRowIndex)).collect(Collectors.toList());
        //该行不需要设置样式
        if (cellMapList == null || cellMapList.size() <= 0) {
            return;
        }
        for (Map<String, Object> map : cellMapList ) {
            //样式信息
            String colIndex = map.get(COLINDEX_NAME) == null ? "" : map.get(COLINDEX_NAME).toString();
            String rowIndex = map.get(ROWINDEX_NAME) == null ? "" : map.get(ROWINDEX_NAME).toString();
            //该样式信息不完整
            if (map.keySet().contains(COLINDEX_NAME) == false || StrUtil.isBlank(colIndex)
                    || map.keySet().contains(ROWINDEX_NAME) == false || StrUtil.isBlank(rowIndex)) {
                continue;
            }
            Row thisRow = sheet.getRow(Integer.parseInt(rowIndex));
            if (thisRow == null) {
                thisRow = sheet.createRow(Integer.parseInt(rowIndex));
            }
            Cell cell = sheet.getRow(Integer.parseInt(rowIndex)).getCell(Integer.parseInt(colIndex));
            if (cell == null) {
                cell = thisRow.createCell(Integer.parseInt(colIndex));
            }
            CellStyle thisCellStyle = cell.getRow().getSheet().getWorkbook().createCellStyle();
            // 克隆出一个 style
            thisCellStyle.cloneStyleFrom(cell.getCellStyle());
            thisCellStyle.setFillForegroundColor(IndexedColors.ROSE.getIndex());
            thisCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cell.setCellStyle(thisCellStyle);
        }
    }
}

 3 调试代码

    public void test() {
        String fileName = "d:/commentWrite/commentWrite" + DateUtil.format(new Date(), "yyyy_MM_dd_HH_mm_ss") + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(fileName, Map.class).registerWriteHandler(new CellStyleWriteHandler(cellStyle())).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
        excelWriter.write(data(), writeSheet);
        // 千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }
    /**
     * 生成测试样式信息
     * @return
     */
   public static List<Map<String, Object>> cellStyle() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 2; j++) {
                if (i == j) {
                    continue;
                }
                Map<String, Object> map = new HashMap<>();
                map.put(CellStyleWriteHandler.COLINDEX_NAME, j);
                map.put(CellStyleWriteHandler.ROWINDEX_NAME, i);
                list.add(map);
            }

        }
        return list;
    }
    /**
     * 生成测试数据
     * @return
     */
    public static List<DemoData> data() {
        List<DemoData> list = new ArrayList<DemoData>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

4 调试结果

 

猜你喜欢

转载自blog.csdn.net/qq_38974638/article/details/114841208