SpringBoot中EasyExcel實(shí)現(xiàn)execl導(dǎo)入導(dǎo)出
引言
在實(shí)際開發(fā)中,處理 Excel 文件是一個常見的需求。EasyExcel 是一個基于 Java 的開源庫,提供了簡單易用的 API,可以方便地讀取和寫入 Excel 文件。本文將介紹如何使用 EasyExcel 實(shí)現(xiàn) Excel 導(dǎo)入功能,以及一些相關(guān)的技巧和注意事項(xiàng)。
環(huán)境搭建
在開始之前,我們需要準(zhǔn)備好 EasyExcel 的環(huán)境。首先,我們需要在項(xiàng)目中引入 EasyExcel 的相關(guān)依賴。在本文中,我們使用 Maven 作為依賴管理工具。在 pom.xml
文件中添加以下依賴:
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.1</version> </dependency>
準(zhǔn)備 Excel 文件
在開始編寫代碼之前,我們需要準(zhǔn)備一個包含數(shù)據(jù)的 Excel 文件,作為導(dǎo)入的示例。確保 Excel 文件的結(jié)構(gòu)和數(shù)據(jù)與實(shí)體類的字段對應(yīng)。
創(chuàng)建實(shí)體類
在使用 EasyExcel 進(jìn)行導(dǎo)入時,我們需要創(chuàng)建一個與 Excel 數(shù)據(jù)結(jié)構(gòu)相匹配的實(shí)體類。實(shí)體類的字段應(yīng)與 Excel 文件的列對應(yīng)。使用 @ExcelProperty
注解來標(biāo)識字段與 Excel 列的映射關(guān)系,以及一些其他注解來設(shè)置字段的屬性。舉個例子如下:
@AllArgsConstructor @NoArgsConstructor @Data public class OperationLog { // @JsonFormat(shape = JsonFormat.Shape.STRING) @ExcelProperty(value = "id", converter = StringConverter.class,index = 0) private String id; //日志id @ExcelProperty(value = "操作人",index = 1) private String userCode; //操作人 @ExcelProperty(value = "操作ip",index = 2) private String ip; //操作ip @ExcelProperty(value = "操作類型",index = 3) private String type; //操作類型 @ExcelProperty(value = "操作名稱",index = 4) private String description; //操作名稱 @ExcelProperty(value = "操作模塊",index = 5) private String model; //操作模塊 @ExcelProperty(value = "操作時間",index = 6) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ExcelIgnore private String operationTime; //操作時間 @ExcelProperty(value = "操作結(jié)果",index = 7) private String result; //操作結(jié)果 }
自定義轉(zhuǎn)換器
有時,我們需要對 Excel 中的數(shù)據(jù)進(jìn)行特殊處理或轉(zhuǎn)換,以適應(yīng)實(shí)體類的字段類型。EasyExcel 允許我們自定義轉(zhuǎn)換器來實(shí)現(xiàn)這個功能。下面是一個示例
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; /** * @program: family-doctor * @author: 阿水 * @create: 2023-06-16 20:44 **/ public class StringConverter implements Converter<String> { @Override public Class<String> supportJavaTypeKey() { return String.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return cellData.getStringValue(); } @Override public CellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return new CellData<>(value); } }
編寫導(dǎo)入邏輯
現(xiàn)在我們開始編寫導(dǎo)入 Excel 的邏輯。以下是一個示例:
我通過的MongoDB的_id集合查詢出來數(shù)據(jù)寫入Excel,readExistingExcelData方法是先讀取之前再寫入,好像沒法直接追加 我目前如此操作 也可以完美實(shí)現(xiàn)后續(xù)添加~
/** * 根據(jù)ids批量導(dǎo)出 * @param ids * @return */ @Override public HttpResult<?> batchExportLogByIds(List<String> ids) { Query query = new Query(Criteria.where("_id").in(ids)); List<OperationLog> resultList = mongoTemplate.find(query, OperationLog.class); // 讀取已有的Excel文件內(nèi)容 List<OperationLog> existingList = readExistingExcelData(); // 合并新數(shù)據(jù)和已有數(shù)據(jù) List<OperationLog> mergedList = new ArrayList<>(existingList); mergedList.addAll(resultList); // 設(shè)置寫入文件夾地址和Excel文件名稱 String filename = "F:\\lps\\write.xlsx"; ExcelWriter excelWriter = null; try { excelWriter = EasyExcel.write(filename, OperationLog.class) .registerConverter(new StringConverter()) .build(); WriteSheet writeSheet = EasyExcel.writerSheet("日志").build(); excelWriter.write(mergedList, writeSheet); } catch (Exception e) { e.printStackTrace(); return new HttpResult<>().fail(); } finally { if (excelWriter != null) { excelWriter.finish(); } } return new HttpResult<>().ok(); } // 讀取已有的Excel文件內(nèi)容 private List<OperationLog> readExistingExcelData() { String filename = "F:\\lps\\write.xlsx"; List<OperationLog> existingList = new ArrayList<>(); try { ExcelReader excelReader = EasyExcel.read(filename, OperationLog.class, new OperationLogDataListener(existingList)).build(); ReadSheet readSheet = EasyExcel.readSheet("日志").build(); excelReader.read(readSheet); excelReader.finish(); } catch (Exception e) { e.printStackTrace(); } return existingList; } // 自定義監(jiān)聽器 private class OperationLogDataListener extends AnalysisEventListener<OperationLog> { private List<OperationLog> dataList; public OperationLogDataListener(List<OperationLog> dataList) { this.dataList = dataList; } @Override public void invoke(OperationLog operationLog, AnalysisContext analysisContext) { dataList.add(operationLog); } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { // 數(shù)據(jù)處理完畢后的操作(如果需要) } }
總結(jié)和展望
通過使用 EasyExcel,我們可以輕松實(shí)現(xiàn) Excel 導(dǎo)入功能,并對導(dǎo)入的數(shù)據(jù)進(jìn)行處理和轉(zhuǎn)換。本文介紹了如何準(zhǔn)備環(huán)境、創(chuàng)建實(shí)體類、自定義轉(zhuǎn)換器以及編寫導(dǎo)入邏輯的步驟和示例代碼。
在實(shí)際開發(fā)中,還可以進(jìn)一步擴(kuò)展功能,例如處理大數(shù)據(jù)量的導(dǎo)入、導(dǎo)入進(jìn)度條的展示等。通過深入研究 EasyExcel 的文檔和示例代碼,你可以發(fā)現(xiàn)更多有趣和有用的功能。
到此這篇關(guān)于SpringBoot中EasyExcel實(shí)現(xiàn)execl導(dǎo)入導(dǎo)出的文章就介紹到這了,更多相關(guān)SpringBoot execl導(dǎo)入導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot?整合?EasyExcel?實(shí)現(xiàn)自由導(dǎo)入導(dǎo)出功能
- SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出
- SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出
- SpringBoot整合EasyExcel實(shí)現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出
- 使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出
- SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能
- SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)
- SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出
- Springboot整合easyexcel實(shí)現(xiàn)一個接口任意表的Excel導(dǎo)入導(dǎo)出
相關(guān)文章
Java通過在主循環(huán)中判斷Boolean來停止線程的方法示例
這篇文章主要介紹了Java通過在主循環(huán)中判斷Boolean來停止線程的方法,結(jié)合具體實(shí)例形式分析了java針對線程的判斷與停止操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04使用spring動態(tài)獲取接口的不同實(shí)現(xiàn)類
這篇文章主要介紹了使用spring動態(tài)獲取接口的不同實(shí)現(xiàn)類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02java利用CountDownLatch實(shí)現(xiàn)并行計算
這篇文章主要介紹了java利用CountDownLatch實(shí)現(xiàn)并行計算,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10Spring Security實(shí)現(xiàn)5次密碼錯誤觸發(fā)賬號自動鎖定功能
在現(xiàn)代互聯(lián)網(wǎng)應(yīng)用中,賬號安全是重中之重,然而,暴力 破解攻擊依然是最常見的安全威脅之一,攻擊者通過自動化腳本嘗試大量的用戶名和密碼組合,試圖找到漏洞進(jìn)入系統(tǒng),所以為了解決這一問題,賬號鎖定機(jī)制被廣泛應(yīng)用,本文介紹了Spring Security實(shí)現(xiàn)5次密碼錯誤觸發(fā)賬號鎖定功能2024-12-12Java 根據(jù)貸款年限對應(yīng)利率計算功能實(shí)現(xiàn)解析
這篇文章主要介紹了Java 根據(jù)貸款年限對應(yīng)利率計算功能實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10