refactor(workBook): 重构 WorkBookController 并添加通用导出工具类

- 将 WorkBookController 重构为 RestController
- 新增 WorkBookResponseUtils 工具类,用于通用的 Excel导出功能
- 优化导入导出逻辑,支持任意对象列表的导出
- 添加单元格值设置异常处理
This commit is contained in:
whai 2024-11-24 11:05:34 +08:00
parent 2b3f4c8995
commit f37f1dd4b3
2 changed files with 93 additions and 1 deletions

View File

@ -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

View File

@ -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;
}
}