导入依赖
<!--excel操作--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>3.3.0</version> </dependency>
定义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(); },