## # Apache POI 一些实例,可供学习和参考cases study (opens new window) > **参考** > [write Excel](http://gael-home.appspot.com//home/WriteExcel/overview.htm) https://poi.apache.org/components/spreadsheet/quick-guide.html#NewWorkbook ## # Easy Excel 看国内开源项目大都开始使用easy excel了 本次我就先试试easy excel,因为感觉poi有点复杂不容易上手。 ```{lang} com.alibaba easyexcel 3.3.1 ``` ```{lang} public class ExcelUtils { /** * 将列表以 Excel 响应给前端 * * @param response 响应 * @param filename 文件名 * @param sheetName Excel sheet 名 * @param head Excel head 头 * @param data 数据列表哦 * @param 泛型,保证 head 和 data 类型的一致性 * @throws IOException 写入失败的情况 */ public static void write(HttpServletResponse response, String filename, String sheetName, Class head, List data) throws IOException { // 输出 Excel EasyExcel.write(response.getOutputStream(), head) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 .sheet(sheetName).doWrite(data); // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); } /** * 从配置文件中读取excel首行title * @param response * @param filename * @param sheetName * @param head * @param data * @param * @throws IOException */ public static void variableTitleWrite(HttpServletResponse response, String filename, String sheetName, Class head, List data) throws IOException { // 输出 Excel EasyExcel.write(response.getOutputStream(), head) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 .sheet(sheetName).doWrite(data); // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); } public static List read(MultipartFile file, Class head) throws IOException { return EasyExcel.read(file.getInputStream(), head, null) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .doReadAllSync(); } } ``` Loading... ## # Apache POI 一些实例,可供学习和参考cases study (opens new window) > **参考** > [write Excel](http://gael-home.appspot.com//home/WriteExcel/overview.htm) https://poi.apache.org/components/spreadsheet/quick-guide.html#NewWorkbook ## # Easy Excel 看国内开源项目大都开始使用easy excel了 本次我就先试试easy excel,因为感觉poi有点复杂不容易上手。 ```{lang} <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.3.1</version> </dependency> ``` ```{lang} public class ExcelUtils { /** * 将列表以 Excel 响应给前端 * * @param response 响应 * @param filename 文件名 * @param sheetName Excel sheet 名 * @param head Excel head 头 * @param data 数据列表哦 * @param <T> 泛型,保证 head 和 data 类型的一致性 * @throws IOException 写入失败的情况 */ public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data) throws IOException { // 输出 Excel EasyExcel.write(response.getOutputStream(), head) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 .sheet(sheetName).doWrite(data); // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); } /** * 从配置文件中读取excel首行title * @param response * @param filename * @param sheetName * @param head * @param data * @param <T> * @throws IOException */ public static <T> void variableTitleWrite(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data) throws IOException { // 输出 Excel EasyExcel.write(response.getOutputStream(), head) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 .sheet(sheetName).doWrite(data); // 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); } public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException { return EasyExcel.read(file.getInputStream(), head, null) .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .doReadAllSync(); } } ``` 最后修改:2025 年 07 月 14 日 © 允许规范转载 赞 别打赏,我怕忍不住购买辣条与续命水