使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
一、導(dǎo)入和導(dǎo)出
導(dǎo)入:通過解析excel表格中的數(shù)據(jù),然后將數(shù)據(jù)放到一個集合中,接著通過對持久層操作,將數(shù)據(jù)插入到數(shù)據(jù)庫中,再加載一下頁面,從而實(shí)現(xiàn)了數(shù)據(jù)的導(dǎo)入
導(dǎo)出:導(dǎo)出也是直接對數(shù)據(jù)庫進(jìn)行操作,獲取數(shù)據(jù)庫中所有的數(shù)據(jù),將其存儲在一個集中,接著使用查詢出來的的數(shù)據(jù)生成一個excel表格
其中導(dǎo)入和導(dǎo)出的功能實(shí)現(xiàn)都是基于EasyExcel實(shí)現(xiàn)的
EasyExcel是阿里巴巴開源的一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項(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)過程
@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表格
- 給實(shí)體類使用注解標(biāo)注實(shí)體類每一個成員變量所對應(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文件
因此,在前端只需要給按鈕添加一個點(diǎn)擊事件,當(dāng)點(diǎ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ù)庫中
導(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方法實(shí)現(xiàn)對excel的讀取功能
在read方法中需要提供一個監(jiān)視器
public class DataListener extends AnalysisEventListener<User> {
/**
* 每隔5條存儲數(shù)據(jù)庫,實(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ù)會執(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實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解的文章就介紹到這了,更多相關(guān)Springboot excel導(dǎo)入導(dǎo)出數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法
- vue項(xiàng)目預(yù)覽excel表格功能(file-viewer插件)
- vue項(xiàng)目使用luckyexcel預(yù)覽excel表格功能(心路歷程)
- Vue3實(shí)現(xiàn)動態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法詳解
- vue使用axios導(dǎo)出后臺返回的文件流為excel表格詳解
- vue項(xiàng)目中常見的三種文件類型在線預(yù)覽實(shí)現(xiàn)(pdf/word/excel表格)
- vue中el-table前端如何導(dǎo)出excel數(shù)據(jù)表格
相關(guān)文章
Java開發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Java開發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
SpringBoot將項(xiàng)目打成war包步驟解析
這篇文章主要介紹了SpringBoot將項(xiàng)目打成war包步驟解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng)的詳細(xì)步驟
這篇文章主要介紹了javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Java設(shè)置Excel數(shù)據(jù)驗(yàn)證的示例代碼
數(shù)據(jù)驗(yàn)證是Excel 2013版本中,數(shù)據(jù)功能組下面的一個功能。本文將通過Java程序代碼演示數(shù)據(jù)驗(yàn)證的設(shè)置方法及結(jié)果,感興趣的可以了解一下2022-05-05
Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù)
這篇文章主要介紹了Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
jdk17?SpringBoot?JPA集成多數(shù)據(jù)庫的示例詳解
這篇文章主要介紹了jdk17?SpringBoot?JPA集成多數(shù)據(jù)庫的示例代碼,包括配置類、請求攔截器、線程上下文等相關(guān)知識,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08

