Vue實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出導(dǎo)入實(shí)戰(zhàn)案例
前言
項(xiàng)目開(kāi)發(fā)當(dāng)中,列表數(shù)據(jù)的導(dǎo)出功能基本是每個(gè)業(yè)務(wù)系統(tǒng)必備的功能、另外Excel數(shù)據(jù)批量導(dǎo)入數(shù)據(jù)庫(kù)也是比較常見(jiàn)的功能,一般開(kāi)發(fā)都會(huì)采用POI、EasyExcel等后端框架實(shí)現(xiàn),后端服務(wù)實(shí)現(xiàn)的話,如果涉及業(yè)務(wù)調(diào)整的話,生產(chǎn)環(huán)境需要重啟后端服務(wù)。如果采用前端處理的話,就會(huì)方便很多,今天給大家介紹采用Vue框架集成xlsx組件的方式實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入、導(dǎo)出功能,希望對(duì)大家能有所幫助!
1、創(chuàng)建一個(gè)空白的vue2/vue3項(xiàng)目
可以通過(guò)腳手架方式創(chuàng)建一個(gè)vue示例項(xiàng)目。
需要的依賴(lài)包如下
"dependencies": {
"element-ui": "2.10.1",
"export2excel": "0.0.1",
"file-saver": "^2.0.5",
"vue": "^2.5.2",
"vue-router": "^3.0.1",
"xlsx": "^0.17.0"
},通過(guò)命令安裝
npm install export2excel@0.0.1 --save #導(dǎo)出到excel依賴(lài)包 npm install file-saver@2.0.5 --save #文件保存到客戶(hù)端 npm install xlsx@0.17.0 --save #操作excel依賴(lài)包
2、創(chuàng)建Export.vue 示例文件
主要實(shí)現(xiàn)表格內(nèi)容的導(dǎo)出和文件內(nèi)容導(dǎo)入的頁(yè)面的表格當(dāng)中,具體文件內(nèi)容完整內(nèi)容如下:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<el-row>
<el-button size="small" type="primary" @click="exportTest">導(dǎo)出</el-button>
<el-upload action="/" :on-change="importTest" :show-file-list="false"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
:auto-upload="false">
<el-button size="small" icon="el-icon-upload" type="primary">導(dǎo)入數(shù)據(jù)</el-button>
</el-upload>
</el-row>
<el-row>
<el-table ref="multipleTable" style="padding-top: 10px;" :data="listData" tooltip-effect="light"
highlight-current-row :header-cell-style="{
background: '#E6EAF3',
'font-size': '13px',
padding: '0px',
height: '40px',
}" v-loading="listLoading" :cell-style="{ 'font-size': '13px', padding: '0px', height: '34px' }">
<el-table-column label="序號(hào)" type="index" width="50"></el-table-column>
<el-table-column label="姓名" show-overflow-tooltip width="110">
<template slot-scope="scope">{{ scope.row.name }}</template>
</el-table-column>
<el-table-column label="年齡" show-overflow-tooltip width="">
<template slot-scope="scope">{{ scope.row.age }}</template>
</el-table-column>
</el-table>
</el-row>
</div>
</template>
<script>
import { export_json_to_excel } from "@/vendor/Export2Excel";
import Xlsx from 'xlsx'
export default {
name: 'HelloWorld',
data() {
return {
msg: '導(dǎo)入導(dǎo)出測(cè)試',
listData: [
{ name: "小明", age: 30 },
{ name: "小張", age: 25 },
{ name: "小李", age: 29 }
],
listLoading: false,
xlscTitle: {
"姓名": "name",
"年齡": "age"
},
}
},
methods: {
exportTest() {
const header = [
"姓名",
"年齡"
];
const body = [
"name",
"age",
];
const data = this.formatJson(body, this.listData);
console.log(data);
export_json_to_excel({
header: header,// 表頭
data: data, // 數(shù)據(jù)列表
filename: "用戶(hù)表",// 保存文件名
});
},
//格式化json數(shù)據(jù)為導(dǎo)出數(shù)據(jù) 過(guò)濾掉查詢(xún)的數(shù)據(jù)列不在導(dǎo)出的列里面的數(shù)據(jù)
formatJson(filterVal, jsonData) {
return jsonData.map((a) => filterVal.map((b) => a[b]));
},
importTest(file) {
let self = this;
const types = file.name.split('.')[1];
const fileType = ['xlsx', 'xlc', 'xlm', 'xls', 'xlt', 'xlw', 'csv'].some(item => {
return item === types
});
if (!fileType) {
this.$message.error('文件格式錯(cuò)誤,請(qǐng)重新選擇文件!')
}
this.file2Xce(file).then(tab => {
// 過(guò)濾,轉(zhuǎn)化正確的JSON對(duì)象格式
if (tab && tab.length > 0) {
tab[0].sheet.forEach(item => {
let obj = {};
for (let key in item) {
obj[self.xlscTitle[key]] = item[key];
}
self.listData.push(obj);
});
if (self.listData.length) {
this.$message.success('上傳成功')
// 獲取數(shù)據(jù)后,下一步操作
} else {
this.$message.error('空文件或數(shù)據(jù)缺失,請(qǐng)重新選擇文件!')
}
}
})
},
// 讀取文件
file2Xce(file) {
return new Promise(function (resolve, reject) {
const reader = new FileReader();
reader.onload = function (e) {
const data = e.target.result;
//var Xlsx = require("xlsx");
this.wb = Xlsx.read(data, {
type: "binary"
});
const result = [];
this.wb.SheetNames.forEach(sheetName => {
result.push({
sheetName: sheetName,
sheet: Xlsx.utils.sheet_to_json(this.wb.Sheets[sheetName])
})
})
resolve(result);
}
reader.readAsBinaryString(file.raw);
})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

總結(jié)
到此這篇關(guān)于Vue實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出導(dǎo)入的文章就介紹到這了,更多相關(guān)Vue數(shù)據(jù)導(dǎo)出導(dǎo)入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁(yè)效果
本文主要介紹了???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁(yè)效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Vite?Vue3?EsLint?Prettier配置步驟極簡(jiǎn)方法詳解
這篇文章主要為大家介紹了Vite?Vue3?EsLint?Prettier配置步驟的極簡(jiǎn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
iview同時(shí)驗(yàn)證多個(gè)表單問(wèn)題總結(jié)
這篇文章主要介紹了iview同時(shí)驗(yàn)證多個(gè)表單問(wèn)題總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
五分鐘教你使用vue-cli3創(chuàng)建項(xiàng)目(新手入門(mén))
本文主要介紹了五分鐘教你使用vue-cli3創(chuàng)建項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
詳解Unity webgl 嵌入Vue實(shí)現(xiàn)過(guò)程
Unity webgl嵌入到前端網(wǎng)頁(yè)中,前端通過(guò)調(diào)用Unity webgl內(nèi)方法實(shí)現(xiàn)需要展示的功能,前端點(diǎn)擊Unity webgl內(nèi)的交互點(diǎn),Unity webgl返回給前端一些需要的數(shù)據(jù),這篇文章主要介紹了Unity webgl 嵌入Vue實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2024-01-01
vue中使用webuploader做斷點(diǎn)續(xù)傳實(shí)現(xiàn)文件上傳功能
之前做的一個(gè)項(xiàng)目中,由于經(jīng)常上傳幾百兆的壓縮包,導(dǎo)致經(jīng)常上傳失敗,所以就找了webuploader插件做了斷點(diǎn)續(xù)傳,斷點(diǎn)續(xù)傳除了需要前端分片,也需要后臺(tái)去支持,所以做的時(shí)候做好對(duì)接協(xié)調(diào),所以本文就給大家詳細(xì)的介紹一下vue中如何使用webuploader做斷點(diǎn)續(xù)傳2023-07-07
基于Vue2實(shí)現(xiàn)的仿手機(jī)QQ單頁(yè)面應(yīng)用功能(接入聊天機(jī)器人 )
這篇文章主要介紹了基于Vue2實(shí)現(xiàn)的仿手機(jī)QQ單頁(yè)面應(yīng)用功能(接入聊天機(jī)器人 ),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
用vue構(gòu)建多頁(yè)面應(yīng)用的示例代碼
這篇文章主要介紹了用vue構(gòu)建多頁(yè)面應(yīng)用的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

