學(xué)術(shù)會(huì)議網(wǎng)站怎么做seo描述快速排名
一、介紹
在上篇文章中,我們介紹了 easypoi 工具實(shí)現(xiàn) excel 文件的導(dǎo)入導(dǎo)出。
本篇我們繼續(xù)深入介紹另一款更優(yōu)秀的 excel 工具庫(kù):easyexcel 。
二、easyexcel
easyexcel 是阿里巴巴開源的一款 excel 解析工具,底層邏輯也是基于 apache poi 進(jìn)行二次開發(fā)的。不同的是,再讀寫數(shù)據(jù)的時(shí)候,采用 sax 模式一行一行解析,在并發(fā)量很大的情況下,依然能穩(wěn)定運(yùn)行!
下面,我們就一起來(lái)了解一下這款新起之秀!
4.1、首先添加依賴包
<dependencies><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.6</version></dependency><!--常用工具庫(kù)--><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>29.0-jre</version></dependency>
</dependencies>
4.2、采用注解導(dǎo)出導(dǎo)入
easyexcel 同樣也支持采用注解方式進(jìn)行導(dǎo)出、導(dǎo)入!
首先,我們創(chuàng)建一個(gè)實(shí)體類UserEntity
,其中@ExcelProperty
注解表示導(dǎo)出文件的頭部信息。
public class UserEntity {@ExcelProperty(value = "姓名")private String name;@ExcelProperty(value = "年齡")private int age;@DateTimeFormat("yyyy-MM-dd HH:mm:ss")@ExcelProperty(value = "操作時(shí)間")private Date time;//set、get省略
}
接著,我們來(lái)編寫導(dǎo)出服務(wù)!
public static void main(String[] args) {List<UserEntity> dataList = new ArrayList<>();for (int i = 0; i < 10; i++) {UserEntity userEntity = new UserEntity();userEntity.setName("張三" + i);userEntity.setAge(20 + i);userEntity.setTime(new Date(System.currentTimeMillis() + i));dataList.add(userEntity);}EasyExcel.write("/Users/hello/Documents/easyexcel-user1.xls", UserEntity.class).sheet("用戶信息").doWrite(dataList);
}
導(dǎo)出的文件預(yù)覽如下:
對(duì)應(yīng)的導(dǎo)入操作,也很簡(jiǎn)單,源碼如下:
public static void main(String[] args) {String filePath = "/Users/hello/Documents/easyexcel-user1.xls";List<DemoData> list = EasyExcel.read(filePath).head(UserEntity.class).sheet().doReadSync();System.out.println(JSONArray.toJSONString(list));
}
運(yùn)行程序,輸出結(jié)果如下:
[{"age":20,"name":"張三0","time":1616920360000},{"age":21,"name":"張三1","time":1616920360000},{"age":22,"name":"張三2","time":1616920360000},{"age":23,"name":"張三3","time":1616920360000},{"age":24,"name":"張三4","time":1616920360000},{"age":25,"name":"張三5","time":1616920360000},{"age":26,"name":"張三6","time":1616920360000},{"age":27,"name":"張三7","time":1616920360000},{"age":28,"name":"張三8","time":1616920360000},{"age":29,"name":"張三9","time":1616920360000}]
4.3、自定義數(shù)據(jù)結(jié)構(gòu)導(dǎo)出導(dǎo)入
easyexcel 同樣也支持自定義數(shù)據(jù)結(jié)構(gòu)導(dǎo)出導(dǎo)入excel。
- 自定義數(shù)據(jù)導(dǎo)出 excel
public static void main(String[] args) {//表頭List<List<String>> headList = new ArrayList<>();headList.add(Lists.newArrayList("姓名"));headList.add(Lists.newArrayList("年齡"));headList.add(Lists.newArrayList("操作時(shí)間"));//數(shù)據(jù)體List<List<Object>> dataList = new ArrayList<>();for (int i = 0; i < 10; i++) {List<Object> data = new ArrayList<>();data.add("張三" + i);data.add(20 + i);data.add(new Date(System.currentTimeMillis() + i));dataList.add(data);}EasyExcel.write("/Users/hello/Documents/easyexcel-user2.xls").head(headList).sheet("用戶信息").doWrite(dataList);
}
- 導(dǎo)入 excel
public static void main(String[] args) {String filePath = "/Users/panzhi/Documents/easyexcel-user2.xls";UserDataListener userDataListener = new UserDataListener();EasyExcel.read(filePath, userDataListener).sheet().doRead();System.out.println("表頭:" + JSONArray.toJSONString(userDataListener.getHeadList()));System.out.println("數(shù)據(jù)體:" + JSONArray.toJSONString(userDataListener.getDataList()));
}
運(yùn)行程序,輸出結(jié)果如下:
表頭:[{0:"姓名",1:"年齡",2:"操作時(shí)間"}]
數(shù)據(jù)體:[{0:"張三0",1:"20",2:"2021-03-28 16:31:39"},{0:"張三1",1:"21",2:"2021-03-28 16:31:39"},{0:"張三2",1:"22",2:"2021-03-28 16:31:39"},{0:"張三3",1:"23",2:"2021-03-28 16:31:39"},{0:"張三4",1:"24",2:"2021-03-28 16:31:39"},{0:"張三5",1:"25",2:"2021-03-28 16:31:39"},{0:"張三6",1:"26",2:"2021-03-28 16:31:39"},{0:"張三7",1:"27",2:"2021-03-28 16:31:39"},{0:"張三8",1:"28",2:"2021-03-28 16:31:39"},{0:"張三9",1:"29",2:"2021-03-28 16:31:39"}]
更多的 api 操作可以訪問(wèn) easyexcel - 接口文檔
三、小結(jié)
總體來(lái)說(shuō),easypoi和easyexcel都是基于apache poi進(jìn)行二次開發(fā)的。
不同點(diǎn)在于:
1、easypoi 在讀寫數(shù)據(jù)的時(shí)候,優(yōu)先是先將數(shù)據(jù)寫入內(nèi)存,優(yōu)點(diǎn)是讀寫性能非常高,但是當(dāng)數(shù)據(jù)量很大的時(shí)候,會(huì)出現(xiàn)oom,當(dāng)然它也提供了 sax 模式的讀寫方式,需要調(diào)用特定的方法實(shí)現(xiàn)。
2、easyexcel 基于sax模式進(jìn)行讀寫數(shù)據(jù),不會(huì)出現(xiàn)oom情況,程序有過(guò)高并發(fā)場(chǎng)景的驗(yàn)證,因此程序運(yùn)行比較穩(wěn)定,相對(duì)于 easypoi 來(lái)說(shuō),讀寫性能稍慢!
easypoi 與 easyexcel 還有一點(diǎn)區(qū)別在于,easypoi 對(duì)定制化的導(dǎo)出支持非常的豐富,如果當(dāng)前的項(xiàng)目需求,并發(fā)量不大、數(shù)據(jù)量也不大,但是需要導(dǎo)出 excel 的文件樣式千差萬(wàn)別,那么我推薦你用 easypoi;反之,使用 easyexcel !
四、參考
1、apache poi - 接口文檔
2、easypoi - 接口文檔
3、easyexcel - 接口文檔
寫到最后
不會(huì)有人刷到這里還想白嫖吧?點(diǎn)贊對(duì)我真的非常重要!在線求贊。加個(gè)關(guān)注我會(huì)非常感激!
本文已整理到技術(shù)筆記中,此外,筆記內(nèi)容還涵蓋 Spring、Spring Boot/Cloud、Dubbo、JVM、集合、多線程、JPA、MyBatis、MySQL、微服務(wù)等技術(shù)棧。
需要的小伙伴可以點(diǎn)擊 技術(shù)筆記 獲取!