填充单元格,EasyExcel 是按列顺序处理单元格的??????

eaf3rand  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(128)
Cell curGroupCell = cell.getSheet().getRow(curRowIndex).getCell(groupColumnIndex);
Cell preGroupCell = cell.getSheet().getRow(curRowIndex - 1).getCell(groupColumnIndex);

假设说groupColumnIndex 4 , 那么为什么 0 1 2 3 会导致无法合并单元格 5、6、7 在groupColumnIndex 可以合并单元格,这是EasyExccel的什么原因导致的,怎么解决?????

猜测原因问题原因:
EasyExcel 是按列顺序处理单元格的。
当处理到 groupColumnIndex (4) 之前的列 (0, 1, 2, 3) 时,groupColumnIndex 的值还没有被写入。
我们的检查逻辑试图访问还不存在的 groupColumnIndex 列的值,导致无法正确判断是否应该合并。
而处理 5、6、7 列时,groupColumnIndex 列已经有值了,所以可以正常合并。

// 分组依据的列索引 只有在第一列,int groupColumnIndex = 0; 才能保证后续每列分组后单元格合并正常,但有时候分组列需求就是要放在其它位置,请问如何解决

// 追加写
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("企业诉求跟踪办理通知单情况表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        InputStream resourceAsStream = ResourceLoader.class.getResourceAsStream("/excel/data_report3_del.xlsx");
        try (ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(resourceAsStream).build()) {
            // 需要合并的列
            int[] mergeColumnIndex = {0, 2,4, 5, 6, 7};
            // 需要从第几行开始合并
            int mergeRowIndex = 3;
            // 分组依据的列索引
            int groupColumnIndex = 0;

            WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new ExcelMergeHandler2(mergeRowIndex, mergeColumnIndex, groupColumnIndex))
                    .build();

            // Map<String, Object> map = new HashMap<>();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();// 填充配置。
            excelWriter.fill(new FillWrapper("data", rsList), fillConfig, writeSheet);
        } catch (Exception e) {
            IoUtil.close(outputStream);
            throw new RuntimeException(e.getMessage());
        } finally {
            IoUtil.close(outputStream);
        }
package com.ruoyi.biz.utils.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

/**
* @Author: lxy
* @CreateTime: 2024-06-12
* @Description: EasyExcel单元格合并处理器
*/
public class ExcelMergeHandler2 implements CellWriteHandler {

    private int[] mergeColumnIndex;
    private int mergeRowIndex;
    private int groupColumnIndex;

    /**
* 构造函数
*
* @param mergeRowIndex    合并开始的行索引
* @param mergeColumnIndex 要合并的列索引数组
* @param groupColumnIndex 分组列索引数组
*/
    public ExcelMergeHandler2(int mergeRowIndex, int[] mergeColumnIndex, int groupColumnIndex) {
        this.mergeRowIndex = mergeRowIndex;
        this.mergeColumnIndex = mergeColumnIndex;
        this.groupColumnIndex = groupColumnIndex;
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 当前行索引
        int curRowIndex = cell.getRowIndex();
        // 当前列索引
        int curColIndex = cell.getColumnIndex();
        // 如果当前行大于合并开始行
        if (curRowIndex > mergeRowIndex) {
            // 当前列在需要合并的列中
            for (int columnIndex : mergeColumnIndex) {
                if (curColIndex == columnIndex) {
                    // 进行合并操作
                    mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
                    break;
                }
            }
        }
    }

    /**
* 当前单元格向上合并
*
* @param writeSheetHolder 当前工作表持有者
* @param cell             当前单元格
* @param curRowIndex      当前行索引
* @param curColIndex      当前列索引
*/
    private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {

        // 获取当前行的当前列的数据和上一行的当前列列数据,通过上一行数据是否相同进行合并
        Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
        // 获取前一个单元格的数据
        Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
        Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();

        // 判断当前单元格和前一个单元格的数据是否相同
        if (curData.equals(preData)) {
            // 检查分组列的数据是否相同
            boolean isSameGroup = false;
            // 为什么curGroupCell 会是空指针
            // 如果你使用了 @ExcelProperty 注解来映射 Java 对象到 Excel 列,
            //可以通过调整注解中的 index 参数来确保 groupColumnIndex 列优先填充。
            Cell curGroupCell = cell.getSheet().getRow(curRowIndex).getCell(groupColumnIndex);
            Cell preGroupCell = cell.getSheet().getRow(curRowIndex - 1).getCell(groupColumnIndex);

            if (curGroupCell != null && preGroupCell != null) {
                Object curGroupData = curGroupCell.getCellTypeEnum() == CellType.STRING ? curGroupCell.getStringCellValue() : curGroupCell.getNumericCellValue();
                Object preGroupData = preGroupCell.getCellTypeEnum() == CellType.STRING ? preGroupCell.getStringCellValue() : preGroupCell.getNumericCellValue();
                if (curGroupData.equals(preGroupData)) {
                    isSameGroup = true;
                }
            }
            if (isSameGroup) {
                // 获取工作表
                Sheet sheet = writeSheetHolder.getSheet();
                // 获取已合并的区域
                List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
                boolean isMerged = false;
                // 检查前一个单元格是否已经被合并
                for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
                    CellRangeAddress cellRangeAddr = mergeRegions.get(i);
                    // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
                    if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
                        sheet.removeMergedRegion(i);
                        cellRangeAddr.setLastRow(curRowIndex);
                        sheet.addMergedRegion(cellRangeAddr);
                        isMerged = true;
                    }
                }
                // 如果前一个单元格未被合并,则新增合并区域
                if (!isMerged) {
                    CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
                    sheet.addMergedRegion(cellRangeAddress);
                }
            }
        }
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    }
}
kuuvgm7e

kuuvgm7e1#

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel-core</artifactId>
        <version>4.0.1</version>
        <scope>compile</scope>
    </dependency>

相关问题