From 670947258fa02814eee803fab9bd8cbac30d0820 Mon Sep 17 00:00:00 2001 From: "yysforget@163.com" Date: Thu, 28 Aug 2025 11:20:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Excel=E5=AF=BC=E5=87=BA=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E6=94=AF=E6=8C=81=E5=AF=BC=E5=87=BA=E7=B1=BB=E4=B8=AD?= =?UTF-8?q?=E5=90=AB=E6=9C=89=E5=A4=9A=E4=B8=AA=E5=AD=90=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/poi/ExcelUtil.java | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 5131908f69..8f3a0489de 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -85,6 +85,7 @@ import com.ruoyi.common.utils.file.FileTypeUtils; import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.common.utils.file.ImageUtils; import com.ruoyi.common.utils.reflect.ReflectUtils; +import org.springframework.util.CollectionUtils; /** * Excel相关处理 @@ -174,12 +175,12 @@ public class ExcelUtil /** * 对象的子列表方法 */ - private Method subMethod; + private Map subMethods = new HashMap<>(); /** * 对象的子列表属性 */ - private List subFields; + private Map> subFieldsMap = new HashMap<>(); /** * 统计列表 @@ -252,7 +253,7 @@ public class ExcelUtil int titleLastCol = this.fields.size() - 1; if (isSubList()) { - titleLastCol = titleLastCol + subFields.size() - 1; + titleLastCol = titleLastCol + subFieldsMap.values().size() - 1; } Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0); titleRow.setHeightInPoints(30); @@ -272,16 +273,18 @@ public class ExcelUtil { Row subRow = sheet.createRow(rownum); int column = 0; - int subFieldSize = subFields != null ? subFields.size() : 0; for (Object[] objects : fields) { Field field = (Field) objects[0]; Excel attr = (Excel) objects[1]; + CellStyle cellStyle = styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())); if (Collection.class.isAssignableFrom(field.getType())) { Cell cell = subRow.createCell(column); cell.setCellValue(attr.name()); - cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); + cell.setCellStyle(cellStyle); + + int subFieldSize = subFieldsMap != null ? subFieldsMap.get(field.getName()).size() : 0; if (subFieldSize > 1) { CellRangeAddress cellAddress = new CellRangeAddress(rownum, rownum, column, column + subFieldSize - 1); @@ -293,7 +296,7 @@ public class ExcelUtil { Cell cell = subRow.createCell(column++); cell.setCellValue(attr.name()); - cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); + cell.setCellStyle(cellStyle); } } rownum++; @@ -697,7 +700,8 @@ public class ExcelUtil Excel excel = (Excel) os[1]; if (Collection.class.isAssignableFrom(field.getType())) { - for (Field subField : subFields) + List currentSubFields = subFieldsMap.get(field.getName()); + for (Field subField : currentSubFields) { Excel subExcel = subField.getAnnotation(Excel.class); this.createHeadCell(subExcel, row, column++); @@ -747,6 +751,7 @@ public class ExcelUtil if (subList != null && !subList.isEmpty()) { int subIndex = 0; + List currentSubFields = subFieldsMap.get(field.getName()); for (Object subVo : subList) { Row subRow = sheet.getRow(currentRowNum + subIndex); @@ -756,14 +761,14 @@ public class ExcelUtil } int subColumn = column; - for (Field subField : subFields) + for (Field subField : currentSubFields) { Excel subExcel = subField.getAnnotation(Excel.class); addCell(subExcel, subRow, (T) subVo, subField, subColumn++); } subIndex++; } - column += subFields.size(); + column += currentSubFields.size(); } } catch (Exception e) @@ -1131,7 +1136,7 @@ public class ExcelUtil { // 创建cell cell = row.createCell(column); - if (isSubListValue(vo) && getListCellValue(vo).size() > 1 && attr.needMerge()) + if (isSubListValue(vo) && getListCellValue(vo) > 1 && attr.needMerge()) { if (subMergedLastRowNum >= subMergedFirstRowNum) { @@ -1584,10 +1589,11 @@ public class ExcelUtil } if (Collection.class.isAssignableFrom(field.getType())) { - subMethod = getSubMethod(field.getName(), clazz); + String fieldName = field.getName(); + subMethods.put(fieldName, getSubMethod(fieldName, clazz)); ParameterizedType pt = (ParameterizedType) field.getGenericType(); Class subClass = (Class) pt.getActualTypeArguments()[0]; - this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class); + subFieldsMap.put(fieldName, FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class)); } } @@ -1839,7 +1845,7 @@ public class ExcelUtil */ public boolean isSubList() { - return StringUtils.isNotNull(subFields) && subFields.size() > 0; + return !CollectionUtils.isEmpty(subFieldsMap); } /** @@ -1847,24 +1853,30 @@ public class ExcelUtil */ public boolean isSubListValue(T vo) { - return StringUtils.isNotNull(subFields) && subFields.size() > 0 && StringUtils.isNotNull(getListCellValue(vo)) && getListCellValue(vo).size() > 0; + return !CollectionUtils.isEmpty(subFieldsMap) && getListCellValue(vo) > 0; } /** * 获取集合的值 */ - public Collection getListCellValue(Object obj) + public int getListCellValue(Object obj) { - Object value; + Collection value; + int max = 0; try { - value = subMethod.invoke(obj, new Object[] {}); + for (String s : subMethods.keySet()) { + value = (Collection )subMethods.get(s).invoke(obj); + if (value.size() > max) { + max = value.size(); + } + } } catch (Exception e) { - return new ArrayList(); + return 0; } - return (Collection) value; + return max; } /** -- Gitee