springboot使用easypoi实现导出Excel表格
Posted i小松i
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot使用easypoi实现导出Excel表格相关的知识,希望对你有一定的参考价值。
导入依赖
<!--excel操作--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>3.3.0</version> </dependency>
Excel工具类
public class ExcelUtils { /** * excel 导出 * * @param list 数据 * @param title 标题 * @param sheetName sheet名称 * @param pojoClass pojo类型 * @param fileName 文件名称 * @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 list 数据 * @param title 标题 * @param sheetName sheet名称 * @param pojoClass pojo类型 * @param fileName 文件名称 * @param response */ public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException { defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF)); } /** * excel 导出 * * @param list 数据 * @param pojoClass pojo类型 * @param fileName 文件名称 * @param response * @param exportParams 导出参数 */ 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 fileName 文件名称 * @param response */ public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { defaultExport(list, fileName, response); } /** * 默认的 excel 导出 * * @param list 数据 * @param pojoClass pojo类型 * @param fileName 文件名称 * @param response * @param exportParams 导出参数 */ private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); downLoadExcel(fileName, response, workbook); } /** * 默认的 excel 导出 * * @param list 数据 * @param fileName 文件名称 * @param response */ private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); downLoadExcel(fileName, response, workbook); } /** * 下载 * * @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-Type", "multipart/form-data"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLS.getValue(), "UTF-8")); workbook.write(response.getOutputStream()); } catch (Exception e) { throw new IOException(e.getMessage()); } } /** * excel 导入 * * @param filePath excel文件路径 * @param titleRows 标题行 * @param headerRows 表头行 * @param pojoClass pojo类型 * @param <T> * @return */ public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { if (StringUtils.isBlank(filePath)) { return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); params.setNeedSave(true); params.setSaveUrl("/excel/"); try { return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); } catch (NoSuchElementException e) { throw new IOException("模板不能为空"); } catch (Exception e) { throw new IOException(e.getMessage()); } } /** * excel 导入 * * @param file excel文件 * @param pojoClass pojo类型 * @param <T> * @return */ public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException { return importExcel(file, 1, 1, pojoClass); } /** * excel 导入 * * @param file excel文件 * @param titleRows 标题行 * @param headerRows 表头行 * @param pojoClass pojo类型 * @param <T> * @return */ public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException { return importExcel(file, titleRows, headerRows, false, pojoClass); } /** * excel 导入 * * @param file 上传的文件 * @param titleRows 标题行 * @param headerRows 表头行 * @param needVerfiy 是否检验excel内容 * @param pojoClass pojo类型 * @param <T> * @return */ public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, boolean needVerfiy, Class<T> pojoClass) throws IOException { if (file == null) { return null; } try { return importExcel(file.getInputStream(), titleRows, headerRows, needVerfiy, pojoClass); } catch (Exception e) { throw new IOException(e.getMessage()); } } /** * excel 导入 * * @param inputStream 文件输入流 * @param titleRows 标题行 * @param headerRows 表头行 * @param needVerfiy 是否检验excel内容 * @param pojoClass pojo类型 * @param <T> * @return */ public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, boolean needVerfiy, Class<T> pojoClass) throws IOException { if (inputStream == null) { return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); params.setSaveUrl("/excel/"); params.setNeedSave(true); params.setNeedVerfiy(needVerfiy); try { return ExcelImportUtil.importExcel(inputStream, pojoClass, params); } catch (NoSuchElementException e) { throw new IOException("excel文件不能为空"); } catch (Exception e) { throw new IOException(e.getMessage()); } } /** * Excel 类型枚举 */ enum ExcelTypeEnum { XLS("xls"), XLSX("xlsx"); private String value; ExcelTypeEnum(String value) { this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }
定义Vo实体类
@Data @EqualsAndHashCode(callSuper = false) public class ProductExportVo implements Serializable { private static final long serialVersionUID = 1L; /** * 商品编号 */ @Excel(name = "商品编号", orderNum = "0", width = 15) private String productNo; /** * 商品名称 */ @Excel(name = "商品名称", orderNum = "1", width = 25) private String productName; /** * 商品类型 */ @Excel(name = "商品类型", orderNum = "2", width = 15) private String productType; /** * 面值 */ @Excel(name = "面值", orderNum = "3", width = 15) private Integer productDeno; /** * 销售区域 0 省域 1 市域 2 全国 */ @Excel(name = "销售区域", orderNum = "4", width = 15) private String productSalesArea; /** * 销售价 */ @Excel(name = "销售价", orderNum = "5", width = 15) private BigDecimal productPrice; /** * 状态 0 下架 1 上架 */ @Excel(name = "状态", orderNum = "6", width = 15) private String productStatus; /** * 创建时间 */ @Excel(name = "创建时间", orderNum = "7", width = 25, exportFormat = "yyyy-MM-dd HH:mm:ss") private String productCreateTime; /** * 修改时间 */ @Excel(name = "修改时间", orderNum = "8", width = 25, exportFormat = "yyyy-MM-dd HH:mm:ss") private String productModifyTime; }
控制层
@PostMapping("products/exportAsExcel") public void exportAsExcel(HttpServletResponse response,ProductSearchDto productSearchDto) { final String name = SecurityContextHolder.getContext().getAuthentication().getName(); log.info("用户[{}, {}]查导出商品表", name, request.getRemoteAddr()); if (productSearchDto.getProductDenoRange() != null && productSearchDto.getProductDenoRange().length < 2) { productSearchDto.setProductDenoRange(null); } if (productSearchDto.getProductPriceRange() != null && productSearchDto.getProductPriceRange().length < 2) { productSearchDto.setProductPriceRange(null); } List<ProductExportVo> productList = productService.queryProduct(productSearchDto); try { ExcelUtils.exportExcel(productList,"商品信息表","商品信息",ProductExportVo.class,"商品信息",response); } catch (IOException e) { e.printStackTrace(); } }
前端Vue
downLoad(parmas) { var temp = document.createElement("form"); temp.action = `/api/products/exportAsExcel?${qs.stringify(parmas)}`; temp.method = "post"; temp.style.display = "none"; document.body.appendChild(temp); temp.submit(); },
更多Easypoi 用法参考文档:http://easypoi.mydoc.io/
以上是关于springboot使用easypoi实现导出Excel表格的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot+easypoi实现excel的导入导出
SpringBoot加Poi仿照EasyPoi实现Excel导出
springboot系列(二十二):集成easypoi实现Excel文件的导入导出(准备篇)
springboot系列(二十二):集成easypoi实现Excel文件的导入导出(准备篇)