EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel工具。
官网:https://www.yuque.com/easyexcel/doc/easyexcel
github地址:https://github.com/alibaba/easyexcel
Java解析、生成Excel比较有名的框架有Apache poi、jxl,但他们都存在一个严重的问题就是非常的耗内存。
EasyExcel 重写了poi,使一个3M的excel只需要几M内存,并且再大的excel不会出现内存溢出。
64M内存1分钟内读取75M(46W行25列)的Excel。
修改pom,添加依赖
<dependencies>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>3.0.5version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
package com.czxy.zx.demo01;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.Date;
/**
* Created by liangtong.
*/
@Data
public class Student {
@ExcelProperty("编号")
private String id;
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("电话")
private String telephone;
@ExcelProperty("邮箱")
private String email;
@ExcelProperty("生日")
private Date brithday;
}
package com.czxy.zx.demo01;
import org.junit.jupiter.api.Test;
/**
* @author 桐叔
* @email [email protected]
*/
public class TestExcel {
/**
* 获得根路径
* @return
*/
public String getPath() {
return this.getClass().getResource("/").getPath();
}
@Test
public void testPath() {
// 测试文件路径
String path = getPath() + "student_demo.xls";
System.out.println(path);
}
}
excel 属于 office组件一个软件
存在若干版本,大体上划分2种情况,2007前的,2007年后的
IV65536
,256列(IV)XFD1048576
,16384列(XFD) /**
* 准备数据
* @return
*/
private List<Student> getData(){
List<Student> list = new ArrayList<Student>();
for(int i = 0 ; i < 10 ; i ++){
Student student = new Student();
student.setId("stu" + i);
student.setName("wang" + i);
student.setAge( 18 + i );
student.setTelephone("1361234" + i);
student.setEmail("wang" + i + "@czxy.com");
student.setBrithday(new Date());
list.add(student);
}
return list;
}
@Test
public void testWrite(){
String file = getPath() + "student_demo.xls";
//EasyExcel.write(位置,对象).sheet("表名").doWrite(数据);
EasyExcel.write(file,Student.class).sheet("班级").doWrite(getData());
}
处理类:
AnalysisEventListener
接口package com.czxy.zx.demo01;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.czxy.zx.domain.Student;
/**
* @author 桐叔
* @email [email protected]
*/
public class StudentListener extends AnalysisEventListener<Student> {
@Override
public void invoke(Student student, AnalysisContext analysisContext) {
System.out.println(student);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("解析完成");
}
}
测试
@Test
public void testRead(){
String file = getPath() + "student_demo.xls";
//EasyExcel.read(文件, 封装对象, 处理类).sheet("表").doRead();
EasyExcel.read(file, Student.class, new StudentListener()).sheet("班级").doRead();
}
package com.czxy.zx.demo02;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
import java.util.Date;
/**
* @author 桐叔
* @email [email protected]
*/
@Data
@ContentRowHeight(20)
@HeadRowHeight(20) //行高
@ColumnWidth(25) //列宽
public class Student2 {
@ExcelProperty("编号")
private String id;
@ExcelProperty({"基本信息","姓名"}) //复制表头
private String name;
@ExcelProperty({"基本信息","年龄"})
private Integer age;
@ExcelProperty("电话")
private String telephone;
@ExcelProperty("邮箱")
private String email;
@ExcelProperty("生日")
@DateTimeFormat("yyyy年MM月dd日")
private Date brithday;
}
package com.czxy.zx.demo02;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.czxy.zx.demo01.Student;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
*/
public class TestExcel2 {
/**
* 获得根路径
* @return
*/
public String getPath() {
return this.getClass().getResource("/").getPath();
}
/**
* 准备数据
* @return
*/
private List<Student2> getData(Integer flag){
List<Student2> list = new ArrayList<Student2>();
for(int m = 0 ; m < 10 ; m ++){
String i = "" + flag + m ;
Student2 student = new Student2();
student.setId("stu" + i);
student.setName("wang" + i);
student.setAge( 18 );
student.setTelephone("1361234" + i);
student.setEmail("wang" + i + "@czxy.com");
student.setBrithday(new Date());
list.add(student);
}
return list;
}
@Test
public void testMoreSheetWrite(){
String file = getPath() + "student_demo2.xls";
ExcelWriter excelWriter = EasyExcel.write(file).build();
for (int i = 0; i < 5; i++) {
WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).head(Student2.class).build();
// 分页去数据库查询数据 这里可以去数据库查询每一页的数据
List<Student2> data = getData(i);
excelWriter.write(data, writeSheet);
}
excelWriter.finish();
}
}
具有缓存处理类
package com.czxy.zx.demo02;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by liangtong.
*/
public class Student2Listener extends AnalysisEventListener<Student2> {
// 批量操作数
private static final int BATCH_COUNT = 10;
// 用于缓存信息
private List<Student2> cache = new ArrayList<Student2>();
public void invoke(Student2 student, AnalysisContext analysisContext) {
//保存学生信息
cache.add(student);
if(cache.size() >= BATCH_COUNT){
// 保存数据
saveData();
}
}
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//最后的不够 BATCH_COUNT 倍数
saveData();
}
private void saveData() {
// 集合不为空
if(! cache.isEmpty()) {
// 处理缓存数据
System.out.println(cache);
// 清空缓存
cache.clear();
}
}
}
读操作
@Test
public void testMoreRead(){
String file = getPath() + "student_demo2.xls";
//EasyExcel.read(文件, 封装对象, 处理类).sheet("表").doRead();
ExcelReader excelReader = EasyExcel.read(file, Student2.class, new Student2Listener()).build();
// 确定需要解析的sheet
for (int i = 0; i < 5; i++) {
ReadSheet readSheet = EasyExcel.readSheet("模板" + i).build();
excelReader.read(readSheet);
}
excelReader.finish();
}