SpringBoot整合EasyExcel進(jìn)行大數(shù)據(jù)處理的方法詳解
EasyExcel
我用過(guò)Poi和EasyPoi這些工具總體來(lái)說(shuō):
- POI 優(yōu)點(diǎn)我覺(jué)得自由,但是迎來(lái)的就是復(fù)雜度,和大數(shù)據(jù)量時(shí)候性能的缺點(diǎn)
- EasyPoi基于POI 的二次封裝,解決了大部分的常用場(chǎng)景,簡(jiǎn)化了代碼,但是特別復(fù)雜表格處理還是不行,而且性能的話和poi差不多,簡(jiǎn)單來(lái)說(shuō)就是簡(jiǎn)化了Poi的操作,少些點(diǎn)代碼
下面來(lái)說(shuō)說(shuō)今天的主角EasyExcel,這個(gè)項(xiàng)目是阿里巴巴開(kāi)發(fā)的開(kāi)源的,專門(mén)針對(duì)大數(shù)據(jù)批量處理,比如100萬(wàn)+的Excel數(shù)據(jù)這種,會(huì)比以上幾款要快很多并且性能上也不會(huì)太占用系統(tǒng)的資源,但是不好的地方就是,處理不了復(fù)雜的表單 ,不能像poi那么自由,所以有得有失,基本日常所需都能辦到,特殊場(chǎng)景在可以使用模板的方式,或者使用poi也行
主流操作的excel格式

下面這種分組的也能讀取,但是需要跳過(guò)前兩行的標(biāo)題

下面演示,基礎(chǔ)的入門(mén)讀和寫(xiě)案例, 需要對(duì)數(shù)據(jù)進(jìn)行特殊處理,多Sheet,或者同步等需要參考文檔,基礎(chǔ)會(huì)了看文檔就簡(jiǎn)單了
需要的Maven
<dependencies>
<!-- 開(kāi)發(fā)web 項(xiàng)目和啟動(dòng)Springboot必須添加的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.3.graal</version>
<scope>compile</scope>
</dependency>
</dependencies>
基礎(chǔ)讀案例
操作的excel

實(shí)體類
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EmployeesEntity {
@ExcelProperty(index = 0)
private Integer no; //工號(hào)
@ExcelProperty(index = 1)
private String name;
@ExcelProperty(index = 2)
private Double fund;
@ExcelProperty(index = 3)
private Double postSalary;
@ExcelProperty(index = 4)
private Double performanceOf;
@ExcelProperty(index = 5)
private Double allWork;
@ExcelProperty(index = 6)
private Double violations;
@ExcelProperty(index = 7)
private Double traffic;
@ExcelProperty(index = 8)
private Double communication;
}
讀取監(jiān)聽(tīng)器
一般是異步讀取,可以指定同步讀取數(shù)據(jù)(看文檔)
public class EmployeesListener extends AnalysisEventListener<EmployeesEntity> {
/**
* 這個(gè)每一條數(shù)據(jù)解析都會(huì)來(lái)調(diào)用
*
* @param data
* one row value. Is is same as {@link AnalysisContext#readRowHolder()}
* @param context
*/
@Override
public void invoke(EmployeesEntity data, AnalysisContext context) {
System.out.println("解析到一條數(shù)據(jù):"+JSON.toJSONString(data));
}
/**
* 所有數(shù)據(jù)解析完成了 都會(huì)來(lái)調(diào)用
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}
測(cè)試
@Test
public void get(){
File file = new File("./src/main/resources/大客戶部-薪酬表.xlsx");
String absolutePath = file.getAbsolutePath();
// // 這里 需要指定讀用哪個(gè)class去讀,然后讀取第一個(gè)sheet 文件流會(huì)自動(dòng)關(guān)閉
EasyExcel.read(absolutePath, EmployeesEntity.class, new EmployeesListener()).sheet().doRead();
}

基礎(chǔ)寫(xiě)案例
寫(xiě)可以指定Sheet進(jìn)行寫(xiě),還可以指定列進(jìn)行寫(xiě),還可以寫(xiě)入圖片,簡(jiǎn)單的合并單元格…下面教程默認(rèn)寫(xiě)入第一個(gè)Sheet中
實(shí)體類
@ExcelProperty("字符串標(biāo)題") 列的標(biāo)題
@Data
public class DemoData {
@ExcelProperty("字符串標(biāo)題")
private String string;
@ExcelProperty("日期標(biāo)題")
private Date date;
@ExcelProperty("數(shù)字標(biāo)題")
private Double doubleData;
/**
* 忽略這個(gè)字段
*/
@ExcelIgnore
private String ignore;
}
測(cè)試
//生成模擬數(shù)據(jù)
private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
/**
* 最簡(jiǎn)單的寫(xiě)
* <p>1. 創(chuàng)建excel對(duì)應(yīng)的實(shí)體對(duì)象 參照{(diào)@link DemoData}
* <p>2. 直接寫(xiě)即可
*/
@Test
public void simpleWrite() {
// 寫(xiě)
File file = new File("./src/main/resources/DemoData.xlsx");
// 這里 需要指定寫(xiě)用哪個(gè)class去寫(xiě),然后寫(xiě)到第一個(gè)sheet,名字為模板 然后文件流會(huì)自動(dòng)關(guān)閉
// 如果這里想使用03 則 傳入excelType參數(shù)即可
EasyExcel.write(file.getAbsolutePath(), DemoData.class).sheet("DemoData").doWrite(data());
}

