Java中EasyPoi多sheet导出功能实现
作者:画小柏 时间:2023-01-15 08:10:39
EasyPoi 多sheet导出
序言:之前一直想开始写博客,都没有时间行动起来,今天终于开始了我的第一篇博客…
最近接到一个导出excel功能的需求,该功能主要难点是
多sheet页
导出合并单元格(跨行、跨列)
多表头合并
我开始的想法是如果采用poi来实现这个功能,业务逻辑可能会有点复杂,于是我使用了easyPoi——一个so easy的工具,它的特点就是非常方便,用jQuery的一句来说就是:write Less,Do More。
话不多说,接下来分享一下我的代码(我使用的是SSH框架搭建环境)
一、引入maven jar包
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
二、编写导出的实体类(使用注解形式)
1、DeptUtil 类
@ExcelTarget("deptUtil")
public class DeptUtil {
@Excel(name = "部门编号", width = 30 , needMerge = true)
private Integer id;
@Excel(name = "部门名称", width = 30 , needMerge = true)
private String deptName;
@ExcelCollection(name = "员工信息")
private List<EmpUtil> emps;
....省略getter、setter方法
2、EmpUtil类
@ExcelTarget("empUtil")
public class EmpUtil{
@Excel(name = "序号", width = 30, isColumnHidden = true)
private Integer id;
@Excel(name = "员工姓名", width = 30, groupName = "基本信息")
private String empName;
@Excel(name = "年龄", width = 30, type = 10, groupName = "基本信息")
private Integer age;
@Excel(name = "入职时间", width = 30, groupName = "工作信息", format = "yyyy/MM/dd HH:mm")
private Date hiredate;
@Excel(name = "薪酬", width = 30, type = 10, groupName = "工作信息")
private BigDecimal salary;
....省略getter、setter方法
3、核心代码
public String export(){
Workbook workBook = null;
try {
List<DeptUtil> exportList = exportService.exportList();
System.err.println(JSONArray.toJSONString(exportList));
// 创建参数对象(用来设定excel得sheet得内容等信息)
ExportParams deptExportParams = new ExportParams();
// 设置sheet得名称
deptExportParams.setSheetName("员工报表1");
// 创建sheet1使用得map
Map<String, Object> deptExportMap = new HashMap<>();
// title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
deptExportMap.put("title", deptExportParams);
// 模版导出对应得实体类型
deptExportMap.put("entity", DeptUtil.class);
// sheet中要填充得数据
deptExportMap.put("data", exportList);
ExportParams empExportParams = new ExportParams();
empExportParams.setSheetName("员工报表2");
// 创建sheet2使用得map
Map<String, Object> empExportMap = new HashMap<>();
empExportMap.put("title", empExportParams);
empExportMap.put("entity", DeptUtil.class);
empExportMap.put("data", exportList);
// 将sheet1、sheet2、sheet3使用得map进行包装
List<Map<String, Object>> sheetsList = new ArrayList<>();
sheetsList.add(deptExportMap);
sheetsList.add(empExportMap);
// 执行方法
workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
fileName = URLEncoder.encode("员工报表导出", "UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workBook.write(outputStream);
outputStream.flush();
byte[] byteArray = outputStream.toByteArray();
excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length);
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if(workBook != null) {
try {
workBook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "success";
}
三、效果展示
有一个问题就是如果sheet填充的数据源是一样的,那么第二个sheet的内容就会为空
不过一般上要实现多sheet页展示,内容应该是不一样的,这里只是作为实现效果展示,就采用了同一个数据获取源。
来源:https://blog.csdn.net/baidu_36821021/article/details/85216855
标签:Java,EasyPoi,多sheet,导出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中const,readonly和static关键字的用法介绍
2021-08-17 15:33:45
WebSocket实现Web聊天室功能
2023-11-27 06:10:52
![](https://img.aspxhome.com/file/2023/9/60189_0s.jpg)
Java引用传递和值传递栈内存与堆内存的指向操作
2021-05-30 04:43:37
![](https://img.aspxhome.com/file/2023/9/79229_0s.jpg)
IntelliJ IDEA maven 构建简单springmvc项目(图文教程)
2021-09-13 05:40:40
![](https://img.aspxhome.com/file/2023/3/69333_0s.jpg)
Java数据结构及算法实例:朴素字符匹配 Brute Force
2022-01-10 15:03:40
android实现banner轮播图无限轮播效果
2023-09-09 02:08:19
![](https://img.aspxhome.com/file/2023/2/93262_0s.gif)
c#用for语句输出一个三角形的方法
2023-12-17 05:46:53
![](https://img.aspxhome.com/file/2023/6/68086_0s.jpg)
Unity3D Shader实现贴图切换效果
2021-07-10 00:31:20
![](https://img.aspxhome.com/file/2023/9/77229_0s.gif)
java增强for循环的实现方法
2023-12-07 16:42:53
springboot 在idea中实现热部署的方法
2022-10-24 22:27:19
![](https://img.aspxhome.com/file/2023/8/70518_0s.png)
基于jstl 标签的使用介绍
2021-10-01 13:48:36
TextBox获取输入焦点时自动全选的实现方法
2022-01-01 02:22:49
C#实现读写ini文件类实例
2023-09-06 18:47:00
java 一个类实现两个接口的案例
2023-08-09 12:24:35
![](https://img.aspxhome.com/file/2023/0/58160_0s.jpg)
springMVC实现图形验证码(kaptcha)代码实例
2022-02-14 22:02:14
java求最大公约数与最小公倍数的方法示例
2023-01-23 03:28:58
![](https://img.aspxhome.com/file/2023/6/87366_0s.png)
springboot做代理分发服务+代理鉴权的实现过程
2021-06-28 03:22:14
![](https://img.aspxhome.com/file/2023/7/71597_0s.png)
Java8中的 Lambda表达式教程
2023-10-13 01:32:29
Spring超详细讲解面向对象到面向切面
2021-12-28 15:15:57
![](https://img.aspxhome.com/file/2023/2/89472_0s.png)
Java遗传算法之冲出迷宫
2022-01-12 21:34:58