阿里EasyExcel入門——讀取excel

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<Object>  data = new ArrayList<Object>();
	
	@Override
	public void doAfterAllAnalysed(AnalysisContext arg0) {
		data.clear();//解析結束後銷燬資源,(疑惑,clear()好像不能測地回收內存。)
	}

	@Override
	public void invoke(Object object, AnalysisContext context) {
        System.out.println("當前行:"+context.getCurrentRowNum());
        
        data.add(object);//數據存儲到list,供批量處理,或者自己後續的業務處理
        doSomething(object);//根據自己的是業務而處理(就是用service層保存到數據庫了)
        

		
	}
	
    public void doSomething(Object object){
    	//入庫操作
    }

}

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內存都會出現內存溢出的問題。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章