Procházet zdrojové kódy

update 优化 支持excel方法抛出json异常

疯狂的狮子Li před 1 rokem
rodič
revize
16923cc86a

+ 22 - 14
ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java

@@ -23,7 +23,6 @@ import org.dromara.common.excel.handler.DataWriteHandler;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -84,7 +83,6 @@ public class ExcelUtil {
      */
     public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) {
         try {
-            resetResponse(sheetName, response);
             ServletOutputStream os = response.getOutputStream();
             exportExcel(list, sheetName, clazz, false, os, null);
         } catch (IOException e) {
@@ -103,7 +101,6 @@ public class ExcelUtil {
      */
     public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response, List<DropDownOptions> options) {
         try {
-            resetResponse(sheetName, response);
             ServletOutputStream os = response.getOutputStream();
             exportExcel(list, sheetName, clazz, false, os, options);
         } catch (IOException e) {
@@ -122,7 +119,6 @@ public class ExcelUtil {
      */
     public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response) {
         try {
-            resetResponse(sheetName, response);
             ServletOutputStream os = response.getOutputStream();
             exportExcel(list, sheetName, clazz, merge, os, null);
         } catch (IOException e) {
@@ -142,7 +138,6 @@ public class ExcelUtil {
      */
     public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response, List<DropDownOptions> options) {
         try {
-            resetResponse(sheetName, response);
             ServletOutputStream os = response.getOutputStream();
             exportExcel(list, sheetName, clazz, merge, os, options);
         } catch (IOException e) {
@@ -186,6 +181,12 @@ public class ExcelUtil {
      */
     public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge,
                                        OutputStream os, List<DropDownOptions> options) {
+        if (CollUtil.isEmpty(list)) {
+            throw new IllegalArgumentException("数据为空");
+        }
+        if (os instanceof HttpServletResponse response) {
+            resetResponse(sheetName, response);
+        }
         ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
             .autoCloseStream(false)
             // 自动适配
@@ -215,9 +216,8 @@ public class ExcelUtil {
      */
     public static <T> void exportTemplate(List<T> data, String filename, String templatePath, HttpServletResponse response) {
         try {
-            resetResponse(filename, response);
             ServletOutputStream os = response.getOutputStream();
-            exportTemplate(data, templatePath, os);
+            exportTemplate(data, filename, templatePath, os);
         } catch (IOException e) {
             throw new RuntimeException("导出Excel异常");
         }
@@ -232,10 +232,13 @@ public class ExcelUtil {
      * @param data         模板需要的数据
      * @param os           输出流
      */
-    public static <T> void exportTemplate(List<T> data, String templatePath, OutputStream os) {
+    public static <T> void exportTemplate(List<T> data, String filename, String templatePath, OutputStream os) {
         if (CollUtil.isEmpty(data)) {
             throw new IllegalArgumentException("数据为空");
         }
+        if (os instanceof HttpServletResponse response) {
+            resetResponse(filename, response);
+        }
         ClassPathResource templateResource = new ClassPathResource(templatePath);
         ExcelWriter excelWriter = EasyExcel.write(os)
             .withTemplate(templateResource.getStream())
@@ -267,7 +270,7 @@ public class ExcelUtil {
         try {
             resetResponse(filename, response);
             ServletOutputStream os = response.getOutputStream();
-            exportTemplateMultiList(data, templatePath, os);
+            exportTemplateMultiList(data, filename, templatePath, os);
         } catch (IOException e) {
             throw new RuntimeException("导出Excel异常");
         }
@@ -285,9 +288,8 @@ public class ExcelUtil {
      */
     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);
+            exportTemplateMultiSheet(data, filename, templatePath, os);
         } catch (IOException e) {
             throw new RuntimeException("导出Excel异常");
         }
@@ -302,10 +304,13 @@ public class ExcelUtil {
      * @param data         模板需要的数据
      * @param os           输出流
      */
-    public static void exportTemplateMultiList(Map<String, Object> data, String templatePath, OutputStream os) {
+    public static void exportTemplateMultiList(Map<String, Object> data, String filename, String templatePath, OutputStream os) {
         if (CollUtil.isEmpty(data)) {
             throw new IllegalArgumentException("数据为空");
         }
+        if (os instanceof HttpServletResponse response) {
+            resetResponse(filename, response);
+        }
         ClassPathResource templateResource = new ClassPathResource(templatePath);
         ExcelWriter excelWriter = EasyExcel.write(os)
             .withTemplate(templateResource.getStream())
@@ -336,10 +341,13 @@ public class ExcelUtil {
      * @param data         模板需要的数据
      * @param os           输出流
      */
-    public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) {
+    public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String filename, String templatePath, OutputStream os) {
         if (CollUtil.isEmpty(data)) {
             throw new IllegalArgumentException("数据为空");
         }
+        if (os instanceof HttpServletResponse response) {
+            resetResponse(filename, response);
+        }
         ClassPathResource templateResource = new ClassPathResource(templatePath);
         ExcelWriter excelWriter = EasyExcel.write(os)
             .withTemplate(templateResource.getStream())
@@ -366,7 +374,7 @@ public class ExcelUtil {
     /**
      * 重置响应体
      */
-    private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException {
+    private static void resetResponse(String sheetName, HttpServletResponse response) {
         String filename = encodingFilename(sheetName);
         FileUtils.setAttachmentResponseHeader(response, filename);
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");