|
|
@@ -43,6 +43,9 @@ public class ExcelStyleHandler implements CellWriteHandler {
|
|
|
/** 是否已经执行过合并 */
|
|
|
private boolean mergeCompleted = false;
|
|
|
|
|
|
+ /** 预计算的需要清空的单元格集合,key格式为 "相对行索引_列索引" */
|
|
|
+ private Set<String> blankCellSet;
|
|
|
+
|
|
|
public ExcelStyleHandler(int templateRowIndex) {
|
|
|
this.templateRowIndex = templateRowIndex;
|
|
|
}
|
|
|
@@ -51,6 +54,8 @@ public class ExcelStyleHandler implements CellWriteHandler {
|
|
|
this.visitorList = visitorList;
|
|
|
this.fileType = fileType;
|
|
|
this.totalRows = visitorList != null ? visitorList.size() : 0;
|
|
|
+ // 预计算需要清空的单元格(合并区域中非首行的单元格)
|
|
|
+ this.blankCellSet = computeBlankCells();
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -201,6 +206,16 @@ public class ExcelStyleHandler implements CellWriteHandler {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 在单元格写入后,立即清空需要合并的非首行单元格的值
|
|
|
+ Cell cell = context.getCell();
|
|
|
+ int currentRowIndex = cell.getRow().getRowNum();
|
|
|
+ int colIndex = cell.getColumnIndex();
|
|
|
+ int relativeRow = currentRowIndex - DATA_START_ROW;
|
|
|
+ if (relativeRow >= 0 && blankCellSet != null
|
|
|
+ && blankCellSet.contains(relativeRow + "_" + colIndex)) {
|
|
|
+ cell.setCellValue("");
|
|
|
+ }
|
|
|
+
|
|
|
// 统计处理的单元格数量
|
|
|
processedCellCount++;
|
|
|
|
|
|
@@ -288,6 +303,50 @@ public class ExcelStyleHandler implements CellWriteHandler {
|
|
|
return s != null ? s : "";
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 预计算合并区域中需要清空的单元格
|
|
|
+ * 合并后只有首行需要保留数据,其余行设为空字符串
|
|
|
+ * @return 需要清空的单元格集合,key格式为 "相对行索引_列索引"
|
|
|
+ */
|
|
|
+ private Set<String> computeBlankCells() {
|
|
|
+ Set<String> blankSet = new HashSet<>();
|
|
|
+ if (visitorList == null || visitorList.size() <= 1) {
|
|
|
+ return blankSet;
|
|
|
+ }
|
|
|
+
|
|
|
+ int n = visitorList.size();
|
|
|
+ Map<Integer, List<int[]>> mergeRanges = new HashMap<>();
|
|
|
+
|
|
|
+ // 预定义每种类型需要合并的列(与 performMerge 中保持一致)
|
|
|
+ List<Integer> sourceCols = Collections.singletonList(0);
|
|
|
+ List<Integer> orderCols = Arrays.asList(1, 2, 3, 4, 5, 6,
|
|
|
+ fileType == 1 ? 7 : -1, fileType == 1 ? 8 : -1).stream()
|
|
|
+ .filter(c -> c >= 0).collect(Collectors.toList());
|
|
|
+ List<Integer> roomCols = (fileType == 1
|
|
|
+ ? Arrays.asList(9, 10, 11)
|
|
|
+ : Arrays.asList(7, 8, 9));
|
|
|
+
|
|
|
+ for (int col : sourceCols) collectMergeRanges(mergeRanges, col, n,
|
|
|
+ i -> safeString(visitorList.get(i).getSourceName()));
|
|
|
+ for (int col : orderCols) collectMergeRanges(mergeRanges, col, n,
|
|
|
+ i -> safeString(visitorList.get(i).getOrderNo()));
|
|
|
+ for (int col : roomCols) collectMergeRanges(mergeRanges, col, n,
|
|
|
+ i -> safeString(visitorList.get(i).getRoomIndexId()) + "_" + safeString(visitorList.get(i).getOrderNo()));
|
|
|
+
|
|
|
+ // 将合并区域中非首行的单元格加入清空集合
|
|
|
+ for (Map.Entry<Integer, List<int[]>> entry : mergeRanges.entrySet()) {
|
|
|
+ int col = entry.getKey();
|
|
|
+ for (int[] range : entry.getValue()) {
|
|
|
+ // range[0]+1 到 range[1] 是非首行,需要清空
|
|
|
+ for (int rowIdx = range[0] + 1; rowIdx <= range[1]; rowIdx++) {
|
|
|
+ blankSet.add(rowIdx + "_" + col);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return blankSet;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 废弃
|