使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
一、導(dǎo)入和導(dǎo)出
導(dǎo)入:通過(guò)解析excel表格中的數(shù)據(jù),然后將數(shù)據(jù)放到一個(gè)集合中,接著通過(guò)對(duì)持久層操作,將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中,再加載一下頁(yè)面,從而實(shí)現(xiàn)了數(shù)據(jù)的導(dǎo)入
導(dǎo)出:導(dǎo)出也是直接對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作,獲取數(shù)據(jù)庫(kù)中所有的數(shù)據(jù),將其存儲(chǔ)在一個(gè)集中,接著使用查詢出來(lái)的的數(shù)據(jù)生成一個(gè)excel表格
其中導(dǎo)入和導(dǎo)出的功能實(shí)現(xiàn)都是基于EasyExcel實(shí)現(xiàn)的
EasyExcel是阿里巴巴開(kāi)源的一個(gè)基于Java的簡(jiǎn)單、省內(nèi)存的讀寫Excel的開(kāi)源項(xiàng)目
EasyExcel官方文檔:https://www.yuque.com/easyexcel/doc/easyexcel
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.0-beta2</version> </dependency>
二、導(dǎo)出數(shù)據(jù)為excel實(shí)現(xiàn)過(guò)程
@GetMapping("/down") public Result down(HttpServletResponse response) throws IOException { List<User> userList = userMapper.selectList(null); System.out.println(userList); //返回輸出流_excel格式 response.setContentType("application/octet-stream"); String fileName = URLEncoder.encode("用戶信息表", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), User.class).autoCloseStream(Boolean.FALSE).sheet("用戶信息表").doWrite(userList); // ExcelUtil.writerExcel(response, userList); return Result.success(); }
- 查詢數(shù)據(jù)庫(kù)的所有數(shù)據(jù)到一個(gè)集合useList中
- 設(shè)置輸出流
- 調(diào)用EasyExcel中的write方法就會(huì)返回一個(gè)excel表格
- 給實(shí)體類使用注解標(biāo)注實(shí)體類每一個(gè)成員變量所對(duì)應(yīng)的表頭(value為表頭名稱,index為表頭位置)
package com.kang.domain; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @Data @TableName("user") @NoArgsConstructor @EqualsAndHashCode public class User { /** * id自增 */ @TableId(type = IdType.AUTO) @ExcelProperty(value = "ID", index = 0) private Integer id; @ExcelProperty(value = "用戶名", index = 1) private String username; @ExcelProperty(value = "密碼", index = 2) private String password; /** * 數(shù)據(jù)庫(kù)中的nick_name會(huì)自動(dòng)轉(zhuǎn)換為駝峰 */ @ExcelProperty(value = "昵稱", index = 3) private String nickName; @ExcelProperty(value = "年齡", index = 4) private Integer age; @ExcelProperty(value = "性別", index = 5) private String sex; @ExcelProperty(value = "住址", index = 6) private String address; }
當(dāng)瀏覽器訪問(wèn)這個(gè)前端控制器的映射地址的時(shí)候,就會(huì)自動(dòng)下載這樣的一個(gè)excel文件
因此,在前端只需要給按鈕添加一個(gè)點(diǎn)擊事件,當(dāng)點(diǎn)擊這個(gè)按鈕的時(shí)候,就訪問(wèn)前端控制器,從而實(shí)現(xiàn)導(dǎo)出功能
download(){ window.location.href='http://localhost:9090/excel/down'; this.$message.success("導(dǎo)出成功"); },
三、將excel中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中
導(dǎo)出功能需要用的一個(gè)文件上傳的組件,這里用的是vue3組件庫(kù)中element-plus提供的el-upload組件
<el-upload class="upload-demo" multiple="" method="post" action="api/excel/updown" style="margin-left: 10px" accept=".xlsx,.xls" :show-file-list="false" :on-success="success" name="files" > <el-button type="primary">導(dǎo)入</el-button> </el-upload>
當(dāng)前比較重要的一個(gè)屬性就是action,這里action所對(duì)應(yīng)的是前端控制器的映射地址,也就是說(shuō)選擇文件會(huì)傳送到地址對(duì)應(yīng)的前端控制器,前端控制就可以獲取這個(gè)文件
@PostMapping("/updown") public Result upload(@RequestParam("files") MultipartFile file) throws IOException { EasyExcel.read(file.getInputStream(), User.class, new DataListener(userMapper)).sheet().doRead(); return Result.success(); }
然后通過(guò)EasyExcel的read方法實(shí)現(xiàn)對(duì)excel的讀取功能
在read方法中需要提供一個(gè)監(jiān)視器
public class DataListener extends AnalysisEventListener<User> { /** * 每隔5條存儲(chǔ)數(shù)據(jù)庫(kù),實(shí)際使用中可以100條,然后清理list ,方便內(nèi)存回收 */ private static final int BATCH_COUNT = 100; private UserMapper userMapper; public DataListener(UserMapper userMapper){ this.userMapper = userMapper; } List<User> list = new ArrayList<User>(); //讀取數(shù)據(jù)會(huì)執(zhí)行這方法 @Override public void invoke(User user, AnalysisContext analysisContext) { System.out.println(JSON.toJSONString(user)); list.add(user); System.out.println(list); if (list.size() >= BATCH_COUNT){ list.clear(); } } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { saveData(); System.out.println("所有數(shù)據(jù)解析完成"); } private void saveData(){ System.out.println(this.userMapper); System.out.println("{" + list.size() + "}條數(shù)據(jù),開(kāi)始存儲(chǔ)數(shù)據(jù)庫(kù)" ); for (User user : list) { userMapper.insert(user); } System.out.println("存儲(chǔ)數(shù)據(jù)庫(kù)成功"); } }
注意:這個(gè)數(shù)據(jù)監(jiān)聽(tīng)器不可以被springboot所代理,需要人工new出來(lái),因此里面寫了一個(gè)構(gòu)造方法,用dao層作為參數(shù)創(chuàng)建,在new的時(shí)候?qū)⑦@個(gè)dao層的相對(duì)于的類作為構(gòu)造參數(shù),從而使得監(jiān)聽(tīng)器可以完成對(duì)持久層的操作
到此這篇關(guān)于使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解的文章就介紹到這了,更多相關(guān)Springboot excel導(dǎo)入導(dǎo)出數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
- SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出
- Java使用EasyExcel實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出
- 基于EasyExcel實(shí)現(xiàn)百萬(wàn)級(jí)數(shù)據(jù)導(dǎo)入導(dǎo)出詳解
- java使用EasyExcel導(dǎo)入導(dǎo)出excel
- 教您如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能
相關(guān)文章
在Windows系統(tǒng)下安裝Thrift的方法與使用講解
今天小編就為大家分享一篇關(guān)于在Windows系統(tǒng)下安裝Thrift的方法與使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12基于Flyway實(shí)現(xiàn)簡(jiǎn)化Spring Boot項(xiàng)目部署
這篇文章主要介紹了基于Flyway實(shí)現(xiàn)簡(jiǎn)化Spring Boot項(xiàng)目部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Spring boot中使用ElasticSearch的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring boot中使用ElasticSearch的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01JAVA使用quartz添加定時(shí)任務(wù),并依賴注入對(duì)象操作
這篇文章主要介紹了JAVA使用quartz添加定時(shí)任務(wù),并依賴注入對(duì)象操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09SpringBoot 自動(dòng)掃描第三方包及spring.factories失效的問(wèn)題解決
這篇文章主要介紹了SpringBoot 自動(dòng)掃描第三方包及spring.factories失效的問(wèn)題,本文給大家分享最新解決方法,需要的朋友可以參考下2023-05-05Springboot如何實(shí)現(xiàn)代理服務(wù)器
這篇文章主要介紹了Springboot如何實(shí)現(xiàn)代理服務(wù)器問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06java如何確定一個(gè)鏈表有環(huán)及入口節(jié)點(diǎn)
這篇文章主要介紹了java如何確定一個(gè)鏈表有環(huán)及入口節(jié)點(diǎn),想了解數(shù)據(jù)結(jié)構(gòu)的同學(xué)可以參考下2021-04-04