您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在SpringBoot中利用EasyExcel導入Excel文件,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1、 在pom.xml中添加EasyExcel依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.3</version> </dependency>
2、創建EasyExcel映射實體類
import com.alibaba.excel.annotation.ExcelProperty; public class ExcelEntity { // ExcelProperty中的參數要對應Excel中的標題 @ExcelProperty("ID") private int ID; @ExcelProperty("NAME") private String name; @ExcelProperty("AGE") private int age; public ExcelEntity() { } public ExcelEntity(int ID, String name, int age) { this.ID = ID; this.name = name; this.age = age; } public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
3、創建自定義Easyexcel的監聽類
這個監聽類里面每一個ExcelEntity對象代表一行數據
在這個監聽類里面可以對讀取到的每一行數據進行單獨操作
這里的讀取的數據是按照Excel中每一條數據的順序進行讀取的
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import java.util.ArrayList; import java.util.List; public class UploadExcelListener extends AnalysisEventListener<ExcelEntity> { private static final Logger logger = LoggerFactory.getLogger(LoggerItemController.class); public static final List<ExcelEntity> list = new ArrayList<>(); @Override public void invoke(ExcelEntity excelEntity, AnalysisContext context) { logger.info(String.valueOf(excelEntity.getID())); logger.info(excelEntity.getName()); logger.info(String.valueOf(excelEntity.getAge())); list.add(excelEntity); }
4、創建controller
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.util.List; @RestController @CrossOrigin @RequestMapping("/loggerItem") public class LoggerItemController { // MultipartFile 這個類一般是用來接受前臺傳過來的文件 @PostMapping("/upload") public List<ExcelEntity> upload(@RequestParam(value = "multipartFile") MultipartFile multipartFile){ if (multipartFile == null){ return null; } InputStream in = null; try { // 從multipartFile獲取InputStream流 in = multipartFile.getInputStream(); /* * EasyExcel 有多個不同的read方法,適用于多種需求 * 這里調用EasyExcel中通過InputStream流方式讀取Excel的Read方法 * 他會返回一個ExcelReaderBuilder類型的返回值 * ExcelReaderBuilder中有一個doReadAll方法,會讀取所有的Sheet */ EasyExcel.read(in,ExcelEntity.class,new UploadExcelListener()) .sheet("Sheet1") .doRead(); // 每次EasyExcel的read方法讀取完之后都會關閉流,我這里為了試驗doReadAll方法,所以重新獲取了一次 in = multipartFile.getInputStream(); /* * ExcelReaderBuilder中的Sheet方法,需要添加讀取的Sheet名作為參數 * 并且不要忘記在后面再調用一下doReadAll方法,否則不會進行讀取操作 */ EasyExcel.read(in,ExcelEntity.class,new UploadExcelListener()).doReadAll(); } catch (IOException e) { e.printStackTrace(); } return UploadExcelListener.list; } }
5、application.yml配置
server: # 指定端口號 port: 8080 spring: servlet: multipart: # 配置單個上傳文件大小 file-size-threshold: 100M # 配置總上傳大小 max-request-size: 300M
6、測試
我們先搞一個簡單的Excel,用來測試
然后通過Postman模擬發送請求
選擇Post請求并輸入請求地址
在下面選擇Body
Key的框中輸入controller中的請求的方法中的參數,后面的下拉框中選擇File
VALUE框中有一個Select File ,點擊后選擇自己剛才創建的測試的Excel
最后點擊Send發送請求
返回值如下:
由于我讀了兩次都放在同一個List中返回,所以返回值中有8個對象。
[ { "name": "小黑", "age": 25, "id": 1 }, { "name": "小白", "age": 22, "id": 2 }, { "name": "小黃", "age": 22, "id": 3 }, { "name": "小綠", "age": 23, "id": 4 }, { "name": "小黑", "age": 25, "id": 1 }, { "name": "小白", "age": 22, "id": 2 }, { "name": "小黃", "age": 22, "id": 3 }, { "name": "小綠", "age": 23, "id": 4 } ]
/** * Build excel the read * * @return Excel reader builder. */ public static ExcelReaderBuilder read() { return new ExcelReaderBuilder(); } /** * Build excel the read * * @param file * File to read. * @return Excel reader builder. */ public static ExcelReaderBuilder read(File file) { return read(file, null, null); } /** * Build excel the read * * @param file * File to read. * @param readListener * Read listener. * @return Excel reader builder. */ public static ExcelReaderBuilder read(File file, ReadListener readListener) { return read(file, null, readListener); } /** * Build excel the read * * @param file * File to read. * @param head * Annotate the class for configuration information. * @param readListener * Read listener. * @return Excel reader builder. */ public static ExcelReaderBuilder read(File file, Class head, ReadListener readListener) { ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); excelReaderBuilder.file(file); if (head != null) { excelReaderBuilder.head(head); } if (readListener != null) { excelReaderBuilder.registerReadListener(readListener); } return excelReaderBuilder; } /** * Build excel the read * * @param pathName * File path to read. * @return Excel reader builder. */ public static ExcelReaderBuilder read(String pathName) { return read(pathName, null, null); } /** * Build excel the read * * @param pathName * File path to read. * @param readListener * Read listener. * @return Excel reader builder. */ public static ExcelReaderBuilder read(String pathName, ReadListener readListener) { return read(pathName, null, readListener); } /** * Build excel the read * * @param pathName * File path to read. * @param head * Annotate the class for configuration information. * @param readListener * Read listener. * @return Excel reader builder. */ public static ExcelReaderBuilder read(String pathName, Class head, ReadListener readListener) { ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); excelReaderBuilder.file(pathName); if (head != null) { excelReaderBuilder.head(head); } if (readListener != null) { excelReaderBuilder.registerReadListener(readListener); } return excelReaderBuilder; } /** * Build excel the read * * @param inputStream * Input stream to read. * @return Excel reader builder. */ public static ExcelReaderBuilder read(InputStream inputStream) { return read(inputStream, null, null); } /** * Build excel the read * * @param inputStream * Input stream to read. * @param readListener * Read listener. * @return Excel reader builder. */ public static ExcelReaderBuilder read(InputStream inputStream, ReadListener readListener) { return read(inputStream, null, readListener); } /** * Build excel the read * * @param inputStream * Input stream to read. * @param head * Annotate the class for configuration information. * @param readListener * Read listener. * @return Excel reader builder. */ public static ExcelReaderBuilder read(InputStream inputStream, Class head, ReadListener readListener) { ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); excelReaderBuilder.file(inputStream); if (head != null) { excelReaderBuilder.head(head); } if (readListener != null) { excelReaderBuilder.registerReadListener(readListener); } return excelReaderBuilder; }
所有的方法都在這兒了,其實如果看不懂到底應該調用哪一個read方法的話,可以以根據自己所能得到的參數來判斷。
讀取本地Excel
public static void main(String[] args) { EasyExcel.read("C:/Users/Lonely Programmer/Desktop/新建 Microsoft Excel 工作表.xlsx" ,ExcelEntity.class ,new UploadExcelListener()) .doReadAll(); }
讀取本地的Excel和通過InPutStream流讀取的方式是一樣的,只是參數變了,原本傳的是InPutStream流,現在傳的是文件的絕對路徑。我這里監聽類和映射實體類都沒有變,和上傳用的是同一個,大家也可以根據需求來設定自己的監聽類與實體類
MultipartFile文檔
MultipartFile文檔地址:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html
翻譯是通過Google Chrome自帶翻譯插件進行翻譯的,建議大家使用Google Chrome打開,自帶翻譯功能
上述就是小編為大家分享的怎么在SpringBoot中利用EasyExcel導入Excel文件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。