文章目录

  • 业务需求
  • 需求难点
  • 通过 EasyPoi 实现需求
  • 具体实现

业务需求

从一个简单的仓库业务说起,仓库业务,会有进库记录,会有出库记录,会有库存,客户的需求就是需要一个库存盘点单,盘点单通俗来讲:将库存中每个商品的出入库记录都统计出来,看看每个商品出过多少货物,入过多少货物,本月库存多少,上月库存多少。

需求难点

一个货物会出过多次货物,入过多次货物,导出的 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;}
}

实体类中使用的注解作用解释:

  1. @Getter lombok 注解,用于给所有属性提供 getter 方法
  2. @Setter lombok 注解,用于给所有属性提供 setter 方法
  3. @Excel easypoi 注解,name 就等于导出 excel 的列名称,width 就是宽度,type 就是这个属性的类型,1表示文本,默认也是文本,10就是数字,needMerge 表示是否纵向合并单元格,也就是上下列合并
  4. @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 就类似于如下这种:

使用EasyPoi完成复杂一对多excel表格导出功能相关推荐

  1. 使用 EasyPoi 完成复杂一对多 excel 表格导出功能

    点击关注公众号,利用碎片时间学习 业务需求 从一个简单的仓库业务说起,仓库业务,会有进库记录,会有出库记录,会有库存,客户的需求就是需要一个库存盘点单,盘点单通俗来讲:将库存中每个商品的出入库记录都统 ...

  2. linux脚本的数据输出到excel,使用shell实现Excel表格导出功能 | 剑花烟雨江南

    在Web项目中,我们经常会遇到Excel表格导出的功能,对于一些数据实时性要求不高的.逻辑相对简单的导出,是否可用通过shell脚本的方式来进行导出,从而降低开发成本呢? 我们都知道,CSV格式可以用 ...

  3. js导出变量 vue_vue.js前端实现excel表格导出和获取headers里的信息

    前段时间写过一篇文章基于element实现后台管理系统,并提到excel表格导出功能,可能描述不是很详细,现在单独整理列出. 后端提供的接口: // 下载分拣列表 export function ge ...

  4. 《springboot中实现excel表格导出》

    <springboot中实现excel表格导出> 简介 在Spring Boot中,实现Excel表格导出的方式有很多种,以下是几种常见的方法: 使用Apache POI:Apache P ...

  5. vue数组转Excel表格导出

    vue数组转Excel表格导出 安装依赖 npm i xlsx vue组件 <template><div><el-button type="success&qu ...

  6. 通用Excel表格导出(Map类型数据导出为表格)

    背景 为提升代码开发效率,项目使用了通用查询(动态数据表.动态条件.动态列名等),各表查询通过同一个页面展现,前端通过获取路径上的表名调用同一个后端控制器--动态获取到查询条件.数据列名.不同表数据等 ...

  7. C# Winfrom Excel表格导出 Aspose.Cells超简单方式

    C# Winfrom Excel表格导出 Aspose.Cells超简单方式 首先需要下载 Aspose.Cells.dll,Aspose.Slides.dll,Aspose.Words.dll 这三 ...

  8. thinkphp excel表格导出

    Thinkphp里实现excel表格导出数据,需要在网上下载PHPExcel类包,放在Vendor文件夹下面 地址:http://phpexcel.codeplex.com/releases/view ...

  9. excel表格导出之后身份证号列变成了科学计数法

    excel表格导出之后身份证号列变成了科学计数法 解决:写sql查询出所有数据,并在身份证列添加字符,然后导出,将要复制的excel表格设置单元格格式问文本类型,然后复制粘贴,再把加入的字符删除,搞定 ...

  10. Java代码实现Excel表格导出

    Java代码实现Excel表格导出 public static ResponseEntity<byte[]> employee2Excel(List<Employee> lis ...

最新文章

  1. Linux centos7安装nfs及服务端配置
  2. oa部署mysql_oa系统部署
  3. 简单几步让你实现本地jar包引入到maven当中
  4. 从Zipkin到Jaeger,Uber的分布式追踪之道tchannel
  5. 验证哥德巴赫猜想c语言算法,验证哥德巴赫猜想的简单优化
  6. 干货分享:插画家Anmi的创作技巧与练习方法
  7. epoll nio区别_【总结】两种 NIO 实现:Selector 与 Epoll
  8. map集合遍历的五种方法
  9. python读取枚举_在python中枚举(enumerate in python)
  10. linux ubutu书籍,Ubuntu Linux入门到精通[图书]
  11. vuex获取php数据,Nuxt中如何使用Vuex-Store异步获取数据
  12. Maven MyEclipse创建web项目没有src/maim/java
  13. win10多合一原版系统_【教程】制作Windows 10 多合一原版系统
  14. 微软开源网络攻防模拟工具CyberBattleSim介绍及源码分析
  15. 测绘的真正出路在于什么?
  16. mac下免费svn工具
  17. UE4 Take Recorder的使用
  18. 基于Java的网上商城系统
  19. 经典美文诵读2 If I Were a Boy Again假如我又回到了童年
  20. 动物识别系统代码python_人工智能-动物识别专家系统算法Python + Pyqt 实现

热门文章

  1. IIS7管理器设置网站首页
  2. 前端vue实现PDF预览
  3. Unity 粒子特效(Particle System)大小自适应和层级的一些问题
  4. unity3d UI粒子特效裁剪
  5. 装系统比较好用的PE工具推荐
  6. PowerBuilder快速入门实践
  7. 商业的10个最佳Android应用程序模板
  8. 深入学习IOZone【转】
  9. Tomcat的日志配置
  10. 实用供热空调设计手册_暖通空调设计与施工数据图表手册