From fca941865712e42a9985479db06c77c31bcd8457 Mon Sep 17 00:00:00 2001 From: "zangch@mesnac.com" Date: Mon, 24 Nov 2025 10:46:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(excel):=20=E6=96=B0=E5=A2=9E=E6=B7=B7?= =?UTF-8?q?=E5=90=88=E6=A8=A1=E5=BC=8F=E6=A8=A1=E6=9D=BF=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 支持 Map 对象的原地填充占位符 - 支持 List 列表的展开填充新行 - 提供基于 HttpServletResponse 的导出接口 - 支持自定义模板路径和输出流导出 - 集成大数字类型转换器防止数据丢失 - 优化导出流程,支持多种数据混合填充 --- .../dromara/common/excel/utils/ExcelUtil.java | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java index 74dbccba..181e7df1 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java @@ -3,8 +3,8 @@ package org.dromara.common.excel.utils; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.resource.ClassPathResource; import cn.hutool.core.util.IdUtil; -import cn.idev.excel.FastExcel; import cn.idev.excel.ExcelWriter; +import cn.idev.excel.FastExcel; import cn.idev.excel.write.builder.ExcelWriterSheetBuilder; import cn.idev.excel.write.metadata.WriteSheet; import cn.idev.excel.write.metadata.fill.FillConfig; @@ -476,4 +476,58 @@ public class ExcelUtil { return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx"; } + + /** + * 混合模式模板导出 (支持 Map 原地填充 + List 列表填充) + * + * @param data 模板需要的数据 + * @param filename 文件名 + * @param templatePath 模板路径 + * @param response 响应体 + */ + public static void exportTemplateMixed(Map data, String filename, String templatePath, HttpServletResponse response) { + try { + resetResponse(filename, response); + ServletOutputStream os = response.getOutputStream(); + exportTemplateMixed(data, templatePath, os); + } catch (IOException e) { + throw new RuntimeException("导出Excel异常"); + } + } + + /** + * 混合模式模板导出 (支持 Map 原地填充 + List 列表填充) + * Map/Object -> forceNewRow=false (原地替换占位符) + * Collection -> forceNewRow=true (展开列表) + * + * @param data 模板需要的数据 + * @param templatePath 模板路径 + * @param os 输出流 + */ + public static void exportTemplateMixed(Map data, String templatePath, OutputStream os) { + ClassPathResource templateResource = new ClassPathResource(templatePath); + ExcelWriter excelWriter = FastExcel.write(os) + .withTemplate(templateResource.getStream()) + .autoCloseStream(false) + .registerConverter(new ExcelBigNumberConvert()) + .build(); + WriteSheet writeSheet = FastExcel.writerSheet().build(); + + for (Map.Entry entry : data.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + boolean isCollection = value instanceof Collection; + + // 智能判断:只有集合才需要展开新行,普通数据原地替换 + FillConfig fillConfig = FillConfig.builder().forceNewRow(isCollection).build(); + + if (isCollection) { + excelWriter.fill(new FillWrapper(key, (Collection) value), fillConfig, writeSheet); + } else { + excelWriter.fill(value, fillConfig, writeSheet); + } + } + excelWriter.finish(); + } + }