[alibaba/easyexcel]使用自定义策略合并单元格时,2k条数据需要耗时10s。且cpu使用率过高。

2024-05-09 956 views
0

问题描述 使用自定义策略合并单元格时,2k条数据需要耗时10s。且cpu使用率过高。easyexcel版本:3.0.0-beta3 自定义策略代码

 public class CellMergeStrategy implements CellWriteHandler {

    /**
     * 合并字段的下标
     */
    private int[] mergeColumnIndex;
    /**
     * 合并几行
     */
    private int mergeRowIndex;

    public CellMergeStrategy() {
    }

    public CellMergeStrategy(int mergeRowIndex, int[] mergeColumnIndex) {
        this.mergeRowIndex = mergeRowIndex;
        this.mergeColumnIndex = mergeColumnIndex;
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        //当前行
        int curRowIndex = cell.getRowIndex();
        //当前列
        int curColIndex = cell.getColumnIndex();

        if (curRowIndex > mergeRowIndex) {
            for (int columnIndex : mergeColumnIndex) {
                if (curColIndex == columnIndex) {
                    mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
                    break;
                }
            }
        }
    }
    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();

        // 比较当前行的第一列的单元格与上一行是否相同,相同合并当前单元格与上一行
        Cell groupIdCell = cell.getSheet().getRow(curRowIndex).getCell(0);
        Object groupData = groupIdCell.getCellTypeEnum() == CellType.STRING ? groupIdCell.getStringCellValue() :
                groupIdCell.getNumericCellValue();
        Cell groupIdPreCell = cell.getSheet().getRow(curRowIndex - 1).getCell(0);
        Object groupPreData = groupIdPreCell.getCellTypeEnum() == CellType.STRING ? groupIdPreCell.getStringCellValue() :
                groupIdPreCell.getNumericCellValue();
        if (curData.equals(preData)&& Objects.equals(groupData,groupPreData)) {
            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.addMergedRegionUnsafe(cellRangeAddr);
                    isMerged = true;
                }
            }
            // 若上一个单元格未被合并,则新增合并单元
            if (!isMerged) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex,
                        curColIndex);
                sheet.addMergedRegionUnsafe(cellRangeAddress);
            }
        }
    }
}

异常提示 结束任务后,却依旧发现cpu占用率高,排查发现。 "http-nio-9093-exec-18" #475 daemon prio=5 os_prio=0 tid=0x00007f5e9000a000 nid=0x46f0 runnable [0x00007f5e19c90000] java.lang.Thread.State: RUNNABLE at org.apache.poi.ss.util.CellReference.separateRefParts(CellReference.java:404) at org.apache.poi.ss.util.CellReference.(CellReference.java:114) at org.apache.poi.ss.util.CellRangeAddress.valueOf(CellRangeAddress.java:133) at org.apache.poi.xssf.usermodel.XSSFSheet.getMergedRegions(XSSFSheet.java:1348) at org.apache.poi.xssf.streaming.SXSSFSheet.getMergedRegions(SXSSFSheet.java:508) at com.tbit.uqzc.client.common.strategy.CellMergeStrategy.mergeWithPrevRow(CellMergeStrategy.java:77) "http-nio-9093-exec-18" #475 daemon prio=5 os_prio=0 tid=0x00007f5e9000a000 nid=0x46f0 runnable [0x00007f5e19c8f000] java.lang.Thread.State: RUNNABLE at java.util.regex.Pattern$CharProperty.match(Pattern.java:3790) at java.util.regex.Pattern$Curly.match0(Pattern.java:4274) at java.util.regex.Pattern$Curly.match(Pattern.java:4248) at java.util.regex.Pattern$Ques.match(Pattern.java:4196) at java.util.regex.Pattern$GroupHead.match(Pattern.java:4672) at java.util.regex.Pattern$Branch.match(Pattern.java:4618) at java.util.regex.Pattern$BranchConn.match(Pattern.java:4582) at java.util.regex.Pattern$GroupTail.match(Pattern.java:4731) at java.util.regex.Pattern$Curly.match0(Pattern.java:4293) at java.util.regex.Pattern$Curly.match(Pattern.java:4248) at java.util.regex.Pattern$Ques.match(Pattern.java:4196) at java.util.regex.Pattern$GroupHead.match(Pattern.java:4672) at java.util.regex.Pattern$Branch.match(Pattern.java:4618) at java.util.regex.Matcher.match(Matcher.java:1270) at java.util.regex.Matcher.matches(Matcher.java:604) at org.apache.poi.ss.util.CellReference.separateRefParts(CellReference.java:398) at org.apache.poi.ss.util.CellReference.(CellReference.java:114) at org.apache.poi.ss.util.CellRangeAddress.valueOf(CellRangeAddress.java:133)

请问这种有无解决方案。望各位指教。

回答

0

我也遇到了这个问题,导出500条数据,需要5分钟,哭了

3

我也遇到了这个问题,导出500条数据,需要5分钟,哭了

有解决方案,请分享给我,万分感谢。

6

求解决方案