From 3f1e5702a293f31c5b70255c888f8ebc68fe49be Mon Sep 17 00:00:00 2001 From: May <1742057357@qq.com> Date: 星期一, 27 十一月 2023 10:52:59 +0800 Subject: [PATCH] !454 添加excel多sheet页导出 * add 添加excel多sheet导出 --- ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 56 insertions(+), 0 deletions(-) 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 329911d..a6c14ad 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 @@ -270,6 +270,26 @@ } /** + * 澶歴heet妯℃澘瀵煎嚭 妯℃澘鏍煎紡涓� {key.灞炴� + * + * @param filename 鏂囦欢鍚� + * @param templatePath 妯℃澘璺緞 resource 鐩綍涓嬬殑璺緞鍖呮嫭妯℃澘鏂囦欢鍚� + * 渚嬪: excel/temp.xlsx + * 閲嶇偣: 妯℃澘鏂囦欢蹇呴』鏀剧疆鍒板惎鍔ㄧ被瀵瑰簲鐨� resource 鐩綍涓� + * @param data 妯℃澘闇�瑕佺殑鏁版嵁 + * @param response 鍝嶅簲浣� + */ + public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String filename, String templatePath, HttpServletResponse response) { + try { + resetResponse(filename, response); + ServletOutputStream os = response.getOutputStream(); + exportTemplateMultiSheet(data, templatePath, os); + } catch (IOException e) { + throw new RuntimeException("瀵煎嚭Excel寮傚父"); + } + } + + /** * 澶氳〃澶氭暟鎹ā鏉垮鍑� 妯℃澘鏍煎紡涓� {key.灞炴� * * @param templatePath 妯℃澘璺緞 resource 鐩綍涓嬬殑璺緞鍖呮嫭妯℃澘鏂囦欢鍚� @@ -304,6 +324,42 @@ } /** + * 澶歴heet妯℃澘瀵煎嚭 妯℃澘鏍煎紡涓� {key.灞炴� + * + * @param templatePath 妯℃澘璺緞 resource 鐩綍涓嬬殑璺緞鍖呮嫭妯℃澘鏂囦欢鍚� + * 渚嬪: excel/temp.xlsx + * 閲嶇偣: 妯℃澘鏂囦欢蹇呴』鏀剧疆鍒板惎鍔ㄧ被瀵瑰簲鐨� resource 鐩綍涓� + * @param data 妯℃澘闇�瑕佺殑鏁版嵁 + * @param os 杈撳嚭娴� + */ + public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) { + ClassPathResource templateResource = new ClassPathResource(templatePath); + ExcelWriter excelWriter = EasyExcel.write(os) + .withTemplate(templateResource.getStream()) + .autoCloseStream(false) + // 澶ф暟鍊艰嚜鍔ㄨ浆鎹� 闃叉澶辩湡 + .registerConverter(new ExcelBigNumberConvert()) + .build(); + if (CollUtil.isEmpty(data)) { + throw new IllegalArgumentException("鏁版嵁涓虹┖"); + } + for (int i = 0; i < data.size(); i++) { + WriteSheet writeSheet = EasyExcel.writerSheet(i).build(); + for (Map.Entry<String, Object> map : data.get(i).entrySet()) { + // 璁剧疆鍒楄〃鍚庣画杩樻湁鏁版嵁 + FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); + if (map.getValue() instanceof Collection) { + // 澶氳〃瀵煎嚭蹇呴』浣跨敤 FillWrapper + excelWriter.fill(new FillWrapper(map.getKey(), (Collection<?>) map.getValue()), fillConfig, writeSheet); + } else { + excelWriter.fill(map.getValue(), writeSheet); + } + } + } + excelWriter.finish(); + } + + /** * 閲嶇疆鍝嶅簲浣� */ private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { -- Gitblit v1.9.3