update 优化 支持excel方法抛出json异常
This commit is contained in:
parent
3761473967
commit
16923cc86a
@ -23,7 +23,6 @@ import org.dromara.common.excel.handler.DataWriteHandler;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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) {
|
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) {
|
||||||
try {
|
try {
|
||||||
resetResponse(sheetName, response);
|
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportExcel(list, sheetName, clazz, false, os, null);
|
exportExcel(list, sheetName, clazz, false, os, null);
|
||||||
} catch (IOException e) {
|
} 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) {
|
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response, List<DropDownOptions> options) {
|
||||||
try {
|
try {
|
||||||
resetResponse(sheetName, response);
|
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportExcel(list, sheetName, clazz, false, os, options);
|
exportExcel(list, sheetName, clazz, false, os, options);
|
||||||
} catch (IOException e) {
|
} 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) {
|
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response) {
|
||||||
try {
|
try {
|
||||||
resetResponse(sheetName, response);
|
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportExcel(list, sheetName, clazz, merge, os, null);
|
exportExcel(list, sheetName, clazz, merge, os, null);
|
||||||
} catch (IOException e) {
|
} 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) {
|
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response, List<DropDownOptions> options) {
|
||||||
try {
|
try {
|
||||||
resetResponse(sheetName, response);
|
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportExcel(list, sheetName, clazz, merge, os, options);
|
exportExcel(list, sheetName, clazz, merge, os, options);
|
||||||
} catch (IOException e) {
|
} 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,
|
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge,
|
||||||
OutputStream os, List<DropDownOptions> options) {
|
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)
|
ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
|
||||||
.autoCloseStream(false)
|
.autoCloseStream(false)
|
||||||
// 自动适配
|
// 自动适配
|
||||||
@ -215,9 +216,8 @@ public class ExcelUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> void exportTemplate(List<T> data, String filename, String templatePath, HttpServletResponse response) {
|
public static <T> void exportTemplate(List<T> data, String filename, String templatePath, HttpServletResponse response) {
|
||||||
try {
|
try {
|
||||||
resetResponse(filename, response);
|
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportTemplate(data, templatePath, os);
|
exportTemplate(data, filename, templatePath, os);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("导出Excel异常");
|
throw new RuntimeException("导出Excel异常");
|
||||||
}
|
}
|
||||||
@ -232,10 +232,13 @@ public class ExcelUtil {
|
|||||||
* @param data 模板需要的数据
|
* @param data 模板需要的数据
|
||||||
* @param os 输出流
|
* @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)) {
|
if (CollUtil.isEmpty(data)) {
|
||||||
throw new IllegalArgumentException("数据为空");
|
throw new IllegalArgumentException("数据为空");
|
||||||
}
|
}
|
||||||
|
if (os instanceof HttpServletResponse response) {
|
||||||
|
resetResponse(filename, response);
|
||||||
|
}
|
||||||
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
||||||
ExcelWriter excelWriter = EasyExcel.write(os)
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
||||||
.withTemplate(templateResource.getStream())
|
.withTemplate(templateResource.getStream())
|
||||||
@ -267,7 +270,7 @@ public class ExcelUtil {
|
|||||||
try {
|
try {
|
||||||
resetResponse(filename, response);
|
resetResponse(filename, response);
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportTemplateMultiList(data, templatePath, os);
|
exportTemplateMultiList(data, filename, templatePath, os);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("导出Excel异常");
|
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) {
|
public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String filename, String templatePath, HttpServletResponse response) {
|
||||||
try {
|
try {
|
||||||
resetResponse(filename, response);
|
|
||||||
ServletOutputStream os = response.getOutputStream();
|
ServletOutputStream os = response.getOutputStream();
|
||||||
exportTemplateMultiSheet(data, templatePath, os);
|
exportTemplateMultiSheet(data, filename, templatePath, os);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("导出Excel异常");
|
throw new RuntimeException("导出Excel异常");
|
||||||
}
|
}
|
||||||
@ -302,10 +304,13 @@ public class ExcelUtil {
|
|||||||
* @param data 模板需要的数据
|
* @param data 模板需要的数据
|
||||||
* @param os 输出流
|
* @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)) {
|
if (CollUtil.isEmpty(data)) {
|
||||||
throw new IllegalArgumentException("数据为空");
|
throw new IllegalArgumentException("数据为空");
|
||||||
}
|
}
|
||||||
|
if (os instanceof HttpServletResponse response) {
|
||||||
|
resetResponse(filename, response);
|
||||||
|
}
|
||||||
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
||||||
ExcelWriter excelWriter = EasyExcel.write(os)
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
||||||
.withTemplate(templateResource.getStream())
|
.withTemplate(templateResource.getStream())
|
||||||
@ -336,10 +341,13 @@ public class ExcelUtil {
|
|||||||
* @param data 模板需要的数据
|
* @param data 模板需要的数据
|
||||||
* @param os 输出流
|
* @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)) {
|
if (CollUtil.isEmpty(data)) {
|
||||||
throw new IllegalArgumentException("数据为空");
|
throw new IllegalArgumentException("数据为空");
|
||||||
}
|
}
|
||||||
|
if (os instanceof HttpServletResponse response) {
|
||||||
|
resetResponse(filename, response);
|
||||||
|
}
|
||||||
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
||||||
ExcelWriter excelWriter = EasyExcel.write(os)
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
||||||
.withTemplate(templateResource.getStream())
|
.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);
|
String filename = encodingFilename(sheetName);
|
||||||
FileUtils.setAttachmentResponseHeader(response, filename);
|
FileUtils.setAttachmentResponseHeader(response, filename);
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user