一行程式碼完成 JAVA 的 EXCEL 讀寫——EasyExcel 的方法封裝
前段時間在 github 上發現了阿里的 EasyExcel 專案,覺得挺不錯的,就寫了一個簡單的方法封裝,做到只用一個函式就完成 Excel 的匯入或者導。剛好前段時間更新修復了一些 BUG,就把我的這個封裝分享出來,請多多指教
附上原始碼: ofollow,noindex">github.com/HowieYuan/e…
EasyExcel
EasyExcel 的 github 地址: github.com/alibaba/eas… EasyExcel 的官方介紹:

可以看到 EasyExcel 最大的特點就是使用記憶體少,當然現在它的功能還比較簡單,能夠面對的複雜場景比較少,不過基本的讀寫完全可以滿足。
一. 依賴
首先是新增該專案的依賴,目前的版本是 1.0.2
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.0.2</version> </dependency> 複製程式碼
二. 需要的類

1. ExcelUtil
工具類,可以直接呼叫該工具類的方法完成 Excel 的讀或者寫
2. ExcelListener
監聽類,可以根據需要與自己的情況,自定義處理獲取到的資料,我這裡只是簡單地把資料新增到一個 List 裡面。
public class ExcelListener extends AnalysisEventListener { //自定義用於暫時儲存data。 //可以通過例項獲取該值 private List<Object> datas = new ArrayList<>(); /** * 通過 AnalysisContext 物件還可以獲取當前 sheet,當前行等資料 */ @Override public void invoke(Object object, AnalysisContext context) { //資料儲存到list,供批量處理,或後續自己業務邏輯處理。 datas.add(object); //根據自己業務做處理 doSomething(object); } private void doSomething(Object object) { } @Override public void doAfterAllAnalysed(AnalysisContext context) { /* datas.clear(); 解析結束銷燬不用的資源 */ } public List<Object> getDatas() { return datas; } public void setDatas(List<Object> datas) { this.datas = datas; } } 複製程式碼
3. ExcelWriterFactroy
用於匯出多個 sheet 的 Excel,通過多次呼叫 write 方法寫入多個 sheet
4. ExcelException
捕獲相關 Exception
三. 讀取 Excel
讀取 Excel 時只需要呼叫 ExcelUtil.readExcel()
方法
@RequestMapping(value = "readExcel", method = RequestMethod.POST) public Object readExcel(MultipartFile excel) { return ExcelUtil.readExcel(excel, new ImportInfo()); } 複製程式碼
其中 excel 是 MultipartFile 型別的檔案物件,而 new ImportInfo() 是該 Excel 所對映的實體物件,需要繼承 BaseRowModel 類,如:
public class ImportInfo extends BaseRowModel { @ExcelProperty(index = 0) private String name; @ExcelProperty(index = 1) private String age; @ExcelProperty(index = 2) private String email; /* 作為 excel 的模型對映,需要 setter 方法 */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } 複製程式碼
作為對映實體類,通過 @ExcelProperty 註解與 index 變數可以標註成員變數所對映的列,同時不可缺少 setter 方法
四. 匯出 Excel
1. 匯出的 Excel 只擁有一個 sheet
只需要呼叫 ExcelUtil.writeExcelWithSheets()
方法:
@RequestMapping(value = "writeExcel", method = RequestMethod.GET) public void writeExcel(HttpServletResponse response) throws IOException { List<ExportInfo> list = getList(); String fileName = "一個 Excel 檔案"; String sheetName = "第一個 sheet"; ExcelUtil.writeExcel(response, list, fileName, sheetName, new ExportInfo()); } 複製程式碼
fileName,sheetName 分別是匯出檔案的檔名和 sheet 名,new ExportInfo() 為匯出資料的對映實體物件,list 為匯出資料。
對於對映實體類,可以根據需要通過 @ExcelProperty 註解自定義表頭,當然同樣需要繼承 BaseRowModel 類,如:
public class ExportInfo extends BaseRowModel { @ExcelProperty(value = "姓名" ,index = 0) private String name; @ExcelProperty(value = "年齡",index = 1) private String age; @ExcelProperty(value = "郵箱",index = 2) private String email; @ExcelProperty(value = "地址",index = 3) private String address; } 複製程式碼
value 為列名,index 為列的序號
如果需要複雜一點,可以實現如下圖的效果:

對應的實體類寫法如下:
public class MultiLineHeadExcelModel extends BaseRowModel { @ExcelProperty(value = {"表頭1","表頭1","表頭31"},index = 0) private String p1; @ExcelProperty(value = {"表頭1","表頭1","表頭32"},index = 1) private String p2; @ExcelProperty(value = {"表頭3","表頭3","表頭3"},index = 2) private int p3; @ExcelProperty(value = {"表頭4","表頭4","表頭4"},index = 3) private long p4; @ExcelProperty(value = {"表頭5","表頭51","表頭52"},index = 4) private String p5; @ExcelProperty(value = {"表頭6","表頭61","表頭611"},index = 5) private String p6; @ExcelProperty(value = {"表頭6","表頭61","表頭612"},index = 6) private String p7; @ExcelProperty(value = {"表頭6","表頭62","表頭621"},index = 7) private String p8; @ExcelProperty(value = {"表頭6","表頭62","表頭622"},index = 8) private String p9; } 複製程式碼
2. 匯出的 Excel 擁有多個 sheet
呼叫 ExcelUtil.writeExcelWithSheets()
處理第一個 sheet,之後呼叫 write()
方法依次處理之後的 sheet,最後使用 finish()
方法結束
public void writeExcelWithSheets(HttpServletResponse response) throws IOException { List<ExportInfo> list = getList(); String fileName = "一個 Excel 檔案"; String sheetName1 = "第一個 sheet"; String sheetName2 = "第二個 sheet"; String sheetName3 = "第三個 sheet"; ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName1, new ExportInfo()) .write(list, sheetName2, new ExportInfo()) .write(list, sheetName3, new ExportInfo()) .finish(); } 複製程式碼
write 方法的引數為當前 sheet 的 list 資料,當前 sheet 名以及對應的對映類