Easy POI学习笔记
文章目录
1 Easy POI简介
1.1 基本简介
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,
Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法。
Easy POI是在Apache POI基础上封装的,目的是使用更加方便。用来替换原有Apache POI的。
不如Apache poi那么自定义,不如jxl那么多标签,但是我们就是写的少,写的少
Easypoi的目标是什么
Easypoi的目标不是替代poi,而是让一个不懂导入导出的快速使用poi完成Excel和word的各种操作,
而不是看很多api才可以完成这样工作
从Easy POI依赖的jar包可以看出,Easy POI是在Apache POI基础上进行的封装。
为什么会写Easypoi
以前的以前(岁月真TMD的快)我虽然写了不少代码但还是很少写poi,然后跳到一家公司之后就和业务人员聊上了,来这个需要个报表,这个报表样式是这样的,这个表头是这样的,就这样我写了大量的poi代码,每次都是大量的篇幅,copy to copy,无聊的一逼,然后加入了jeecg,jeecg中有一个小的工具类,虽然我也不知道是谁写的,然是可以用注解搞定最简单的导出,突然豁然开朗,我可以完善,让我从报表的苦海当中脱离出来,这样我花了一周的时间做了第一个版本支持导入导出放到了jeecg,发现还是不错的,慢慢的用的人越来越多,我就把这块独立出来了,再然后有人提出了模板,然后就加入了模板功能,提出了word的需求,加入了word的功能,后来工作忙了虽然没再参与jeecg,但还是一直维持这easypoi的更新,根据见识的增长也不断的重构这代码,直到现在。
1.2 功能
Excel自适应xls和xlsx两种格式,word只支持docx模式
1.Excel导入
- 注解导入
- Map导入
- 大数据量导入sax模式
- 导入文件保存
- 文件校验
- 字段校验
2.Excel导出
- 注解导出
- 模板导出
- html导出
3.Excel转html
4.word导出
5.pdf导出
2 Easy POI环境搭建
maven依赖
<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>
3 Easy POI注解
easypoi起因就是Excel的导入导出,最初的模板是实体和Excel的对应,
model--row,filed--col 这样利用注解我们可以和容易做到excel到导入导出
经过一段时间发展,现在注解有5个类分别是
3.1 @ExcelTarget
@ExcelTarget 这个是作用于最外层的对象,描述这个对象的id,以便支持一个对象可以针对不同导出做出不同处理
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
}
3.2 @Excel
@Excel 作用到filed上面,是对Excel一列的一个描述
#常用属性
name: [String] 生成Excel表格中的列名
width: [double] 指定导出Excel时列的宽度
format: [String] 设置字段的时间格式
orderNum: [String] 指定生成Excel中列的顺序,安装数字自然顺序排序,从0开始
suffix: [String] 后缀
replace: [String[]] 替换 replace = {"未激活_0","激活_1"} 0换成未激活,1换成 激活
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
3.3 @ExcelIgnore
@ExcelIgnore 和名字一样表示这个字段被忽略跳过这个导导出
不在Excel中导出,这一字段不导出到Excel的列中
指定不导出的字段
@ExcelIgnore
private String name; //name字段忽略,不会导出
3.4 @ExcelEntity
@ExcelEntity 一对一
@ExcelEntity //标识一对多
private Card card;
3.5 @ExcelCollection
@ExcelCollection 表示一个集合,主要针对一对多的导出,比如一个老师对应多个科目,科目就可以用集合表示
一对多
#常用属性
name: [String] 定义集合列名
orderNum: [int] 用来指定导出Excel集合内列的顺序
@ExcelCollection(name="订单列表")
private List<Order> orderList;
4 Easy POI写入Excel
4.1 写入基本数据
注意:导出Excel的对象必须实现对象序列号接口
User类
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 说明:
* 作者:汤观林
* 日期:2021年12月28日 00时
*/
@Data
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
@Excel(name="编号")
private String id;
@Excel(name="姓名")
private String name;
@Excel(name="年龄",suffix = "%")
private int age;
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
}
WriteExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 说明:导出基本数据
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class WriteExcel {
public static void main(String[] args) throws IOException {
List<User> userList = getUserList();
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);
FileOutputStream fos = new FileOutputStream("H:\\010Easy POI\\用户信息.xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
System.out.println("写入成功");
}
//查询所有记录
public static List<User> getUserList(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setId(String.valueOf(i));
user.setName("小陈_"+i);
user.setAge(10+i);
user.setBir(new Date());
if(i%2==0){
user.setStatus("1");
}else{
user.setStatus("0");
}
userList.add(user);
}
return userList;
}
}
导出结果:
H:\010Easy POI\用户信息.xls
4.2 写入指定字段
@ExcelIgnore //该字段不导出
private String name;
User类
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 说明:user类
* 作者:汤观林
* 日期:2021年12月28日 00时
*/
@Data
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
@Excel(name="编号")
private String id;
@ExcelIgnore //该字段不导出
private String name;
@Excel(name="年龄",suffix = "%")
private int age;
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
}
WriteExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 说明:导出指定字段
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class WriteExcel {
public static void main(String[] args) throws IOException {
List<User> userList = getUserList();
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);
FileOutputStream fos = new FileOutputStream("H:\\010Easy POI\\用户信息.xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
System.out.println("写入成功");
}
//查询所有记录
public static List<User> getUserList(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setId(String.valueOf(i));
user.setName("小陈_"+i);
user.setAge(10+i);
user.setBir(new Date());
if(i%2==0){
user.setStatus("1");
}else{
user.setStatus("0");
}
userList.add(user);
}
return userList;
}
}
运行结果:
H:\010Easy POI\用户信息.xls
4.3 写入出list集合
@ExcelIgnore
private List<String> habbys; //爱好
@Excel(name="爱好",width = 35.0)
private String habbyStr;
public String getHabbyStr() {
StringBuffer sb =new StringBuffer();
habbys.forEach(e->{
sb.append(e).append("、");
});
return sb.toString();
}
User类
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 说明:
* 作者:汤观林
* 日期:2021年12月28日 00时
*/
@Data
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
@Excel(name="编号")
private String id;
@Excel(name="姓名")
private String name;
@Excel(name="年龄",suffix = "%")
private int age;
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
@ExcelIgnore
private List<String> habbys; //爱好
@Excel(name="爱好",width = 35.0)
private String habbyStr;
public String getHabbyStr() {
StringBuffer sb =new StringBuffer();
habbys.forEach(e->{
sb.append(e).append("、");
});
return sb.toString();
}
}
WriteExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 说明:导出list集合
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class WriteExcel {
public static void main(String[] args) throws IOException {
List<User> userList = getUserList();
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);
FileOutputStream fos = new FileOutputStream("H:\\010Easy POI\\用户信息.xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
System.out.println("写入成功");
}
//查询所有记录
public static List<User> getUserList(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setId(String.valueOf(i));
user.setName("小陈_"+i);
user.setAge(10+i);
user.setBir(new Date());
if(i%2==0){
user.setStatus("1");
user.setHabbys(Arrays.asList("打篮球","看书","看片"));
}else{
user.setStatus("0");
user.setHabbys(Arrays.asList("喝酒","抽烟","烫头"));
}
userList.add(user);
}
return userList;
}
}
运行结果:
H:\010Easy POI\用户信息.xls
4.4 写入对象中含有其他对象
@ExcelEntity //标识一对一
private Card card;
Card.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;
import java.io.Serializable;
/**
* 说明:身份证
* 作者:汤观林
* 日期:2021年12月28日 14时
*/
@ExcelTarget("card")
@Data
public class Card implements Serializable {
@Excel(name="身份证号码",width = 20.0)
private String no;
@Excel(name="籍贯",width = 35.0)
private String address;
}
User.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 说明:
* 作者:汤观林
* 日期:2021年12月28日 00时
*/
@Data
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
@Excel(name="编号")
private String id;
@Excel(name="姓名")
private String name;
@Excel(name="年龄",suffix = "%")
private int age;
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
@ExcelIgnore
private List<String> habbys; //爱好
@Excel(name="爱好",width = 35.0)
private String habbyStr;
@ExcelEntity //标识一对一
private Card card;
public String getHabbyStr() {
StringBuffer sb =new StringBuffer();
habbys.forEach(e->{
sb.append(e).append("、");
});
return sb.toString();
}
}
WriteExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 说明:导出对象中含有其他对象
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class WriteExcel {
public static void main(String[] args) throws IOException {
List<User> userList = getUserList();
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);
FileOutputStream fos = new FileOutputStream("H:\\010Easy POI\\用户信息.xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
System.out.println("写入成功");
}
//查询所有记录
public static List<User> getUserList(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setId(String.valueOf(i));
user.setName("小陈_"+i);
user.setAge(10+i);
user.setBir(new Date());
//身份信息
Card card = new Card();
card.setNo("13423232323232323");
card.setAddress("北京市朝阳区国贸大厦3层507A");
user.setCard(card);
if(i%2==0){
user.setStatus("1");
user.setHabbys(Arrays.asList("打篮球","看书","看片"));
}else{
user.setStatus("0");
user.setHabbys(Arrays.asList("喝酒","抽烟","烫头"));
}
userList.add(user);
}
return userList;
}
}
运行结果:
H:\010Easy POI\用户信息.xls
4.5 写入一对多关系
@ExcelCollection(name="订单列表")
private List<Order> orderList;
Order.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
/**
* 说明: 订单
* 作者:汤观林
* 日期:2021年12月28日 14时
*/
@Data
@AllArgsConstructor
@ExcelTarget("order")
public class Order implements Serializable {
@Excel(name="订单编号",width = 25.0)
private String no;
@Excel(name="订单名称",width = 15.0)
private String name;
}
User.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 说明:
* 作者:汤观林
* 日期:2021年12月28日 00时
*/
@Data
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
@Excel(name="编号")
private String id;
@Excel(name="姓名")
private String name;
@Excel(name="年龄",suffix = "%")
private int age;
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
@ExcelIgnore
private List<String> habbys; //爱好
@Excel(name="爱好",width = 35.0)
private String habbyStr;
@ExcelCollection(name="订单列表")
private List<Order> orderList;
public String getHabbyStr() {
StringBuffer sb =new StringBuffer();
habbys.forEach(e->{
sb.append(e).append("、");
});
return sb.toString();
}
}
WriteExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 说明:导出一对多关系
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class WriteExcel {
public static void main(String[] args) throws IOException {
List<User> userList = getUserList();
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);
FileOutputStream fos = new FileOutputStream("H:\\010Easy POI\\用户信息.xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
System.out.println("写入成功");
}
//查询所有记录
public static List<User> getUserList(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setId(String.valueOf(i));
user.setName("小陈_"+i);
user.setAge(10+i);
user.setBir(new Date());
//订单信息
List<Order> orderList = new ArrayList<>();
orderList.add(new Order("12","超短"));
orderList.add(new Order("13","超短连衣裙"));
orderList.add(new Order("14","连衣裙"));
user.setOrderList(orderList);
if(i%2==0){
user.setStatus("1");
user.setHabbys(Arrays.asList("打篮球","看书","看片"));
}else{
user.setStatus("0");
user.setHabbys(Arrays.asList("喝酒","抽烟","烫头"));
}
userList.add(user);
}
return userList;
}
}
运行结果:
H:\010Easy POI\用户信息.xls
4.6 写入图片
往往随着业务不断变化,可能需要在导出Excel时将图片信息也一并导出,如商品图标,用户头像信息等数据。
这个时候easy poi该如何处理呢?
@Excel(name="头像",width = 30.0,height = 30.0,type = 2) //type=2为图片类型
private String photo; //头像信息
User.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 说明:
* 作者:汤观林
* 日期:2021年12月28日 00时
*/
@Data
@ExcelTarget("user") //唯一标识
public class User implements Serializable {
@Excel(name="编号")
private String id;
@Excel(name="姓名")
private String name;
@Excel(name="年龄",suffix = "%")
private int age;
@Excel(name = "生日",width=30.0,format = "yyyy-MM-dd hh:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
@ExcelIgnore
private List<String> habbys; //爱好
@Excel(name="爱好",width = 35.0)
private String habbyStr;
@Excel(name="头像",width = 30.0,height = 30.0,type = 2) //type=2为图片类型
private String photo; //头像信息
public String getHabbyStr() {
StringBuffer sb =new StringBuffer();
habbys.forEach(e->{
sb.append(e).append("、");
});
return sb.toString();
}
}
WriteExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* 说明:导出图片
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class WriteExcel {
public static void main(String[] args) throws IOException {
List<User> userList = getUserList();
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList);
FileOutputStream fos = new FileOutputStream("H:\\010Easy POI\\用户信息.xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
System.out.println("写入成功");
}
//查询所有记录
public static List<User> getUserList(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setId(String.valueOf(i));
user.setName("小陈_"+i);
user.setAge(10+i);
user.setBir(new Date());
if(i%2==0){
user.setStatus("1");
user.setHabbys(Arrays.asList("打篮球","看书","看片"));
}else{
user.setStatus("0");
user.setHabbys(Arrays.asList("喝酒","抽烟","烫头"));
}
//设置头像路径
user.setPhoto("H:\\010Easy POI\\image.jpg");
userList.add(user);
}
return userList;
}
}
运行结果:
H:\010Easy POI\用户信息.xls
4.7 大数据量写入
大数据导出是当我们的导出数量在几万,到几百万的数据时,一次从数据库查询这么多数据加载到内存然后写入会对我们的内存和CPU都产生压力,这个时候需要我们像分页一样处理导出分段写入Excel缓解Excel的压力。
注意:最好大量数据进行分页处理,每次导出数据最好不要超过1万条记录。
/**
* 参数1:exportParams 导出配置对象
* 参数2:导出的类型
* 参数3:导出数据集合
*/
ExportParams exportParams = new ExportParams();
exportParams.setTitle("用户信息列表"); //表格标题
exportParams.setSheetName("用户信息"); //sheet名
ExcelExportUtil.exportBigExcel(exportParams, User.class, userList)
代码:
@Test
public void bigDataExport() throws Exception {
List<MsgClient> list = new ArrayList<MsgClient>();
Workbook workbook = null;
Date start = new Date();
ExportParams params = new ExportParams("大数据测试", "测试");
for (int i = 0; i < 1000000; i++) { //一百万数据量
MsgClient client = new MsgClient();
client.setBirthday(new Date());
client.setClientName("小明" + i);
client.setClientPhone("18797" + i);
client.setCreateBy("JueYue");
client.setId("1" + i);
client.setRemark("测试" + i);
MsgClientGroup group = new MsgClientGroup();
group.setGroupName("测试" + i);
client.setGroup(group);
list.add(client);
if(list.size() == 10000){
workbook = ExcelExportUtil.exportBigExcel(params, MsgClient.class, list);
list.clear();
}
}
ExcelExportUtil.closeExportBigExcel();
System.out.println(new Date().getTime() - start.getTime());
File savefile = new File("D:/excel/");
if (!savefile.exists()) {
savefile.mkdirs();
}
FileOutputStream fos = new FileOutputStream("D:/excel/ExcelExportBigData.bigDataExport.xlsx");
workbook.write(fos);
fos.close();
}
生成的Excel数据
CPU和内存:
多次测试用时统计,速度还是可以接受的,
数据量 | 用时 | 文件大小 | 列数 |
---|---|---|---|
100W | 16.4s | 24.3MB | 5 |
100W | 15.9s | 24.3MB | 5 |
200W | 29.5s | 48.5MB | 5 |
100W | 30.8s | 37.8MB | 10 |
200W | 58.7s | 76.1MB | 10 |
5 Easy POI读取Excel
//参数说明
mportParams importParams = new ImportParams();
importParams.setTitleRows(1); //标题
importParams.setHeadRows(1); //表头
importParams.setStartSheetIndex(1); //从哪个sheet开始读
importParams.setSheetNum(4); //从第1个读到第4个sheet
importParams.setImportFields(new String[]{"编号","状态"}); //校验字段 是否合法的Excel
List<Employee> employeeList = ExcelImportUtil.importExcel(fis,Employee.class,importParams);
代码:
H:\010Easy POI\用户信息.xlsx
Employee.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 说明:
* 作者:汤观林
* 日期:2021年12月28日 17时
*/
@Data
@ExcelTarget("employee")
public class Employee implements Serializable {
@Excel(name="编号")
private String id;
@Excel(name="姓名")
private String name;
@Excel(name="年龄")
private int age;
@Excel(name="生日",format = "yyyy-MM-dd HH:mm:ss")
private Date bir;
@Excel(name="状态",replace = {"激活_1","锁定_0"})
private String status;
}
ReadExcel.java
package com.tangguanlin.easypoi;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import java.io.FileInputStream;
import java.util.List;
/**
* 说明:读取Excel
* 作者:汤观林
* 日期:2021年12月27日 23时
*/
public class ReadExcel {
public static void main(String[] args) throws Exception {
/**
* 参数1:导入Excel文件流
* 参数2:导入类型
* 参数3:导入的配置对象
*/
FileInputStream fis = new FileInputStream("H:\\010Easy POI\\用户信息.xlsx");
ImportParams importParams = new ImportParams();
importParams.setTitleRows(1); //标题行数
importParams.setHeadRows(1); //表头行数
importParams.setStartSheetIndex(0); //从哪个sheet开始读
//importParams.setSheetNum(4); //从第1个读到第4个sheet
importParams.setImportFields(new String[]{"编号","状态"}); //校验字段
List<Employee> employeeList = ExcelImportUtil.importExcel(fis,Employee.class,importParams);
for(Employee employee:employeeList){
System.out.println(employee);
}
}
}
运行结果:
Employee(id=0, name=小陈_0, age=11, bir=Tue Dec 28 04:26:24 CST 2021, status=1)
Employee(id=1, name=小陈_1, age=12, bir=Wed Dec 29 04:26:27 CST 2021, status=0)
Employee(id=2, name=小陈_2, age=13, bir=Thu Dec 30 04:26:28 CST 2021, status=1)
Employee(id=3, name=小陈_3, age=14, bir=Mon Dec 27 04:26:24 CST 2021, status=0)
Employee(id=4, name=小陈_4, age=15, bir=Sun Dec 26 04:26:24 CST 2021, status=1)