SpringBoot中EasyExcel實(shí)現(xiàn)execl導(dǎo)入導(dǎo)出
引言
在實(shí)際開發(fā)中,處理 Excel 文件是一個(gè)常見的需求。EasyExcel 是一個(gè)基于 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)備一個(gè)包含數(shù)據(jù)的 Excel 文件,作為導(dǎo)入的示例。確保 Excel 文件的結(jié)構(gòu)和數(shù)據(jù)與實(shí)體類的字段對(duì)應(yīng)。

創(chuàng)建實(shí)體類
在使用 EasyExcel 進(jìn)行導(dǎo)入時(shí),我們需要?jiǎng)?chuàng)建一個(gè)與 Excel 數(shù)據(jù)結(jié)構(gòu)相匹配的實(shí)體類。實(shí)體類的字段應(yīng)與 Excel 文件的列對(duì)應(yīng)。使用 @ExcelProperty 注解來標(biāo)識(shí)字段與 Excel 列的映射關(guān)系,以及一些其他注解來設(shè)置字段的屬性。舉個(gè)例子如下:
@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 = "操作時(shí)間",index = 6)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ExcelIgnore
private String operationTime; //操作時(shí)間
@ExcelProperty(value = "操作結(jié)果",index = 7)
private String result; //操作結(jié)果
}自定義轉(zhuǎn)換器
有時(shí),我們需要對(duì) Excel 中的數(shù)據(jù)進(jìn)行特殊處理或轉(zhuǎn)換,以適應(yīng)實(shí)體類的字段類型。EasyExcel 允許我們自定義轉(zhuǎn)換器來實(shí)現(xiàn)這個(gè)功能。下面是一個(gè)示例
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 的邏輯。以下是一個(gè)示例:
我通過的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)入功能,并對(duì)導(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)容請(qǐng)搜索腳本之家以前的文章或繼續(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)一個(gè)接口任意表的Excel導(dǎo)入導(dǎo)出
相關(guān)文章
SpringBoot應(yīng)用部署于外置Tomcat容器的方法
這篇文章主要介紹了SpringBoot應(yīng)用部署于外置Tomcat容器的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
SpringBoot大學(xué)心理服務(wù)系統(tǒng)實(shí)現(xiàn)流程分步講解
本系統(tǒng)主要論述了如何使用JAVA語言開發(fā)一個(gè)大學(xué)生心理服務(wù)系統(tǒng) ,本系統(tǒng)將嚴(yán)格按照軟件開發(fā)流程進(jìn)行各個(gè)階段的工作,采用B/S架構(gòu),面向?qū)ο缶幊趟枷脒M(jìn)行項(xiàng)目開發(fā)2022-09-09
Java使用JFreeChart創(chuàng)建動(dòng)態(tài)圖表的代碼示例
在數(shù)據(jù)可視化的世界中,圖表是展示數(shù)據(jù)的強(qiáng)大工具,無論是折線圖、柱狀圖還是餅圖,它們都能幫助我們更直觀地理解數(shù)據(jù),在Java生態(tài)中,JFreeChart是一個(gè)功能強(qiáng)大且靈活的圖表庫,廣泛應(yīng)用于各種 Java 應(yīng)用程序中,本文將帶你從零開始學(xué)習(xí)如何使用JFreeChart創(chuàng)建動(dòng)態(tài)圖表2025-02-02
Javafx利用fxml變換場景的實(shí)現(xiàn)示例
本文主要介紹了Javafx利用fxml變換場景的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
SpringBoot集成Jasypt實(shí)現(xiàn)敏感信息加密保護(hù)功能
在數(shù)字化時(shí)代背景下,互聯(lián)網(wǎng)滲透生活的方方面面,同時(shí)也帶來了日益嚴(yán)峻的安全挑戰(zhàn),因此,采用有效的敏感信息加密手段不僅是保護(hù)知識(shí)產(chǎn)權(quán)和業(yè)務(wù)安全的必要舉措,所以本文介紹了SpringBoot集成Jasypt實(shí)現(xiàn)敏感信息加密保護(hù)功能,需要的朋友可以參考下2025-04-04
聊聊SpringCloud和SpringCloudAlibaba的區(qū)別
這篇文章主要介紹了SpringCloud和SpringCloudAlibaba的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(53)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08

