Spring Boot + EasyExcel 導(dǎo)入導(dǎo)出,好用到爆!
來(lái)源:www.jianshu.com/p/4e6aa6342b33
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ò)程的接口 支持一定程度的單元格合并等靈活化操作
二、常用注解
@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ì)參與
三、依賴(lài)
??
<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)注交流。
END
推薦閱讀 一鍵生成Springboot & Vue項(xiàng)目!【畢設(shè)神器】
Java可視化編程工具系列(一)
順便給大家推薦一個(gè)GitHub項(xiàng)目,這個(gè) GitHub 整理了上千本常用技術(shù)PDF,絕大部分核心的技術(shù)書(shū)籍都可以在這里找到,
GitHub地址:https://github.com/javadevbooks/books
Gitee地址:https://gitee.com/javadevbooks/books
電子書(shū)已經(jīng)更新好了,你們需要的可以自行下載了,記得點(diǎn)一個(gè)star,持續(xù)更新中..
評(píng)論
圖片
表情

