dengzedong
2024-12-25 73a05d31b68521c8154816d67bfbe0c8ccacf25b
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.excel.core.util;
H 2
3 import com.iailab.framework.excel.core.handler.SelectSheetWriteHandler;
4 import com.alibaba.excel.EasyExcel;
5 import com.alibaba.excel.converters.longconverter.LongStringConverter;
6 import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
7 import org.springframework.web.multipart.MultipartFile;
8
9 import javax.servlet.http.HttpServletResponse;
10 import java.io.IOException;
11 import java.net.URLEncoder;
12 import java.nio.charset.StandardCharsets;
13 import java.util.List;
14
15 /**
16  * Excel 工具类
17  *
18  * @author iailab
19  */
20 public class ExcelUtils {
21
22     /**
23      * 将列表以 Excel 响应给前端
24      *
25      * @param response  响应
26      * @param filename  文件名
27      * @param sheetName Excel sheet 名
28      * @param head      Excel head 头
29      * @param data      数据列表哦
30      * @param <T>       泛型,保证 head 和 data 类型的一致性
31      * @throws IOException 写入失败的情况
32      */
33     public static <T> void write(HttpServletResponse response, String filename, String sheetName,
34                                  Class<T> head, List<T> data) throws IOException {
35         // 输出 Excel
36         EasyExcel.write(response.getOutputStream(), head)
37                 .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
38                 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度
03e8ac 39                 .registerWriteHandler(new SelectSheetWriteHandler(head,false)) // 基于固定 sheet 实现下拉框
e7c126 40                 .registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
H 41                 .sheet(sheetName).doWrite(data);
42         // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
43         response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
44         response.setContentType("application/vnd.ms-excel;charset=UTF-8");
45     }
46
47     public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
48         return EasyExcel.read(file.getInputStream(), head, null)
49                 .autoCloseStream(false)  // 不要自动关闭,交给 Servlet 自己处理
50                 .doReadAllSync();
51     }
52
03e8ac 53     public static <T> void write(HttpServletResponse response, String filename, String sheetName,
J 54                                  Class<T> head, List<T> data, boolean selectFlag) throws IOException {
55         // 输出 Excel
56         EasyExcel.write(response.getOutputStream(), head)
57                 .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
58                 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度
59                 .registerWriteHandler(new SelectSheetWriteHandler(head,selectFlag)) // 基于固定 sheet 实现下拉框
60                 .registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
61                 .sheet(sheetName).doWrite(data);
62         // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
63         response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
64         response.setContentType("application/vnd.ms-excel;charset=UTF-8");
65     }
e7c126 66 }