From f82ea7942a1d423c6acd2d35c46012356c233f77 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 9 Mar 2026 15:04:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Excel=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=A0=BC=E5=BC=8F=E6=A0=B7=E5=BC=8F=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/poi/ExcelUtil.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 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 e577634865..7fb2c65a1a 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 @@ -106,6 +106,11 @@ public class ExcelUtil */ public Map sysDictMap = new HashMap(); + /** + * 单元格样式缓存 + */ + private Map cellStyleCache = new HashMap(); + /** * Excel sheet最大行数,默认65536 */ @@ -418,7 +423,7 @@ public class ExcelUtil Object val = this.getCellValue(row, entry.getKey()); // 如果不存在实例则新建. - entity = (entity == null ? clazz.getDeclaredConstructor().newInstance() : entity); + entity = (entity == null ? clazz.newInstance() : entity); // 从map中得到对应列的field. Field field = (Field) entry.getValue()[0]; Excel attr = (Excel) entry.getValue()[1]; @@ -1125,7 +1130,7 @@ public class ExcelUtil * 添加单元格 */ @SuppressWarnings("deprecation") - public Cell addCell(Excel attr, Row row, T vo, Field field, int column) + public Cell addCell(Excel attr, Row row, T vo, Field field, int column) { Cell cell = null; try @@ -1202,9 +1207,16 @@ public class ExcelUtil */ private CellStyle createCellStyle(CellStyle cellStyle, String format) { + String key = cellStyle.getIndex() + "|" + format; + CellStyle cached = cellStyleCache.get(key); + if (cached != null) + { + return cached; + } CellStyle style = wb.createCellStyle(); style.cloneStyleFrom(cellStyle); style.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat(format)); + cellStyleCache.put(key, style); return style; } @@ -1426,7 +1438,7 @@ public class ExcelUtil { try { - Object instance = excel.handler().getDeclaredConstructor().newInstance(); + Object instance = excel.handler().newInstance(); Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class }); value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb); }