|
@@ -150,32 +150,38 @@ public class ExcelFillCellMergeStrategy implements CellWriteHandler {
|
|
|
* @param curColIndex 当前列
|
|
|
*/
|
|
|
private void mergeWithPrevCol(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
|
|
|
- //获取当前行的当前列的数据和上一列的当前行行数据,通过上一列数据是否相同进行合并
|
|
|
- final Object curData = cell.getCellType() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
|
|
|
- final Cell preCell = cell.getSheet().getRow(curRowIndex).getCell(curColIndex - 1);
|
|
|
- final Object preData = preCell.getCellType() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
|
|
|
-
|
|
|
- // 比较当前行的单元格与上一列是否相同,相同合并当前单元格与上一列
|
|
|
- if (curData.equals(preData)) {
|
|
|
- final Sheet sheet = writeSheetHolder.getSheet();
|
|
|
- final List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
|
|
|
- boolean isMerged = false;
|
|
|
- for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
|
|
|
- final CellRangeAddress cellRangeAddr = mergeRegions.get(i);
|
|
|
- // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
|
|
|
- if (cellRangeAddr.isInRange(curRowIndex, curColIndex - 1)) {
|
|
|
- sheet.removeMergedRegion(i);
|
|
|
- cellRangeAddr.setLastColumn(curColIndex);
|
|
|
- sheet.addMergedRegion(cellRangeAddr);
|
|
|
- isMerged = true;
|
|
|
+ try {
|
|
|
+ //获取当前行的当前列的数据和上一列的当前行行数据,通过上一列数据是否相同进行合并
|
|
|
+ final Object curData = cell.getCellType() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
|
|
|
+ final Cell preCell = cell.getSheet().getRow(curRowIndex).getCell(curColIndex - 1);
|
|
|
+ final Object preData = preCell.getCellType() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
|
|
|
+
|
|
|
+ // 比较当前行的单元格与上一列是否相同,相同合并当前单元格与上一列
|
|
|
+ if (curData.equals(preData)) {
|
|
|
+ final Sheet sheet = writeSheetHolder.getSheet();
|
|
|
+ final List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
|
|
|
+ boolean isMerged = false;
|
|
|
+ for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
|
|
|
+ final CellRangeAddress cellRangeAddr = mergeRegions.get(i);
|
|
|
+ // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
|
|
|
+ if (cellRangeAddr.isInRange(curRowIndex, curColIndex - 1)) {
|
|
|
+ sheet.removeMergedRegion(i);
|
|
|
+ cellRangeAddr.setLastColumn(curColIndex);
|
|
|
+ sheet.addMergedRegion(cellRangeAddr);
|
|
|
+ isMerged = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 若上一个单元格未被合并,则新增合并单元
|
|
|
+ if (!isMerged) {
|
|
|
+ final CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex, curRowIndex, curColIndex - 1, curColIndex);
|
|
|
+ sheet.addMergedRegion(cellRangeAddress);
|
|
|
}
|
|
|
}
|
|
|
- // 若上一个单元格未被合并,则新增合并单元
|
|
|
- if (!isMerged) {
|
|
|
- final CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex, curRowIndex, curColIndex - 1, curColIndex);
|
|
|
- sheet.addMergedRegion(cellRangeAddress);
|
|
|
- }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("合并单元格error:",e);
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|