From 82fdb37c6b54fe591b04fea23b46a997921015fc Mon Sep 17 00:00:00 2001
From: AprilWind <2100166581@qq.com>
Date: 星期五, 19 七月 2024 16:50:40 +0800
Subject: [PATCH] fix 修复判断表名为空错误
---
ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java | 82 +++++++++++++++++++++++------------------
1 files changed, 46 insertions(+), 36 deletions(-)
diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java
index bcb5be7..7c0a48b 100644
--- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java
+++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java
@@ -1,8 +1,12 @@
package org.dromara.common.excel.core;
import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ReflectUtil;
+import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.write.handler.WorkbookWriteHandler;
+import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -15,10 +19,7 @@
import org.dromara.common.excel.annotation.CellMerge;
import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
* 鍒楀�奸噸澶嶅悎骞剁瓥鐣�
@@ -26,7 +27,7 @@
* @author Lion Li
*/
@Slf4j
-public class CellMergeStrategy extends AbstractMergeStrategy {
+public class CellMergeStrategy extends AbstractMergeStrategy implements WorkbookWriteHandler {
private final List<CellRangeAddress> cellList;
private final boolean hasTitle;
@@ -41,13 +42,24 @@
@Override
protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
- // judge the list is not null
- if (CollUtil.isNotEmpty(cellList)) {
- // the judge is necessary
- if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == 0) {
- for (CellRangeAddress item : cellList) {
- sheet.addMergedRegion(item);
+ //鍗曞厓鏍煎啓鍏ヤ簡,閬嶅巻鍚堝苟鍖哄煙,濡傛灉璇ell鍦ㄥ尯鍩熷唴,浣嗛潪棣栬,鍒欐竻绌�
+ final int rowIndex = cell.getRowIndex();
+ if (CollUtil.isNotEmpty(cellList)){
+ for (CellRangeAddress cellAddresses : cellList) {
+ final int firstRow = cellAddresses.getFirstRow();
+ if (cellAddresses.isInRange(cell) && rowIndex != firstRow){
+ cell.setBlank();
}
+ }
+ }
+ }
+
+ @Override
+ public void afterWorkbookDispose(final WorkbookWriteHandlerContext context) {
+ //褰撳墠琛ㄦ牸鍐欏畬鍚庯紝缁熶竴鍐欏叆
+ if (CollUtil.isNotEmpty(cellList)){
+ for (CellRangeAddress item : cellList) {
+ context.getWriteContext().writeSheetHolder().getSheet().addMergedRegion(item);
}
}
}
@@ -93,35 +105,15 @@
// 绌哄�艰烦杩囦笉鍚堝苟
continue;
}
+
if (!cellValue.equals(val)) {
- if (i - repeatCell.getCurrent() > 1) {
+ if ((i - repeatCell.getCurrent() > 1) && isMerge(list, i, field)) {
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
}
map.put(field, new RepeatCell(val, i));
- } else if (j == 0) {
- if (i == list.size() - 1) {
- if (i > repeatCell.getCurrent()) {
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
- }
- }
- } else {
- // 鍒ゆ柇鍓嶉潰鐨勬槸鍚﹀悎骞朵簡
- RepeatCell firstCell = map.get(mergeFields.get(0));
- if (repeatCell.getCurrent() != firstCell.getCurrent()) {
- if (i == list.size() - 1) {
- if (i > repeatCell.getCurrent()) {
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
- }
- } else if (repeatCell.getCurrent() < firstCell.getCurrent()) {
- if (i - repeatCell.getCurrent() > 1) {
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
- }
- map.put(field, new RepeatCell(val, i));
- }
- } else if (i == list.size() - 1) {
- if (i > repeatCell.getCurrent()) {
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
- }
+ } else if (i == list.size() - 1) {
+ if (i > repeatCell.getCurrent() && isMerge(list, i, field)) {
+ cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
}
}
}
@@ -130,6 +122,24 @@
return cellList;
}
+ private boolean isMerge(List<?> list, int i, Field field) {
+ boolean isMerge = true;
+ CellMerge cm = field.getAnnotation(CellMerge.class);
+ final String[] mergeBy = cm.mergeBy();
+ if (StrUtil.isAllNotBlank(mergeBy)) {
+ //姣斿褰撳墠list(i)鍜宭ist(i - 1)鐨勫悇涓睘鎬у�间竴涓�姣斿 濡傛灉鍏ㄤ负鐪� 鍒欎负鐪�
+ for (String fieldName : mergeBy) {
+ final Object valCurrent = ReflectUtil.getFieldValue(list.get(i), fieldName);
+ final Object valPre = ReflectUtil.getFieldValue(list.get(i - 1), fieldName);
+ if (!Objects.equals(valPre, valCurrent)) {
+ //渚濊禆瀛楁濡傛湁浠讳竴涓嶇瓑鍊�,鍒欐爣璁颁负涓嶅彲鍚堝苟
+ isMerge = false;
+ }
+ }
+ }
+ return isMerge;
+ }
+
@Data
@AllArgsConstructor
static class RepeatCell {
--
Gitblit v1.9.3