EasyExcel是18年3月份左右发布的,刚刚发布就是打着“低内存”解决POI的oom的口号。经过一年版本迭代现在最新版是1.1.2-beta4
GitHub地址https://github.com/alibaba/easyexcel
maven地址:https://mvnrepository.com/artifact/com.alibaba/easyexcel
easyexcel是在poi基础的上做的开发所有需要poi的jar包。下面的案例是用easyexcel 1.1.2-beta4和poi3.17
全部的jar包:推荐使用maven,现在好多项目都是maven项目,学习一个新想技术,光是找jar就节省了不少时间。
github上介绍的03版和07本的excel读取方法一样,只需要把流作为参数写入方法中和指定ExcelTypeEnum就行。poi就麻烦了。需要自己读取excel的格式再生成对应的workbook。
读取大于1000行数据返回List extend BaseRowModel>(实际开发中,根本就无法确定excel的行数,直接用大于1000行的方法)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.greatchn.htfsweb.listen.ExcelListener;
public class Read07 {
public static void main(String[] args) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream("D:\\天津云成科技有限公司_凭证.xlsx"));
try {
//读取07版的excel
ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(in, new Sheet(1, 0, PZ.class),excelListener);
} catch (Exception e) {
// TODO: handle exception
}finally {
in.close();
}
}
}
注意:文件流还要转成buffer,读取文件要用BufferedInputStream来读取,只FileInputStream会报错。
ExcelListener是解析监听器,每解析一行都会回调invoke(),整个excel解析结束执行 ExcelListener中的doAfterAllAnalysed()方法
package com.greatchn.htfsweb.listen;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
public class ExcelListener extends AnalysisEventListener {
private List
Java模型pz的写法:注意要继承BaseRowModel
package com.greatchn.htfsweb.controller;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
public class PZ extends BaseRowModel {
@ExcelProperty(index = 0)
private String ztdm;
@ExcelProperty(index = 1)
private String pznm;
@ExcelProperty(index = 2)
private String kjnd;
@ExcelProperty(index = 3)
private String kjqj;
@ExcelProperty(index = 4)
private String pzlx;
@ExcelProperty(index = 5)
private String pzbh;
@ExcelProperty(index = 6)
private String pzrq;
@ExcelProperty(index = 7)
private String fj;
@ExcelProperty(index = 8)
private String flbh;
@ExcelProperty(index = 9)
private String zy;
@ExcelProperty(index = 10)
private String kmbh;
@ExcelProperty(index = 11)
private String jje;
@ExcelProperty(index = 12)
private String dje;
@ExcelProperty(index = 13)
private String dj;
@ExcelProperty(index = 14)
private String dw;
@ExcelProperty(index = 15)
private String jsl;
@ExcelProperty(index = 16)
private String dsl;
@ExcelProperty(index = 17)
private String wbbh;
@ExcelProperty(index = 18)
private String hl;
@ExcelProperty(index = 19)
private String jwb;
@ExcelProperty(index = 20)
private String dwb;
@ExcelProperty(index = 21)
private String jsfs;
@ExcelProperty(index = 22)
private String jsdh;
@ExcelProperty(index = 23)
private String ywrq;
@ExcelProperty(index = 24)
private String dfdw;
@ExcelProperty(index = 25)
private String xjllid;
@ExcelProperty(index = 26)
private String FZ1KHBH;
@ExcelProperty(index = 27)
private String FZ2GYSBH;
@ExcelProperty(index = 28)
private String FZ3BMBH;
@ExcelProperty(index = 29)
private String FZ4RYBH;
@ExcelProperty(index = 30)
private String FZ5CHBH;
@ExcelProperty(index = 31)
private String ZDYFZBH1;
@ExcelProperty(index = 32)
private String ZDYFZBH2;
}
实际项目中读取的excel已经到了7w多行了。在原生的poi上解析excel无论怎么配置tomcat内存都会出现内存溢出的问题。