优化代码

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,12 +156,7 @@ public class ExcelUtils {
*/
public static <T> void excelExport(List<List<String>> 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(head).sheet(StringUtils.isBlank(sheetName) ? "sheet1" : sheetName).doWrite(data);
} catch (IOException e) {
@ -174,6 +164,17 @@ public class ExcelUtils {
}
}
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");
response.setCharacterEncoding("UTF-8");
String fileName = URLUtil.encode(excelName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
return response;
}
/**
* 解析字典数据到字段上
* 比如 T中有 genderLabel字段 为男 需要给 gender 字段自动设置为0