Ver código fonte

!454 添加excel多sheet页导出
* add 添加excel多sheet导出

May 1 ano atrás
pai
commit
3f1e5702a2

+ 56 - 0
ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java

@@ -269,6 +269,26 @@ public class ExcelUtil {
         }
     }
 
+    /**
+     * 多sheet模板导出 模板格式为 {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.属性}
      *
@@ -303,6 +323,42 @@ public class ExcelUtil {
         excelWriter.finish();
     }
 
+    /**
+     * 多sheet模板导出 模板格式为 {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();
+    }
+
     /**
      * 重置响应体
      */

+ 35 - 0
ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/TestExcelController.java

@@ -94,6 +94,41 @@ public class TestExcelController {
         exportExcelService.exportWithOptions(response);
     }
 
+    /**
+     * 多个sheet导出
+     */
+    @GetMapping("/exportTemplateMultiSheet")
+    public void exportTemplateMultiSheet(HttpServletResponse response) {
+        List<TestObj1> list1 = new ArrayList<>();
+        list1.add(new TestObj1("list1测试1", "list1测试2", "list1测试3"));
+        list1.add(new TestObj1("list1测试4", "list1测试5", "list1测试6"));
+        List<TestObj1> list2 = new ArrayList<>();
+        list2.add(new TestObj1("list2测试1", "list2测试2", "list2测试3"));
+        list2.add(new TestObj1("list2测试4", "list2测试5", "list2测试6"));
+        List<TestObj1> list3 = new ArrayList<>();
+        list3.add(new TestObj1("list3测试1", "list3测试2", "list3测试3"));
+        list3.add(new TestObj1("list3测试4", "list3测试5", "list3测试6"));
+        List<TestObj1> list4 = new ArrayList<>();
+        list4.add(new TestObj1("list4测试1", "list4测试2", "list4测试3"));
+        list4.add(new TestObj1("list4测试4", "list4测试5", "list4测试6"));
+
+        List<Map<String, Object>> list = new ArrayList<>();
+        Map<String, Object> sheetMap1 = new HashMap<>();
+        sheetMap1.put("data1", list1);
+        Map<String, Object> sheetMap2 = new HashMap<>();
+        sheetMap2.put("data2", list2);
+        Map<String, Object> sheetMap3 = new HashMap<>();
+        sheetMap3.put("data3", list3);
+        Map<String, Object> sheetMap4 = new HashMap<>();
+        sheetMap4.put("data4", list4);
+
+        list.add(sheetMap1);
+        list.add(sheetMap2);
+        list.add(sheetMap3);
+        list.add(sheetMap4);
+        ExcelUtil.exportTemplateMultiSheet(list, "多sheet列表", "excel/多sheet列表.xlsx", response);
+    }
+
     /**
      * 导入表格
      */

BIN
ruoyi-modules/ruoyi-demo/src/main/resources/excel/多sheet列表.xlsx