使用 EasyPoi 完成复杂一对多 excel 表格导出功能
点击关注公众号,利用碎片时间学习
业务需求
从一个简单的仓库业务说起,仓库业务,会有进库记录,会有出库记录,会有库存,客户的需求就是需要一个库存盘点单,盘点单通俗来讲:将库存中每个商品的出入库记录都统计出来,看看每个商品出过多少货物,入过多少货物,本月库存多少,上月库存多少。
需求难点
一个货物会出过多次货物,入过多次货物,导出的 excel 就要做成 一对多 格式的导出
简单举例:
啤酒:入库2次,出库3次,最终体现在 excel 中效果如下图:
通过 EasyPoi 实现需求
EasyPoi 文档地址:
http://doc.wupaas.com/docs/easypoi/easypoi-1c0u4mo8p4ro8
SpringBoot 使用:
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.2.0</version>
</dependency>
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.2.0</version>
</dependency>
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.2.0</version>
</dependency>
Gradle 使用:
implementation 'cn.afterturn:easypoi-base:4.2.0'
implementation 'cn.afterturn:easypoi-annotation:4.2.0'
implementation 'cn.afterturn:easypoi-web:4.2.0'
使用 EasyPoi 提供的注解,自定义导出类模板
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import lombok.Getter;
import lombok.Setter;import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;/*** 导出 excel 模板类*/
@Getter
@Setter
public class ExportTemplate implements Serializable {@Excel(name = "序号", needMerge = true, type = 10)private int index;@Excel(name = "商品名称", needMerge = true, width = 30.0)private String goodName;@Excel(name = "商品单位", needMerge = true)private String goodUnit;@Excel(name = "上月库存数量", needMerge = true, type = 10)private Integer lastMonthSurplusNum;@Excel(name = "本月库存数量", needMerge = true, type = 10)private Integer thisMonthSurplusNum;@ExcelCollection(name = "本月入库信息")private List<GoodInItem> goodInItems;@ExcelCollection(name = "本月出库信息")private List<GoodOutItem> goodOutItems;@Excel(name = "备注", needMerge = true, width = 30.0)private String remark;/*** 入库信息*/@Getter@Setterpublic static class GoodInItem {@Excel(name = "入库日期", exportFormat = "yyyy-MM-dd", width = 20.50)private Date purchaseDate;@Excel(name = "入库号", width = 25.50)private String purchaseNum;@Excel(name = "入库单价", type = 10)private BigDecimal unitPrice;@Excel(name = "入库数量", type = 10)private Integer totalNum;}/*** 出库信息*/@Getter@Setterpublic static class GoodOutItem {@Excel(name = "出库日期", exportFormat = "yyyy-MM-dd", width = 20.50)private Date outDate;@Excel(name = "出库号", width = 25.50)private String sellNum;@Excel(name = "出库数量", type = 10)private Integer totalNum;@Excel(name = "成本金额", type = 10)private BigDecimal priceIn;@Excel(name = "销售金额", type = 10)private BigDecimal priceOut;}
}
实体类中使用的注解作用解释:
@Getter lombok 注解,用于给所有属性提供 getter 方法
@Setter lombok 注解,用于给所有属性提供 setter 方法
@Excel easypoi 注解,name 就等于导出 excel 的列名称,width 就是宽度,type 就是这个属性的类型,1表示文本,默认也是文本,10就是数字,needMerge 表示是否纵向合并单元格,也就是上下列合并
@ExcelCollection easypoi 注解,name 就等于导出 excel 的列名称,被此注解标注的集合,就等于在其列下面创建对等数量的行,就类似于这种
最后模板弄好之后,就可以通过easypoi 的工具类来导出,easypoi 推荐的导出工具类如下:
这个方法的三个参数表示含义解释:
ExportParams :参数表示Excel 导出参数设置类,easypoi 自定义的类
pojoClass:你要导出的类模板
dataSet:数据集合
具体实现
@GetMapping(value = "export")
public void export(HttpServletRequest req, HttpServletResponse resp) {List<ExportTemplate> exportData = new ArrayList();// 步骤1:构建要导出excel的数据集合for (int i = 0; i < 5; i++) {ExportTemplate data = new ExportTemplate();data.setIndex(i);data.setGoodName("测试商品");data.setGoodUnit("瓶");data.setLastMonthSurplusNum(5); // 上月库存data.setThisMonthSurplusNum(3); // 本月库存// ... 剩下的就是类似的加值exportData.add(data);}try {// 步骤2:开始导出 excelExportParams params = new ExportParams();params.setTitle("库存盘点单标题");params.setSheetName("库存盘点单工作表名称");params.setType(ExcelType.XSSF);Workbook workbook = ExcelExportUtil.exportExcel(params, ExportTemplate.class, exportData);String nowStr = DateTimeFormatter.ofPattern(LocalDateTime.now()).format("yyyyMMddHHmm"); // 时间串String fileName = nowStr + "_库存盘点单"; // 文件名称String tempDir = "C:/Users/huxim/Downloads";File filePath = new File(tempDir + File.separator);if (!filePath.exists()) filePath.mkdirs(); // 如果文件目录不存在就创建这个目录FileOutputStream fos = new FileOutputStream(tempDir + File.separator + fileName);workbook.write(fos);fos.close();resp.setContentType("application/octet-stream");resp.setCharacterEncoding("utf-8");response.addHeader("Content-disposition", "attachment; filename="+ this.makeDownloadFileName(req, fileName));IOUtils.copy(new FileInputStream(tempFile), response.getOutputStream());System.out.println("导出成功~~~");} catch (Exception e) {throw new RuntimeException("导出 excel 失败~~~");}
}/*** 判断是否是 IE 浏览器* 返回对应的字符串格式*/
public static String makeDownloadFileName(HttpServletRequest request, String fileName) {String agent = request.getHeader("User-Agent");byte[] bytes = fileName.getBytes(StandardCharsets.UTF_8);if (agent.contains("MSIE") || agent.contains("Trident") || agent.contains("Edge")) {// IEreturn new String(bytes, StandardCharsets.UTF_8);} else {return new String(bytes, StandardCharsets.ISO_8859_1);}
}
导出成功后的excel 就类似于如下这种:
感谢阅读,希望对你有所帮助 :)
来源:blog.csdn.net/qq_43647359/article/
details/117512165
推荐:最全的java面试题库PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!
使用 EasyPoi 完成复杂一对多 excel 表格导出功能相关推荐
- linux脚本的数据输出到excel,使用shell实现Excel表格导出功能 | 剑花烟雨江南
在Web项目中,我们经常会遇到Excel表格导出的功能,对于一些数据实时性要求不高的.逻辑相对简单的导出,是否可用通过shell脚本的方式来进行导出,从而降低开发成本呢? 我们都知道,CSV格式可以用 ...
- js导出变量 vue_vue.js前端实现excel表格导出和获取headers里的信息
前段时间写过一篇文章基于element实现后台管理系统,并提到excel表格导出功能,可能描述不是很详细,现在单独整理列出. 后端提供的接口: // 下载分拣列表 export function ge ...
- 《springboot中实现excel表格导出》
<springboot中实现excel表格导出> 简介 在Spring Boot中,实现Excel表格导出的方式有很多种,以下是几种常见的方法: 使用Apache POI:Apache P ...
- vue数组转Excel表格导出
vue数组转Excel表格导出 安装依赖 npm i xlsx vue组件 <template><div><el-button type="success&qu ...
- 通用Excel表格导出(Map类型数据导出为表格)
背景 为提升代码开发效率,项目使用了通用查询(动态数据表.动态条件.动态列名等),各表查询通过同一个页面展现,前端通过获取路径上的表名调用同一个后端控制器--动态获取到查询条件.数据列名.不同表数据等 ...
- C# Winfrom Excel表格导出 Aspose.Cells超简单方式
C# Winfrom Excel表格导出 Aspose.Cells超简单方式 首先需要下载 Aspose.Cells.dll,Aspose.Slides.dll,Aspose.Words.dll 这三 ...
- thinkphp excel表格导出
Thinkphp里实现excel表格导出数据,需要在网上下载PHPExcel类包,放在Vendor文件夹下面 地址:http://phpexcel.codeplex.com/releases/view ...
- excel表格导出之后身份证号列变成了科学计数法
excel表格导出之后身份证号列变成了科学计数法 解决:写sql查询出所有数据,并在身份证列添加字符,然后导出,将要复制的excel表格设置单元格格式问文本类型,然后复制粘贴,再把加入的字符删除,搞定 ...
- Java代码实现Excel表格导出
Java代码实现Excel表格导出 public static ResponseEntity<byte[]> employee2Excel(List<Employee> lis ...
最新文章
- 深入理解IOC模式及Unity框架
- 一周一论文(翻译)——[SIGMOD 2016] RDMA over Commodity Ethernet at Scale
- Feb 26 Programming Notes
- jq之hover()
- C# 序列化理解 1(转)
- 一个程序员的简洁职业生涯规划
- 安卓线程同步面试_面试BAT大厂,可少不了这些题目!
- 某游戏服务运维架构进化史(上云方案)
- android ListView和GridView拖拽移位具体实现及拓展
- python常用语音识别库_python语音识别
- 计算机网络技术动态路由配置,计算机网络实验六动态路由的配置
- python爬取歌曲_python爬取网易云音乐热歌榜实例代码
- 好用的Bin文件查看器,J-flash
- opencv-python(cv2)——如何读取和保存中文路径图片(含代码)
- Codeforeces #710 div3题解报告
- 记一次lumen直接删除migration文件踩的坑
- NO2.高可用搭建-mysql安装和双主配置
- 马云的 18 个合伙创办人现在各自情况怎样?
- Qt扫盲-QNetworkAccessManager理论总结
- [学习笔记]金融风控实战