在Vue+SpringBoot應(yīng)用中實現(xiàn)導入導出excel表功能全過程
如何在Vue+SpringBoot應(yīng)用中導入導出excel表
今天,人超級好的組長交給我一個任務(wù),讓我完成導入導出用戶列表的功能。

后端
1.控制層寫導入導出的接口

2.Service層寫導入導出的具體方法
以下是導入數(shù)據(jù)的代碼,要用到FastExcel讀取數(shù)據(jù)后,再調(diào)用UserDao進行數(shù)據(jù)的插入
public ResponseDTO<String> importUsers(MultipartFile file){
List<UserImportForm> dataList;
try{
dataList = FastExcel.read(file.getInputStream()).head(UserImportForm.class)
.sheet()
.doReadSync();
for(UserImportForm userImportForm:dataList){
UserEntity userEntity = SmartBeanUtil.copy(userImportForm,UserEntity.class);
userDao.insert(userEntity);
}
}catch(IOException e){
log.error(e.getMessage(),e);
throw new BusinessException("數(shù)據(jù)格式存在問題,無法讀取");
}
if(CollectionUtils.isEmpty(dataList)){
return ResponseDTO.userErrorParam("數(shù)據(jù)為空");
}
return ResponseDTO.okMsg("成功導入"+dataList.size()+"條,具體數(shù)據(jù)為:"+ JSON.toJSONString(dataList));
}
前端
1.分為三步

2.第一步下載模板
<a-button @click="downloadExcel"> <download-outlined />第一步:下載模板</a-button>
直接將excel表單放在static文件夾下就可以了,前端就可以直接下載表單
function downloadExcel(){
window.open('http://localhost:1024/user.xlsx');
}
3.第二步選擇文件

4.第三步開始導入
<a-button @click="onImportUsers"> <ImportOutlined /> 第三步:開始導入 </a-button>
中間調(diào)用后端提供的接口/user/importUsers就可
async function onImportUsers(){
const formData = new FormData();
fileList.value.forEach((file)=>{
formData.append('file',file.originFileObj);
});
SmartLoading.show();
try{
let res = await userApi.importUsers(formData);
message.success(res.msg);
}catch(e){
smartSentry.captureError(e);
}finally {
SmartLoading.hide();
}
}
以上就是在Vue+SpringBoot應(yīng)用中實現(xiàn)導入導出excel表功能全過程的詳細內(nèi)容,更多關(guān)于Vue SpringBoot導入導出excel表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue Element前端應(yīng)用開發(fā)之菜單資源管理
在權(quán)限管理系統(tǒng)中,菜單也屬于權(quán)限控制的一個資源,屬于角色控制的一環(huán)。不同角色用戶,登錄系統(tǒng)后,出現(xiàn)的系統(tǒng)菜單是不同的。菜單結(jié)合路由集合,實現(xiàn)可訪問路由的過濾,也就實現(xiàn)了對應(yīng)角色菜單的展示和可訪問路由的控制。2021-05-05
Vue實現(xiàn)兩個列表之間的數(shù)據(jù)聯(lián)動的代碼示例
在Vue.js應(yīng)用開發(fā)中,列表數(shù)據(jù)的聯(lián)動是一個常見的需求,這種聯(lián)動可以用于多種場景,例如過濾篩選、關(guān)聯(lián)選擇等,本文將詳細介紹如何在Vue項目中實現(xiàn)兩個列表之間的數(shù)據(jù)聯(lián)動,并通過多個具體的代碼示例來幫助讀者理解其實現(xiàn)過程,需要的朋友可以參考下2024-10-10
defineProps宏函數(shù)不需要從vue中import導入的原因解析
這篇文章主要介紹了defineProps宏函數(shù)不需要從vue中import導入的原因解析,本文給大家介紹的非常詳細,需要的朋友可以參考下2024-07-07

