SpringBoot整合EasyExcel進行大數據處理的方法詳解
EasyExcel
我用過Poi和EasyPoi這些工具總體來說:
- POI 優(yōu)點我覺得自由,但是迎來的就是復雜度,和大數據量時候性能的缺點
- EasyPoi基于POI 的二次封裝,解決了大部分的常用場景,簡化了代碼,但是特別復雜表格處理還是不行,而且性能的話和poi差不多,簡單來說就是簡化了Poi的操作,少些點代碼
下面來說說今天的主角EasyExcel,這個項目是阿里巴巴開發(fā)的開源的,專門針對大數據批量處理,比如100萬+的Excel數據這種,會比以上幾款要快很多并且性能上也不會太占用系統(tǒng)的資源,但是不好的地方就是,處理不了復雜的表單 ,不能像poi那么自由,所以有得有失,基本日常所需都能辦到,特殊場景在可以使用模板的方式,或者使用poi也行
主流操作的excel格式
下面這種分組的也能讀取,但是需要跳過前兩行的標題
下面演示,基礎的入門讀和寫案例, 需要對數據進行特殊處理,多Sheet,或者同步等需要參考文檔,基礎會了看文檔就簡單了
需要的Maven
<dependencies> <!-- 開發(fā)web 項目和啟動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>
基礎讀案例
操作的excel
實體類
@Data @Builder @AllArgsConstructor @NoArgsConstructor public class EmployeesEntity { @ExcelProperty(index = 0) private Integer no; //工號 @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)聽器
一般是異步讀取,可以指定同步讀取數據(看文檔)
public class EmployeesListener extends AnalysisEventListener<EmployeesEntity> { /** * 這個每一條數據解析都會來調用 * * @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("解析到一條數據:"+JSON.toJSONString(data)); } /** * 所有數據解析完成了 都會來調用 * * @param context */ @Override public void doAfterAllAnalysed(AnalysisContext context) { } }
測試
@Test public void get(){ File file = new File("./src/main/resources/大客戶部-薪酬表.xlsx"); String absolutePath = file.getAbsolutePath(); // // 這里 需要指定讀用哪個class去讀,然后讀取第一個sheet 文件流會自動關閉 EasyExcel.read(absolutePath, EmployeesEntity.class, new EmployeesListener()).sheet().doRead(); }
基礎寫案例
寫可以指定Sheet進行寫,還可以指定列進行寫,還可以寫入圖片,簡單的合并單元格…下面教程默認寫入第一個Sheet中
實體類
@ExcelProperty("字符串標題") 列的標題
@Data public class DemoData { @ExcelProperty("字符串標題") private String string; @ExcelProperty("日期標題") private Date date; @ExcelProperty("數字標題") private Double doubleData; /** * 忽略這個字段 */ @ExcelIgnore private String ignore; }
測試
//生成模擬數據 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; } /** * 最簡單的寫 * <p>1. 創(chuàng)建excel對應的實體對象 參照{@link DemoData} * <p>2. 直接寫即可 */ @Test public void simpleWrite() { // 寫 File file = new File("./src/main/resources/DemoData.xlsx"); // 這里 需要指定寫用哪個class去寫,然后寫到第一個sheet,名字為模板 然后文件流會自動關閉 // 如果這里想使用03 則 傳入excelType參數即可 EasyExcel.write(file.getAbsolutePath(), DemoData.class).sheet("DemoData").doWrite(data()); }
Excel模板方式
一般特別復雜的excel,比如發(fā)票,等, 一般就需要使用模板的方式,如果使用代碼的話太復雜了
準備模塊
實體類
@Data public class FillData { private String name; private double number; }
測試
@Test public void simpleFill() { // 模板注意 用{} 來表示你要用的變量 如果本來就有"{","}" 特殊字符 用"\{","\}"代替 File templateFileName = new File("./src/main/resources/模板.xlsx"); File fill = new File("./src/main/resources/fillData.xlsx"); // 這里 會填充到第一個sheet, 然后文件流會自動關閉 FillData fillData = new FillData(); fillData.setName("張三"); fillData.setNumber(5.2); EasyExcel.write(fill.getAbsolutePath()).withTemplate(templateFileName.getAbsolutePath()).sheet().doFill(fillData); }
如果是統(tǒng)計表,而且標題是非常復雜的情況下,那么我們使用列表填充會很容易解決(自行看文檔)
到此這篇關于SpringBoot整合EasyExcel進行大數據處理的方法詳解的文章就介紹到這了,更多相關SpringBoot EasyExcel大數據處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA 中使用 ECJ 編譯出現 java.lang.IllegalArgumentException的錯誤問題
這篇文章主要介紹了IDEA 中使用 ECJ 編譯出現 java.lang.IllegalArgumentException問題 ,本文內容簡短給大家介紹的好,需要的朋友可以參考下2020-05-05Java中YYYY-MM-dd與yyyy-MM-dd的區(qū)別及跨年問題
YYYY-MM-dd可能會導致跨年周的日期被歸屬到錯誤的年份, yyyy-MM-dd總是表示實際的日歷年份,無論日期所在的周是否跨年,本文就來介紹一下兩者的區(qū)別,感興趣的可以了解一下2024-01-01spring cloud oauth2 feign 遇到的坑及解決
這篇文章主要介紹了spring cloud oauth2 feign 遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03spring注解如何為bean指定InitMethod和DestroyMethod
這篇文章主要介紹了spring注解如何為bean指定InitMethod和DestroyMethod,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Springboot 整合RabbitMq(用心看完這一篇就夠了)
這篇文章主要介紹了Springboot 整合RabbitMq(用心看完這一篇就夠了),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12