From 48ee686ade2d855a4943027d09feb2a2722f0271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Wed, 10 May 2023 18:34:01 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=20CellMergeStrategy=20=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E7=BA=A7=E8=A1=A8=E5=A4=B4=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=B0=8F=E9=97=AE=E9=A2=98=20=E6=95=B4=E7=90=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/excel/core/CellMergeStrategy.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 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 a40937240..71a5703ae 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,19 +1,20 @@ package org.dromara.common.excel.core; +import cn.hutool.core.collection.CollUtil; +import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.write.merge.AbstractMergeStrategy; -import org.dromara.common.excel.annotation.CellMerge; import lombok.AllArgsConstructor; import lombok.Data; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; +import org.dromara.common.core.utils.reflect.ReflectUtils; +import org.dromara.common.excel.annotation.CellMerge; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -24,20 +25,27 @@ import java.util.Map; * * @author Lion Li */ -@AllArgsConstructor @Slf4j public class CellMergeStrategy extends AbstractMergeStrategy { - private List list; - private boolean hasTitle; + private final List list; + private final boolean hasTitle; + private int rowIndex; - @Override + public CellMergeStrategy(List list, boolean hasTitle) { + this.list = list; + this.hasTitle = hasTitle; + // 行合并开始下标 + this.rowIndex = hasTitle ? 1 : 0; + } + + @Override protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) { List cellList = handle(list, hasTitle); // judge the list is not null - if (CollectionUtils.isNotEmpty(cellList)) { + if (CollUtil.isNotEmpty(cellList)) { // the judge is necessary - if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 0) { + if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == 0) { for (CellRangeAddress item : cellList) { sheet.addMergedRegion(item); } @@ -46,13 +54,13 @@ public class CellMergeStrategy extends AbstractMergeStrategy { } @SneakyThrows - private static List handle(List list, boolean hasTitle) { + private List handle(List list, boolean hasTitle) { List cellList = new ArrayList<>(); - if (CollectionUtils.isEmpty(list)) { + if (CollUtil.isEmpty(list)) { return cellList; } - Class clazz = list.get(0).getClass(); - Field[] fields = clazz.getDeclaredFields(); + Field[] fields = ReflectUtils.getFields(list.get(0).getClass(), field -> !"serialVersionUID".equals(field.getName())); + // 有注解的字段 List mergeFields = new ArrayList<>(); List mergeFieldsIndex = new ArrayList<>(); @@ -60,21 +68,21 @@ public class CellMergeStrategy extends AbstractMergeStrategy { Field field = fields[i]; if (field.isAnnotationPresent(CellMerge.class)) { CellMerge cm = field.getAnnotation(CellMerge.class); - mergeFields.add(field); - mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index()); + mergeFields.add(field); + mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index()); + if (hasTitle) { + ExcelProperty property = field.getAnnotation(ExcelProperty.class); + rowIndex = Math.max(rowIndex, property.value().length); + } } } - // 行合并开始下标 - int rowIndex = hasTitle ? 1 : 0; + Map map = new HashMap<>(); // 生成两两合并单元格 for (int i = 0; i < list.size(); i++) { for (int j = 0; j < mergeFields.size(); j++) { Field field = mergeFields.get(j); - String name = field.getName(); - String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); - Method readMethod = clazz.getMethod(methodName); - Object val = readMethod.invoke(list.get(i)); + Object val = ReflectUtils.invokeGetter(list.get(i), field.getName()); int colNum = mergeFieldsIndex.get(j); if (!map.containsKey(field)) {