springboot整合easypoi实现浏览器自动下载excel文件,一行代码实现,附带完整项目和导出工具。(一)

您所在的位置:网站首页 浏览器文件导出excel springboot整合easypoi实现浏览器自动下载excel文件,一行代码实现,附带完整项目和导出工具。(一)

springboot整合easypoi实现浏览器自动下载excel文件,一行代码实现,附带完整项目和导出工具。(一)

2024-07-11 01:50| 来源: 网络整理| 查看: 265

先看效果图: 在这里插入图片描述 备注:

此方法不能实现自定义表头导出,只能导出实体类中所有的属性此种方法封装了easypoi的方法如果需要自定义表头,需要使用easypoi原理实现。详情请点击springboot整合easypoi实现浏览器自动下载自定义表头excel文件,,附带导出工具。完整项目下载项目地址(温馨提示,一下代码基本上展示95%)

可能出现的问题:

java.lang.NoClassDefFoundError:org/apache/poi/xssf/streaming/SXSSFWorkbook

这样是因为jar的问题,因为jar会进行覆盖,所以按照下面导入jar不会出现这种问题。

实现过程 4. 引入依赖

cn.afterturn easypoi-base org.apache.poi poi-ooxml org.apache.poi poi 4.2.0 org.apache.poi poi-ooxml 4.1.1 compile cn.afterturn easypoi-annotation 4.2.0 org.apache.poi poi 4.1.1

5. 工具类

/** * Excel导入导出工具类 * @author lenovo */ public class ExcelUtil { /** * excel 导出 * * @param list 数据列表 * @param fileName 导出时的excel名称 * @param response */ public static void exportExcel(List list, String fileName, HttpServletResponse response) throws IOException { defaultExport(list, fileName, response); } /** * 默认的 excel 导出 * * @param list 数据列表 * @param fileName 导出时的excel名称 * @param response */ private static void defaultExport(List list, String fileName, HttpServletResponse response) throws IOException { //把数据添加到excel表格中 Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); downLoadExcel(fileName, response, workbook); } /** * excel 导出 * * @param list 数据列表 * @param pojoClass pojo类型 * @param fileName 导出时的excel名称 * @param response * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型) */ private static void defaultExport(List list, Class pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException { //把数据添加到excel表格中 Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); downLoadExcel(fileName, response, workbook); } /** * excel 导出 * * @param list 数据列表 * @param pojoClass pojo类型 * @param fileName 导出时的excel名称 * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型) * @param response */ public static void exportExcel(List list, Class pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException { defaultExport(list, pojoClass, fileName, response, exportParams); } /** * excel 导出 * * @param list 数据列表 * @param title 表格内数据标题 * @param sheetName sheet名称 * @param pojoClass pojo类型 * @param fileName 导出时的excel名称 * @param response */ public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, HttpServletResponse response) throws IOException { ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF); defaultExport(list, pojoClass, fileName, response,exportParams); } /** * excel 导出 * * @param list 数据列表 * @param title 表格内数据标题 * @param sheetName sheet名称 * @param pojoClass pojo类型 * @param fileName 导出时的excel名称 * @param isCreateHeader 是否创建表头 * @param response */ public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException { ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF); exportParams.setCreateHeadRows(isCreateHeader); defaultExport(list, pojoClass, fileName, response, exportParams); } /** * excel下载 * * @param fileName 下载时的文件名称 * @param response * @param workbook excel数据 */ private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8")); workbook.write(response.getOutputStream()); } catch (Exception e) { throw new IOException(e.getMessage()); } } }

6. 实体类 @ExcelTarget(“Export”) 千万不能忘记 @Excel(name = “时间”, width = 30)

package com.excel.entity; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import lombok.Data; import java.io.Serializable; /** * @author lenovo */ @Data @ExcelTarget("Export") public class Export implements Serializable { private static final long serialVersionUID = 1L; /** * 时间 */ @Excel(name = "时间", width = 30) private String datetime; /** * 姓名 */ @Excel(name = "姓名", width = 30) private String name; /** * 年龄 */ @Excel(name = "年龄", width = 30) private String age; /** * 性别 */ @Excel(name = "性别", width = 30) private String gender; /** * 地址 */ @Excel(name = "地址", width = 30) private String address; }

7. 接口

@GetMapping("/export") public void export(HttpServletResponse response) throws Exception { //为了达到数据展示,手动写一些数据,实际情况可以去数据库获取 List exportList=new ArrayList(); for (int i=1;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3