Spring Boot + EasyExcel 導(dǎo)入導(dǎo)出,好用到爆,可以扔掉 POI 了!
來(lái)源:www.jianshu.com/
p/4e6aa6342b33
一、EasyExcel 二、常用注解 三、依賴 四、監(jiān)聽(tīng) 五、接口導(dǎo)入Excel 六、接口導(dǎo)出Excel 七、本地導(dǎo)入、本地導(dǎo)出

一、EasyExcel
EasyExcel是阿里巴巴開(kāi)源poi插件之一,主要解決了poi框架使用復(fù)雜,sax解析模式不容易操作,數(shù)據(jù)量大起來(lái)容易OOM,解決了POI并發(fā)造成的報(bào)錯(cuò)。
主要解決方式:通過(guò)解壓文件的方式加載,一行一行的加載,并且拋棄樣式字體等不重要的數(shù)據(jù),降低內(nèi)存的占用。
EasyExcel優(yōu)勢(shì)
注解式自定義操作。 輸入輸出簡(jiǎn)單,提供輸入輸出過(guò)程的接口 支持一定程度的單元格合并等靈活化操作
推薦下自己做的 Spring Boot 的實(shí)戰(zhàn)項(xiàng)目:
https://github.com/YunaiV/ruoyi-vue-pro
二、常用注解
@ExcelProperty
指定當(dāng)前字段對(duì)應(yīng)excel中的那一列??梢愿鶕?jù)名字或者Index去匹配。當(dāng)然也可以不寫(xiě),默認(rèn)第一個(gè)字段就是index=0,以此類(lèi)推。千萬(wàn)注意,要么全部不寫(xiě),要么全部用index,要么全部用名字去匹配。千萬(wàn)別三個(gè)混著用,除非你非常了解源代碼中三個(gè)混著用怎么去排序的。
@ExcelIgnore
默認(rèn)所有字段都會(huì)和excel去匹配,加了這個(gè)注解會(huì)忽略該字段
@DateTimeFormat
日期轉(zhuǎn)換,用String去接收excel日期格式的數(shù)據(jù)會(huì)調(diào)用這個(gè)注解。里面的value參照java.text.SimpleDateFormat
@NumberFormat
數(shù)字轉(zhuǎn)換,用String去接收excel數(shù)字格式的數(shù)據(jù)會(huì)調(diào)用這個(gè)注解。里面的value參照java.text.DecimalFormat
@ExcelIgnoreUnannotated
默認(rèn)不加ExcelProperty 的注解的都會(huì)參與讀寫(xiě),加了不會(huì)參與
推薦下自己做的 Spring Cloud 的實(shí)戰(zhàn)項(xiàng)目:
https://github.com/YunaiV/onemall
三、依賴
?
<dependency>
???<groupId>com.alibabagroupId>
???<artifactId>easyexcelartifactId>
???<version>2.1.4version>
dependency>
<dependency>
???<groupId>javax.servletgroupId>
???<artifactId>javax.servlet-apiartifactId>
???<version>4.0.1version>
???<scope>providedscope>
dependency>
<dependency>
???<groupId>com.alibabagroupId>
???<artifactId>fastjsonartifactId>
???<version>1.2.47version>
dependency>
四、監(jiān)聽(tīng)
?/**
?*?EasyExcel?導(dǎo)入監(jiān)聽(tīng)
?*/
public?class?ExcelListener?extends?AnalysisEventListener?{
????//可以通過(guò)實(shí)例獲取該值
????private?List五、接口導(dǎo)入Excel
try?{
????????//獲取文件名
????????String?filename?=?file.getOriginalFilename();
????????//獲取文件流
????????InputStream?inputStream?=?file.getInputStream();
????????//實(shí)例化實(shí)現(xiàn)了AnalysisEventListener接口的類(lèi)
????????ExcelListener?listener?=?new?ExcelListener();
????????//傳入?yún)?shù)
????????ExcelReader?excelReader?=?new?ExcelReader(inputStream,?ExcelTypeEnum.XLS,?null,?listener);
????????//讀取信息
????????excelReader.read(new?Sheet(1,?0,?Test.class));
????????//獲取數(shù)據(jù)
????????List?list?=?listener.getDatas();
????????if?(list.size()?>?1)?{
????????????for?(int?i?=?0;?i?????????????????Testobj?=?(Test)?list.get(i);
????????????????JSONObject?jo?=?new?JSONObject();
????????????}
????????}
????}?catch?(Exception?e)?{
????????System.out.println(e.getMessage());
????}
六、接口導(dǎo)出Excel
HttpServletResponse response, HttpServletRequest request
?try?{
????String?filenames?=?"111111";
????String?userAgent?=?request.getHeader("User-Agent");
????if?(userAgent.contains("MSIE")?||?userAgent.contains("Trident"))?{
????????filenames?=?URLEncoder.encode(filenames,?"UTF-8");
????}?else?{
????????filenames?=?new?String(filenames.getBytes("UTF-8"),?"ISO-8859-1");
????}
????response.setContentType("application/vnd.ms-exce");
????response.setCharacterEncoding("utf-8");
????response.addHeader("Content-Disposition",?"filename="?+?filenames?+?".xlsx");
????EasyExcel.write(response.getOutputStream(),?Test.class).sheet("sheet").doWrite(testList);
}?catch?(Exception?e)?{
}
七、本地導(dǎo)入、本地導(dǎo)出
List?testList?=?new?ArrayList<>();
try?{
????String?strUrl?=?"C:\\Users\\Administrator\\Desktop\\json.xlsx";
????File?multipartFile?=?new?File(strUrl);
????InputStream?inputStream?=?new?FileInputStream(multipartFile);
????//實(shí)例化實(shí)現(xiàn)了AnalysisEventListener接口的類(lèi)
????ExcelListener?listener?=?new?ExcelListener();
????//傳入?yún)?shù)
????ExcelReader?excelReader?=?new?ExcelReader(inputStream,?ExcelTypeEnum.XLS,?null,?listener);
????//讀取信息
????excelReader.read(new?Sheet(1,?0,?Test.class));
????//獲取數(shù)據(jù)
????List?list?=?listener.getDatas();
????if?(list.size()?>?1)?{
????????for?(int?i?=?0;?i?????????????Testobj?=?(Test)?list.get(i);
????????}
????}
}?catch?(Exception?e)?{
????System.out.println(e.getMessage());
}
try?{
????String?strUrl?=?"C:\\Users\\Administrator\\Desktop\\json"+System.currentTimeMillis()+".xlsx";
????EasyExcel.write(strUrl,Test.class).sheet("sheet").doWrite(testList);
}?catch?(Exception?e)?{
}
以上就是EasyExcel的基礎(chǔ)使用過(guò)程,歡迎點(diǎn)贊關(guān)注交流。
?關(guān)注公眾號(hào):Java后端編程,回復(fù)下面關(guān)鍵字?
要Java學(xué)習(xí)完整路線,回復(fù)??路線?
缺Java入門(mén)視頻,回復(fù):?視頻?
要Java面試經(jīng)驗(yàn),回復(fù)??面試?
缺Java項(xiàng)目,回復(fù):?項(xiàng)目?
進(jìn)Java粉絲群:?加群?
PS:如果覺(jué)得我的分享不錯(cuò),歡迎大家隨手點(diǎn)贊、在看。
(完) 加我"微信"?獲取一份 最新Java面試題資料 請(qǐng)備注:666,不然不通過(guò)~
最近好文
1、GitHub 近兩萬(wàn) Star,可一鍵生成前后端代碼
最近面試BAT,整理一份面試資料《Java面試BAT通關(guān)手冊(cè)》,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。 獲取方式:關(guān)注公眾號(hào)并回復(fù)?java?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。 明天見(jiàn)(??ω??)??
