From 316117524d12bfb5b6154488d87c0cb6d82a770c Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 9 Mar 2026 15:14:51 +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 --- .../ruoyi/common/core/utils/poi/ExcelUtil.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java index 71a0e2381..2d03cc835 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java @@ -79,6 +79,11 @@ public class ExcelUtil public static final String[] FORMULA_STR = { "=", "-", "+", "@" }; + /** + * 单元格样式缓存 + */ + private Map cellStyleCache = new HashMap(); + /** * Excel sheet最大行数,默认65536 */ @@ -976,7 +981,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 @@ -1043,9 +1048,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; } @@ -1241,7 +1253,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); }