From b6b0f9c47d43e41792db7ef0c3405fd15ac901bb Mon Sep 17 00:00:00 2001
From: AprilWind <2100166581@qq.com>
Date: 星期五, 19 七月 2024 15:54:37 +0800
Subject: [PATCH] update 优化代码生成表名查询数据库
---
ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java | 62 +++++++++++++++++++++++++++++--
1 files changed, 58 insertions(+), 4 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 00a78ea..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
@@ -196,10 +196,8 @@
// 鍚堝苟澶勭悊鍣�
builder.registerWriteHandler(new CellMergeStrategy(list, true));
}
- if (CollUtil.isNotEmpty(options)) {
- // 娣诲姞涓嬫媺妗嗘搷浣�
- builder.registerWriteHandler(new ExcelDownHandler(options));
- }
+ // 娣诲姞涓嬫媺妗嗘搷浣�
+ builder.registerWriteHandler(new ExcelDownHandler(options));
builder.doWrite(list);
}
@@ -272,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 鐩綍涓嬬殑璺緞鍖呮嫭妯℃澘鏂囦欢鍚�
@@ -306,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