fix 导出Excel时设置合并列,指定合并列的第一行数据为null、空字符时导致后续列无法正常合并

This commit is contained in:
zhengyulun 2025-08-26 15:41:35 +08:00
parent c01ed34602
commit b345702705

View File

@ -73,12 +73,14 @@ public class CellMergeHandler {
Object val = ReflectUtils.invokeGetter(rowObj, field.getName()); Object val = ReflectUtils.invokeGetter(rowObj, field.getName());
int colNum = mergeFieldsIndex.get(j); int colNum = mergeFieldsIndex.get(j);
if (!map.containsKey(field)) { Object cellValue = map.containsKey(field) ? map.get(field).value() : null;
if (!map.containsKey(field) || (isEmpty(cellValue) && val != null)) {
map.put(field, new RepeatCell(val, i)); map.put(field, new RepeatCell(val, i));
} else { } else {
RepeatCell repeatCell = map.get(field); RepeatCell repeatCell = map.get(field);
Object cellValue = repeatCell.value(); cellValue = repeatCell.value();
if (cellValue == null || "".equals(cellValue)) { if (isEmpty(cellValue)) {
// 空值跳过不合并 // 空值跳过不合并
continue; continue;
} }
@ -110,6 +112,10 @@ public class CellMergeHandler {
return cellList; return cellList;
} }
private boolean isEmpty(Object value) {
return value == null || "".equals(value);
}
private boolean isMerge(List<?> list, int i, Field field) { private boolean isMerge(List<?> list, int i, Field field) {
boolean isMerge = true; boolean isMerge = true;
CellMerge cm = field.getAnnotation(CellMerge.class); CellMerge cm = field.getAnnotation(CellMerge.class);