iview+vue實(shí)現(xiàn)導(dǎo)入EXCEL預(yù)覽功能
本文實(shí)例為大家分享了iview+vue實(shí)現(xiàn)導(dǎo)入EXCEL預(yù)覽的具體代碼,供大家參考,具體內(nèi)容如下
Xboot中,前端實(shí)現(xiàn)導(dǎo)入EXCEL預(yù)覽功能
HTML部分
<!-- 導(dǎo)入數(shù)據(jù) -->
? <Drawer title="導(dǎo)入數(shù)據(jù)" closable v-model="importModalVisible" width="1000">
? ? ? <div
? ? ? ? style="
? ? ? ? ? display: flex;
? ? ? ? ? justify-content: space-between;
? ? ? ? ? align-items: center;
? ? ? ? "
? ? ? >
? ? ? ? <Upload ref="upload"
? ? ? ? ? :action="importFile"
? ? ? ? ? :before-upload="beforeUploadImport"
? ? ? ? ? accept=".xls, .xlsx"
? ? ? ? ? :on-success="uploadSucess"
? ? ? ? ? :headers="accessToken">
? ? ? ? ? <Button
? ? ? ? ? ? :loading="reading"
? ? ? ? ? ? icon="ios-cloud-upload-outline"
? ? ? ? ? ? style="margin-right: 10px"
? ? ? ? ? ? >上傳Excel文件</Button
? ? ? ? ? >
? ? ? ? ? <span v-if="uploadfile.name"
? ? ? ? ? ? >當(dāng)前選擇文件:{{ uploadfile.name }}</span
? ? ? ? ? >
? ? ? ? </Upload>
? ? ? ? <Button @click="clearImportData" icon="md-trash">清空數(shù)據(jù)</Button>
? ? ? </div>
? ? ? <Alert type="warning" show-icon
? ? ? ? >導(dǎo)入前請(qǐng)下載查看導(dǎo)入模版數(shù)據(jù)文件查看所需字段及說明,確保數(shù)據(jù)格式正確,不得修改列英文名稱</Alert
? ? ? >
? ? ? <Table
? ? ? ? :columns="importColumns"
? ? ? ? border
? ? ? ? :data="importTableData"
? ? ? ? ref="importTable"
? ? ? ></Table>
? ? ? <!-- <div class="drawer-footer"> -->
? ? ? ? <div style="position: absolute; right: 15px; display: inline-block">
? ? ? ? ? <Button @click="importModalVisible = false">關(guān)閉</Button>
? ? ? ? ? <Button
? ? ? ? ? ? :loading="importLoading"
? ? ? ? ? ? :disabled="importTableData.length <= 0"
? ? ? ? ? ? @click="importData"
? ? ? ? ? ? style="margin-left: 8px"
? ? ? ? ? ? type="primary"
? ? ? ? ? >
? ? ? ? ? ? 確認(rèn)導(dǎo)入
? ? ? ? ? ? <span v-if="importTableData.length > 0"
? ? ? ? ? ? ? >{{ importTableData.length }} 條數(shù)據(jù)</span
? ? ? ? ? ? >
? ? ? ? ? </Button>
? ? ? ? </div>
? ? ? <!-- </div> -->
</Drawer>
需要引入
1、安裝插件
npm i xlsx
2、引入
import XLSX from "xlsx";
data中定義
importFile: importEquipment,//接口
accessToken: {},
uploadfile: {
??? ?name: ''
},
importColumns: [],
importTableData: [],js代碼
//導(dǎo)入數(shù)據(jù)
? ? beforeUploadImport(file) {
? ? ? this.uploadfile = file
? ? ? const fileExt = file.name
? ? ? ? .split('.')
? ? ? ? .pop()
? ? ? ? .toLocaleLowerCase()
? ? ? if (fileExt == 'xlsx' || fileExt == 'xls') {
? ? ? ? this.readFile(file)
? ? ? ? this.file = file
? ? ? } else {
? ? ? ? this.$Notice.warning({
? ? ? ? ? title: '文件類型錯(cuò)誤',
? ? ? ? ? desc:
? ? ? ? ? ? '所選文件‘ ' +
? ? ? ? ? ? file.name +
? ? ? ? ? ? ' '不是EXCEL文件,請(qǐng)選擇后綴為.xlsx或者.xls的EXCEL文件。'
? ? ? ? })
? ? ? }
? ? ? return false
? ? },
? ? // 讀取文件
? ? readFile(file) {
? ? ? this.reading = true
? ? ? const reader = new FileReader()
? ? ? reader.readAsArrayBuffer(file)
? ? ? reader.onerror = (e) => {
? ? ? ? this.reading = false
? ? ? ? this.$Message.error('文件讀取出錯(cuò)')
? ? ? }
? ? ? reader.onload = (e) => {
? ? ? ? console.log(e.target.result)
? ? ? ? const data = e.target.result
? ? ? ? const { header, results } = excel.read(data, 'array')
? ? ? ? const tableTitle = header.map((item) => {
? ? ? ? ? return { title: item, key: item, minWidth: 130, align: 'center' }
? ? ? ? })
? ? ? ? this.importTableData = results
? ? ? ? this.importColumns = tableTitle
? ? ? ? this.reading = false
? ? ? ? this.$Message.success('讀取數(shù)據(jù)成功')
? ? ? }
? ? },
? ? uploadSucess(response) {
? ? ? if (response.code == 200) {
? ? ? ? this.$Message.success('導(dǎo)入成功')
? ? ? ? this.importModalVisible = false
? ? ? ? this.clearImportData()
? ? ? ? this.getDataList()
? ? ? } else {
? ? ? ? this.$Message.error(response.message)
? ? ? }
? ? ? this.uploadfile = {}
? ? },
? ? clearImportData() {
? ? ? this.importTableData = []
? ? ? this.importColumns = []
? ? ? this.uploadfile = {}
? ? ? this.$refs.upload.clearFiles();
? ? },導(dǎo)入模板

