优化代码

This commit is contained in:
阿沐 2023-10-06 22:36:29 +08:00
parent bd1595331b
commit 32a16c753f

View File

@ -137,12 +137,7 @@ public class ExcelUtils {
*/
public static <T> void excelExport(Class<T> head, String excelName, String sheetName, List<T> data) {
try {
HttpServletResponse response = HttpContextUtils.getHttpServletResponse();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setCharacterEncoding("UTF-8");
String fileName = URLUtil.encode(excelName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
HttpServletResponse response = getExportResponse(excelName);
EasyExcel.write(response.getOutputStream(), head).sheet(StringUtils.isBlank(sheetName) ? "sheet1" : sheetName).doWrite(data);
} catch (IOException e) {
@ -161,6 +156,15 @@ public class ExcelUtils {
*/
public static <T> void excelExport(List<List<String>> head, String excelName, String sheetName, List<T> data) {
try {
HttpServletResponse response = getExportResponse(excelName);
EasyExcel.write(response.getOutputStream()).head(head).sheet(StringUtils.isBlank(sheetName) ? "sheet1" : sheetName).doWrite(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static HttpServletResponse getExportResponse(String excelName) {
HttpServletResponse response = HttpContextUtils.getHttpServletResponse();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
@ -168,10 +172,7 @@ public class ExcelUtils {
String fileName = URLUtil.encode(excelName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream()).head(head).sheet(StringUtils.isBlank(sheetName) ? "sheet1" : sheetName).doWrite(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
return response;
}
/**