Excel模板方式
一般特別復(fù)雜的excel,比如發(fā)票,等, 一般就需要使用模板的方式,如果使用代碼的話太復(fù)雜了
準(zhǔn)備模塊

實(shí)體類
@Data
public class FillData {
private String name;
private double number;
}
測(cè)試
@Test
public void simpleFill() {
// 模板注意 用{} 來(lái)表示你要用的變量 如果本來(lái)就有"{","}" 特殊字符 用"\{","\}"代替
File templateFileName = new File("./src/main/resources/模板.xlsx");
File fill = new File("./src/main/resources/fillData.xlsx");
// 這里 會(huì)填充到第一個(gè)sheet, 然后文件流會(huì)自動(dòng)關(guān)閉
FillData fillData = new FillData();
fillData.setName("張三");
fillData.setNumber(5.2);
EasyExcel.write(fill.getAbsolutePath()).withTemplate(templateFileName.getAbsolutePath()).sheet().doFill(fillData);
}
如果是統(tǒng)計(jì)表,而且標(biāo)題是非常復(fù)雜的情況下,那么我們使用列表填充會(huì)很容易解決(自行看文檔)
到此這篇關(guān)于SpringBoot整合EasyExcel進(jìn)行大數(shù)據(jù)處理的方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot EasyExcel大數(shù)據(jù)處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合EasyExcel實(shí)現(xiàn)大規(guī)模數(shù)據(jù)的并行導(dǎo)出與壓縮下載
- SpringBoot利用EasyExcel實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)
- 使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的教程詳解
- SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)
- SpringBoot種如何使用?EasyExcel?實(shí)現(xiàn)自定義表頭導(dǎo)出并實(shí)現(xiàn)數(shù)據(jù)格式化轉(zhuǎn)換
相關(guān)文章
快速入門(mén)介紹Java中強(qiáng)大的String.format()
這篇文章主要給大家介紹了如何快速入門(mén)介紹Java中強(qiáng)大的String.format()的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
IDEA 中使用 ECJ 編譯出現(xiàn) java.lang.IllegalArgumentException的錯(cuò)誤問(wèn)題
這篇文章主要介紹了IDEA 中使用 ECJ 編譯出現(xiàn) java.lang.IllegalArgumentException問(wèn)題 ,本文內(nèi)容簡(jiǎn)短給大家介紹的好,需要的朋友可以參考下2020-05-05
Java中YYYY-MM-dd與yyyy-MM-dd的區(qū)別及跨年問(wèn)題
YYYY-MM-dd可能會(huì)導(dǎo)致跨年周的日期被歸屬到錯(cuò)誤的年份, yyyy-MM-dd總是表示實(shí)際的日歷年份,無(wú)論日期所在的周是否跨年,本文就來(lái)介紹一下兩者的區(qū)別,感興趣的可以了解一下2024-01-01
spring cloud oauth2 feign 遇到的坑及解決
這篇文章主要介紹了spring cloud oauth2 feign 遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
spring注解如何為bean指定InitMethod和DestroyMethod
這篇文章主要介紹了spring注解如何為bean指定InitMethod和DestroyMethod,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java調(diào)用WebService服務(wù)的三種方式總結(jié)
雖然WebService這個(gè)框架已經(jīng)過(guò)時(shí),但是有些公司還在使用,在調(diào)用他們的服務(wù)的時(shí)候就不得不面對(duì)各種問(wèn)題,本篇文章總結(jié)了最近我調(diào)用?WebService的心路歷程,3種方式可以分別嘗試,需要的朋友可以參考下2023-08-08
Springboot 整合RabbitMq(用心看完這一篇就夠了)
這篇文章主要介紹了Springboot 整合RabbitMq(用心看完這一篇就夠了),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringBoot 枚舉類型的自動(dòng)轉(zhuǎn)換的實(shí)現(xiàn)
一般我們?cè)跀?shù)據(jù)庫(kù)都會(huì)定義數(shù)值型的枚舉常量,不管是序列化還是反序列化都是需要我們手動(dòng)去轉(zhuǎn)換成枚舉類型的,本文主要介紹了Spring Boot 枚舉類型的自動(dòng)轉(zhuǎn)換,感興趣的可以了解一下2022-03-03