效果預(yù)覽


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue如何在for循環(huán)中設(shè)置ref并獲取$refs
眾所周知在寫循環(huán)的時(shí)候給循環(huán)中的數(shù)據(jù)定義ref以便再下面直接通過this.$ref.來訪問,下面這篇文章主要給大家介紹了關(guān)于vue如何在for循環(huán)中設(shè)置ref并獲取$refs的相關(guān)資料,需要的朋友可以參考下2022-12-12
使用vue-cli3新建一個(gè)項(xiàng)目并寫好基本配置(推薦)
這篇文章主要介紹了使用vue-cli3新建一個(gè)項(xiàng)目并寫好基本配置的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04
vite+vue3中require?is?not?defined問題及解決
這篇文章主要介紹了vite+vue3中require?is?not?defined問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue3.2?新增指令?v-memo?用法詳解(提高性能利器)
v-memo 接受一個(gè)依賴的數(shù)組,依賴的數(shù)組變化,v-memo 所對(duì)應(yīng)的 DOM 包括子集將會(huì)重新渲染,這篇文章主要介紹了Vue3.2?新增指令?v-memo?用法,提高性能的又一利器,需要的朋友可以參考下2022-09-09
element?實(shí)現(xiàn)導(dǎo)航欄收起展開功能及思路
這篇文章主要介紹了element?實(shí)現(xiàn)導(dǎo)航欄收起展開功能,實(shí)現(xiàn)思路先給 el-menu加上 :collapse="isCollapse" 屬性,這個(gè)屬性也是 element 上的一個(gè)參數(shù),意思為是否開啟折疊動(dòng)畫,在 data 中定義 isCollapse ,用 true 和 false 控制展開與收起,需要的朋友可以參考下2023-01-01

