问题描述 使用自定义策略合并单元格时,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.
请问这种有无解决方案。望各位指教。