阿里在18年3月份左右发布easyexcel,刚发布就是打着“低内存”解决POI的oom的口号,本人在测试过程中发现相比poi,确实在使用的体验、操作简便性上有不少提升,感谢阿里团队的努力,官方给出的项目demp的github地址:https://github.com/alibaba/easyexcel
下面就easy excel操作excel的常用操作做简单总结如下
导入依赖jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beat1</version>
</dependency>
需求1:将一批数据写入到本地的excel中(或者从数据库读取数据并写到excel中)
为了演示方便,本例中以接口的形式对不同的功能和代码块进行封装和测试(下同)
接口代码:
/**
* 将excel写到本地磁盘
* @return
*/
@GetMapping("/doExportExcel")
public String doExportExcel() {
return exportService.exportExcelsData();
}
exportExcelsData代码:
写入到本地磁盘的路径,可以以参数的形式传入进来
//导出单个excel到本地
public String exportExcelsData(){
String filePath = "C:\\其他文件\\excel文件测试\\测试.xlsx";
ArrayList<StudentExcelProperty> data = new ArrayList<>();
for(int i = 0; i < 2000; i++){
data.add(excelToEntity(i));
}
ExcelUtils.writeWithTemplate(filePath,data);
return "success";
}
excelToEntity为将excel数据转换成一个实体对象,使用easy excel的实体转换,需要自定义一个实体类,并继承BaseRowModel
//封装解析excel的数据到实体对象中
public StudentExcelProperty excelToEntity(int i){
StudentExcelProperty studentExcelProperty = new StudentExcelProperty();
studentExcelProperty.setName("cmj" + i);
studentExcelProperty.setNumbo("A09b03" + i);
studentExcelProperty.setSchool(schoolNames.get(new Random().nextInt(schoolNames.size())));
return studentExcelProperty;
}
StudentExcelProperty 实体类:
@EqualsAndHashCode(callSuper = true)
@Data
public class StudentExcelProperty extends BaseRowModel {
/**
* value: 表头名称
* index: 列的号, 0表示第一列
*/
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ExcelProperty(value = "编号",index = 1)
private String numbo;
@ExcelProperty(value = "学校",index = 2)
private String school;
}
读取excel工具类
/**
* 读取excel工具类
*/
@Slf4j
public class ExcelUtils {
private static Sheet initSheet;
//自定义设置初始化sheet相关参数,可根据实际情形调整
static {
initSheet = new Sheet(1, 0);
initSheet.setSheetName("sheet");
initSheet.setAutoWidth(Boolean.TRUE);
}
/**
* 读取少于1000行数据
* @param filePath 文件绝对路径
* @return
*/
public static List<Object> readLessThan1000Row(String filePath){
return readLessThan1000RowBySheet(filePath,null);
}
/**
* 读小于1000行数据, 带样式
* filePath 文件绝对路径
* initSheet :
* sheetNo: sheet页码,默认为1
* headLineMun: 从第几行开始读取数据,默认为0, 表示从第一行开始读取
* clazz: 返回数据List<Object> 中Object的类名
*/
public static List<Object> readLessThan1000RowBySheet(String filePath, Sheet sheet){
if(!StringUtils.hasText(filePath)){
return null;
}
sheet = sheet != null ? sheet : initSheet;
InputStream fileStream = null;
try {
fileStream = new FileInputStream(filePath);
return EasyExcelFactory.read(fileStream, sheet);
} catch (FileNotFoundException e) {
log.info("找不到文件或文件路径错误, 文件:{}", filePath);
}finally {
try {
if(fileStream != null){
fileStream.close();
}
} catch (IOException e) {
log.info("excel文件读取失败, 失败原因:{}", e);
}
}
return null;
}
/**
* 读大于1000行数据
* @param filePath 文件觉得路径
* @return
*/
public static List<Object> readMoreThan1000Row(String filePath){
return readMoreThan1000RowBySheet(filePath,null);
}
/**
* 读大于1000行数据, 带样式
* @param filePath 文件觉得路径
* @return
*/
public static List<Object> readMoreThan1000RowBySheet(String filePath, Sheet sheet){
if(!StringUtils.hasText(filePath)){
return null;
}
sheet = sheet != null ? sheet : initSheet;
InputStream fileStream = null;
try {
fileStream = new FileInputStream(filePath);
ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(fileStream, sheet, excelListener);
return excelListener.getDatas();
} catch (FileNotFoundException e) {
log.error("找不到文件或文件路径错误, 文件:{}", filePath);
}finally {
try {
if(fileStream != null){
fileStream.close();
}
} catch (IOException e) {
log.error("excel文件读取失败, 失败原因:{}", e);
}
}
return null;
}
/**
* 生成excle
* @param filePath 绝对路径
* @param data 数据源
* @param head 表头
*/
public static void writeBySimple(String filePath, List<List<Object>> data, List<String> head){
writeSimpleBySheet(filePath,data,head,null);
}
/**
* 生成excle
* @param filePath 绝对路径,
* @param data 数据源
* @param sheet excle页面样式
* @param head 表头
*/
public static void writeSimpleBySheet(String filePath, List<List<Object>> data, List<String> head, Sheet sheet){
sheet = (sheet != null) ? sheet : initSheet;
if(head != null){
List<List<String>> list = new ArrayList<>();
head.forEach(h -> list.add(Collections.singletonList(h)));
sheet.setHead(list);
}
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new FileOutputStream(filePath);
writer = EasyExcelFactory.getWriter(outputStream);
writer.write1(data,sheet);
} catch (FileNotFoundException e) {
log.error("找不到文件或文件路径错误, 文件:{}", filePath);
}finally {
try {
if(writer != null){
writer.finish();
}
if(outputStream != null){
outputStream.close();
}
} catch (IOException e) {
log.error("excel文件导出失败, 失败原因:{}", e);
}
}
}
/**
* 生成excle
* @param filePath 绝对路径,
* @param data 数据源
*/
public static void writeWithTemplate(String filePath, List<? extends BaseRowModel> data){
writeWithTemplateAndSheet(filePath,data,null);
}
/**
* 生成excle
* @param filePath 绝对路径,
* @param data 数据源
* @param sheet excle页面样式
*/
public static void writeWithTemplateAndSheet(String filePath, List<? extends BaseRowModel> data, Sheet sheet){
if(CollectionUtils.isEmpty(data)){
return;
}
sheet = (sheet != null) ? sheet : initSheet;
sheet.setClazz(data.get(0).getClass());
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new FileOutputStream(filePath);
writer = EasyExcelFactory.getWriter(outputStream);
writer.write(data,sheet);
} catch (FileNotFoundException e) {
log.error("找不到文件或文件路径错误, 文件:{}", filePath);
}finally {
try {
if(writer != null){
writer.finish();
}
if(outputStream != null){
outputStream.close();
}
} catch (IOException e) {
log.error("excel文件导出失败, 失败原因:{}", e);
}
}
}
/**
* 生成多Sheet的excle
* @param filePath 绝对路径,
* @param multipleSheelPropetys
*/
public static void writeWithMultipleSheel(String filePath,List<MultipleSheelPropety> multipleSheelPropetys){
if(CollectionUtils.isEmpty(multipleSheelPropetys)){
return;
}
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new FileOutputStream(filePath);
writer = EasyExcelFactory.getWriter(outputStream);
for (MultipleSheelPropety multipleSheelPropety : multipleSheelPropetys) {
Sheet sheet = multipleSheelPropety.getSheet() != null ? multipleSheelPropety.getSheet() : initSheet;
if(!CollectionUtils.isEmpty(multipleSheelPropety.getData())){
sheet.setClazz(multipleSheelPropety.getData().get(0).getClass());
}
writer.write(multipleSheelPropety.getData(), sheet);
}
} catch (FileNotFoundException e) {
log.error("找不到文件或文件路径错误, 文件:{}", filePath);
}finally {
try {
if(writer != null){
writer.finish();
}
if(outputStream != null){
outputStream.close();
}
} catch (IOException e) {
log.error("excel文件导出失败, 失败原因:{}", e);
}
}
}
@Data
public static class MultipleSheelPropety{
private List<? extends BaseRowModel> data;
private Sheet sheet;
}
/**
* 解析监听器,
* 每解析一行会回调invoke()方法。
* 整个excel解析结束会执行doAfterAllAnalysed()方法
*/
@Getter
@Setter
public static class ExcelListener extends AnalysisEventListener {
private List<Object> datas = new ArrayList<>();
/**
* 逐行解析
* object : 当前行的数据
*/
@Override
public void invoke(Object object, AnalysisContext context) {
//当前行
// context.getCurrentRowNum()
if (object != null) {
datas.add(object);
}
}
/**
* 解析完所有数据后会调用该方法
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
//解析结束销毁不用的资源
}
}
}
工具类代码说明,在easy excel的API中,官方在原来的POI读取excel的操作上进行了优化,比如他们认为超过1000行的excel数据为比较大的excel数据,那么再使用同步的方法读取时就不合适了,因此给出了EasyExcelFactory.read和EasyExcelFactory.readBySax两个方法,其实底层有一个同步读取也异步读取的思想在里面
测试上面的接口:http://localhost:8083/doExportExcel
3000条数据,用了将近1秒左右的时间就完成了,效率还是不错的
补充说明:关于读取数据并写入到excel的其他操作,比如想把单张超过10万条数据的表导出到excel中,可以考虑导出到多个sheet页中,也可以使用工具类中的方法尝试
需求2:读取本地的数据到excel并输出到浏览器(模拟导出excel)
接口代码:
//下载excel到浏览器
@GetMapping("/downLoadExcel")
public void downLoadExcel(HttpServletResponse response) {
exportService.downLoadExcel(response);
}
downLoadExcel方法:
/**
* 导出excel到浏览器
*/
public void downLoadExcel(HttpServletResponse response) {
ServletOutputStream out = null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
try {
//获取sheet
Sheet sheet1 = getSheet("学生信息表");
//得到要填充的数据
ArrayList<StudentExcelProperty> data = getSheetData();
writer.write(data, sheet1);
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
writer.finish();
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
获取sheet:
private Sheet getSheet(String sheetName) {
Sheet sheet1 = new Sheet(1, 0, StudentExcelProperty.class);
sheet1.setSheetName(sheetName == null || sheet1.equals("") ? sheetName :"第一个sheet");
return sheet1;
}
测试:http://localhost:8083/doExportExcel,效果如下所示
需求3:读取excel数据并导入到数据库
上面提到读取excel的时候,如果数据量比较大的时候,可以使用readBySax这个异步的读取方法,以便减少内存的开销,在这种场景中,通常需要继承easy excel提供的一个监听器(listener),
public class ExcelModelListener extends AnalysisEventListener<StudentExcelProperty> {
/**
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<StudentExcelProperty> list = new ArrayList<>();
private static int count = 1;
@Override
public void invoke(StudentExcelProperty data, AnalysisContext context) {
System.out.println("解析到一条数据:{ "+ data.getName() +" }");
list.add(data);
count ++;
if (list.size() >= BATCH_COUNT) {
saveData(count );
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//saveData( count );
System.out.println("所有数据解析完成!");
System.out.println(" count :" + count);
}
/**
* 具体执行读取到的excel数据进行存储的逻辑,比如写入到mysql等操作
*/
private void saveData(int count) {
System.out.println("{ "+ count +" }条数据,开始存储数据库!" + list.size());
System.out.println("存储数据库成功!");
}
}
上述监听器类中,需要继承AnalysisEventListener,重写里面的两个方法,通过这两个方法,可以对读取excel数据的过程中,对读取到的数据行进行相关的业务逻辑操作,比如校验等
接口代码:
/**
* 异步读取excel数据并导入到数据库
*/
@GetMapping("/readExcelAsync")
public void readExcelAsync( ) {
readService.readExcelAsync();
}
readExcelAsync:
//异步读取excel数据
public void readExcelAsync() {
// 读取 excel 表格的路径
String readPath = "C:\\其他文件\\excel文件测试\\测试.xlsx";
try {
Sheet sheet = new Sheet(1, 1, StudentExcelProperty.class);
EasyExcelFactory.readBySax(new FileInputStream(readPath), sheet, new ExcelModelListener());
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error("读取本地文件失败");
}
}
由于是异步读取,该方法的主线程中获取不到读取的数据,需要在listener类中做处理,比如进行数据库的写入操作等
测试:http://localhost:8083/readExcelAsync
整个花了不到2秒的时间,解析出了3000条数据,可以看出easy excel的强劲之处了吧,个人猜想,底层应该是使用了线程池之类的技术,有兴趣的同学可以翻开源码学习学习!本篇到此结束,最后感谢观看!