From f37f1dd4b305b7c79bc3eee4528015164dbcb564 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 24 Nov 2024 11:05:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(workBook):=20=E9=87=8D=E6=9E=84=20Work?= =?UTF-8?q?BookController=20=E5=B9=B6=E6=B7=BB=E5=8A=A0=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 WorkBookController 重构为 RestController - 新增 WorkBookResponseUtils 工具类,用于通用的 Excel导出功能 - 优化导入导出逻辑,支持任意对象列表的导出 - 添加单元格值设置异常处理 --- .../workBook/WorkBookController.java | 9 +- .../export/WorkBookResponseUtils.java | 85 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/export/WorkBookResponseUtils.java diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/WorkBookController.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/WorkBookController.java index 8eb5060..20d017d 100644 --- a/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/WorkBookController.java +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/WorkBookController.java @@ -6,6 +6,7 @@ import com.google.common.collect.Lists; import jakarta.servlet.http.HttpServletResponse; import lombok.Data; import lombok.Setter; +import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -17,8 +18,10 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.io.*; +import java.lang.reflect.Field; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -29,9 +32,13 @@ import java.util.List; * @Date 2024/11/20 16:35 * @注释 */ -@Controller +@RestController @RequestMapping("/workBook") public class WorkBookController { + + + + @GetMapping(value = "/exportUser") public void export(HttpServletResponse response) throws UnsupportedEncodingException { // WorkBook diff --git a/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/export/WorkBookResponseUtils.java b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/export/WorkBookResponseUtils.java new file mode 100644 index 0000000..978ab11 --- /dev/null +++ b/SpringDemo/src/main/java/cn/whaifree/springdemo/controller/workBook/export/WorkBookResponseUtils.java @@ -0,0 +1,85 @@ +package cn.whaifree.springdemo.controller.workBook.export; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.ReflectUtil; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Field; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Optional; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/22 16:56 + * @注释 + */ +public class WorkBookResponseUtils { + + public static void setHead(HttpServletResponse response, String fileName) throws UnsupportedEncodingException { + // WorkBook + fileName = Optional.ofNullable(fileName).orElse("workBook.xlsx"); + + response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder + .encode(fileName, StandardCharsets.UTF_8)); + response.setContentType("application/vnd.ms-excel;charset=utf-8"); + } + + public static Workbook getWorkBook(List list, Class clazz, HttpServletResponse response, String fileName) { + try { + setHead(response, fileName); + return getWorkBook(list, clazz); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + public static Workbook getWorkBook(List list, Class clazz) { + try (InputStream inputStream = ExportAutoController.class.getClassLoader().getResourceAsStream("template/empty.xlsx")) { + Workbook workbook = new XSSFWorkbook(inputStream); + Sheet sheetAt = workbook.getSheetAt(0); + Field[] fields = ReflectUtil.getFields(clazz); + Row row = sheetAt.getRow(0); + if (row == null) { + row = sheetAt.createRow(0); + } + for (int i = 0; i < fields.length; i++) { + Cell cell = row.createCell(i); + cell.setCellValue(fields[i].getName()); + } + int length = fields.length; // 列数 + for (int i = 0; i < list.size(); i++) { + Object o = list.get(i); + row = sheetAt.createRow(i + 1); + for (int j = 0; j < length; j++) { + Cell cell = row.createCell(j); + Field field = fields[j]; + field.setAccessible(true); + try { + Object value = field.get(o); // 获取字段值 + if (value != null) { + cell.setCellValue(value.toString()); // 将字段值设置到单元格中 + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + return workbook; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +}