使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
一、導(dǎo)入和導(dǎo)出
導(dǎo)入:通過解析excel表格中的數(shù)據(jù),然后將數(shù)據(jù)放到一個集合中,接著通過對持久層操作,將數(shù)據(jù)插入到數(shù)據(jù)庫中,再加載一下頁面,從而實現(xiàn)了數(shù)據(jù)的導(dǎo)入
導(dǎo)出:導(dǎo)出也是直接對數(shù)據(jù)庫進行操作,獲取數(shù)據(jù)庫中所有的數(shù)據(jù),將其存儲在一個集中,接著使用查詢出來的的數(shù)據(jù)生成一個excel表格
其中導(dǎo)入和導(dǎo)出的功能實現(xiàn)都是基于EasyExcel實現(xiàn)的
EasyExcel是阿里巴巴開源的一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項目
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實現(xiàn)過程
@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ù)庫的所有數(shù)據(jù)到一個集合useList中
- 設(shè)置輸出流
- 調(diào)用EasyExcel中的write方法就會返回一個excel表格
- 給實體類使用注解標(biāo)注實體類每一個成員變量所對應(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ù)庫中的nick_name會自動轉(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)瀏覽器訪問這個前端控制器的映射地址的時候,就會自動下載這樣的一個excel文件
因此,在前端只需要給按鈕添加一個點擊事件,當(dāng)點擊這個按鈕的時候,就訪問前端控制器,從而實現(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ù)庫中
導(dǎo)出功能需要用的一個文件上傳的組件,這里用的是vue3組件庫中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)前比較重要的一個屬性就是action,這里action所對應(yīng)的是前端控制器的映射地址,也就是說選擇文件會傳送到地址對應(yīng)的前端控制器,前端控制就可以獲取這個文件
@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(); }
然后通過EasyExcel的read方法實現(xiàn)對excel的讀取功能
在read方法中需要提供一個監(jiān)視器
public class DataListener extends AnalysisEventListener<User> { /** * 每隔5條存儲數(shù)據(jù)庫,實際使用中可以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ù)會執(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ù),開始存儲數(shù)據(jù)庫" ); for (User user : list) { userMapper.insert(user); } System.out.println("存儲數(shù)據(jù)庫成功"); } }
注意:這個數(shù)據(jù)監(jiān)聽器不可以被springboot所代理,需要人工new出來,因此里面寫了一個構(gòu)造方法,用dao層作為參數(shù)創(chuàng)建,在new的時候?qū)⑦@個dao層的相對于的類作為構(gòu)造參數(shù),從而使得監(jiān)聽器可以完成對持久層的操作
到此這篇關(guān)于使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解的文章就介紹到這了,更多相關(guān)Springboot excel導(dǎo)入導(dǎo)出數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Windows系統(tǒng)下安裝Thrift的方法與使用講解
今天小編就為大家分享一篇關(guān)于在Windows系統(tǒng)下安裝Thrift的方法與使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12基于Flyway實現(xiàn)簡化Spring Boot項目部署
這篇文章主要介紹了基于Flyway實現(xiàn)簡化Spring Boot項目部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Spring boot中使用ElasticSearch的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring boot中使用ElasticSearch的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01JAVA使用quartz添加定時任務(wù),并依賴注入對象操作
這篇文章主要介紹了JAVA使用quartz添加定時任務(wù),并依賴注入對象操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09SpringBoot 自動掃描第三方包及spring.factories失效的問題解決
這篇文章主要介紹了SpringBoot 自動掃描第三方包及spring.factories失效的問題,本文給大家分享最新解決方法,需要的朋友可以參考下2023-05-05java如何確定一個鏈表有環(huán)及入口節(jié)點
這篇文章主要介紹了java如何確定一個鏈表有環(huán)及入口節(jié)點,想了解數(shù)據(jù)結(jié)構(gòu)的同學(xué)可以參考下2021-04-04