如何使用vue實(shí)現(xiàn)前端導(dǎo)入excel數(shù)據(jù)
前言
繼前邊的vue的導(dǎo)出功能后,自己又去在網(wǎng)上搜了vue導(dǎo)入excel一些文章,自己通過對代碼的整理和調(diào)整,實(shí)現(xiàn)了vue導(dǎo)入excel的功能。
一、主界面先引入導(dǎo)入組件
1.這段主要是中間的那段excel-import標(biāo)簽的那部分代碼,loadList是后邊導(dǎo)入成功后,子組件要調(diào)用的方法,這個(gè)方法是導(dǎo)入成功后,刷新表格數(shù)據(jù)的
<a-space class="crud-buttons"> <a-button @click="handleEdit" type="primary"> <a-icon type="edit"></a-icon> 修改 </a-button> <excel-import @fatherMethod="loadList"></excel-import> <a-button @click="exportData" type="danger" :loading="btnLoading"> <a-icon type="upload" />導(dǎo)出</a-button> <a-button type="primary" @click="grantWelfare"> <a-icon type="plus"></a-icon> 發(fā)放 </a-button> </a-space>
路徑根據(jù)你們的要求自行修改
import excelImport from "../../components/excel-import"
二、封裝excel-import組件
1.首先是template代碼(這里用的是ant vue desgin框架的組件)
<template> <div class="import"> <a-button @click="exportData" type="primary"><a-icon type="import" />導(dǎo)入</a-button> <a-modal v-model="visible" title="導(dǎo)入數(shù)據(jù)" @ok="handleOk" @cancel="handleCancel" width="400px"> <a-upload-dragger :file-list="fileList" :multiple="false" :before-upload="beforeUpload" :remove="removeFile"> <p class="ant-upload-drag-icon"> <a-icon type="inbox"/> </p> <p class="ant-upload-hint"> 僅允許導(dǎo)入xls、xlsx格式文件 </p> </a-upload-dragger> </a-modal> </div> </template>
2.引入接口
import WelfareApi from '@/api/master/welfare'
3.js代碼methods
exportData(){ this.visible=true }, async handleOk(){ if (this.data.length === 0) { this.$message.error("請選擇要上傳的文件") } else { const data=await WelfareApi.importExcelData(this.data) this.$message.success(data.message) this.visible=false; this.fileList=[] this.$emit('fatherMethod'); } }, handleCancel(){ this.fileList=[] this.visible=false }, // 文件上傳前的鉤子 beforeUpload(file) { const isXslx = file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || file.type === "application/vnd.ms-excel"; const isLt2M = file.size / 1024 / 1024 < 2; if (!isXslx) { this.$message.error("附件格式錯(cuò)誤,請刪除后重新上傳!"); } if (isXslx) { if (this.fileList.length == 0) { this.fileList = [...this.fileList, file] this.importfxx(this.fileList); } else { this.$message.error("僅能選擇一個(gè)文件進(jìn)行上傳!") } } if (!isLt2M) { this.$message.error("上傳文件大小不能超過 2MB!"); } return false }, removeFile(file) { const index = this.fileList.indexOf(file); const newFileList = this.fileList.slice(); newFileList.splice(index, 1); this.fileList = newFileList; }, importfxx(obj) { let _this = this; let inputDOM = this.$refs.inputer; // 通過DOM取文件數(shù)據(jù) this.file = event.currentTarget.files[0]; let rABS = false; //是否將文件讀取為二進(jìn)制字符串 let f = this.file; let reader = new FileReader(); FileReader.prototype.readAsBinaryString = function (f) { let binary = ""; let rABS = false; //是否將文件讀取為二進(jìn)制字符串 let pt = this; let wb; //讀取完成的數(shù)據(jù) let outdata; let reader = new FileReader(); reader.onload = function (e) { let bytes = new Uint8Array(reader.result); let length = bytes.byteLength; for (let i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } //此處引入,用于解析excel let XLSX = require("xlsx"); if (rABS) { wb = XLSX.read(btoa(fixdata(binary)), { //手動(dòng)轉(zhuǎn)化 type: "base64" }); } else { wb = XLSX.read(binary, { type: "binary" }) } outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); //outdata就是讀取的數(shù)據(jù)(不包含標(biāo)題行即表頭,表頭會(huì)作為對象的下標(biāo)) //此處可對數(shù)據(jù)進(jìn)行處理 let arr = []; outdata.map(v => { let obj = {} obj.id = v['序號'] obj.dpStage = v['DP班號'] obj.traineeName = v['學(xué)員姓名'] obj.name = v['收貨人姓名'] obj.mobile = v['收貨人電話'] obj.province = v['省'] obj.city = v['市'] obj.district = v['區(qū)'] obj.detailAddr = v['詳細(xì)地址'] obj.expressName = v['快遞名稱'] obj.expressNumber = v['快遞編號'] obj.expressTime = v['發(fā)貨時(shí)間'] obj.statusName = v['狀態(tài)'] arr.push(obj) }); _this.data=arr; _this.dalen=arr.length; return arr; }; reader.readAsArrayBuffer(f); }; if (rABS) { reader.readAsArrayBuffer(f); } else { reader.readAsBinaryString(f); } }
這里有幾點(diǎn)注意的地方
在對outdata處理的時(shí)候,例如obj.id=v['序號']
這里的id就是傳給后端的對象集合中對象的屬性名,后端也要用對應(yīng)的model接收
這里的序號就是excel表格的表頭列名,一定要文字對應(yīng),不然會(huì)導(dǎo)入失敗
三.附加提示(后端也要寫的小伙伴可以參考下邊建議哈)
我們傳給后端的數(shù)據(jù),應(yīng)該是一個(gè)集合形式,在后端一般需要對數(shù)據(jù)進(jìn)行處理,因?yàn)槲覀円褦?shù)據(jù)insert到我們的數(shù)據(jù)庫中去,當(dāng)然可能會(huì)有重復(fù)的數(shù)據(jù)(比如說導(dǎo)入的是用戶信息,數(shù)據(jù)庫里存在這個(gè)人的,你要導(dǎo)入的excel也存在)如果要求是修改的話,這時(shí)候就要根據(jù)的唯一標(biāo)識(id,或者是電話、身份證號等等)來分情況做處理到底是新增還是修改。最后我還建議可以設(shè)置兩個(gè)變量,一個(gè)來記錄成功導(dǎo)入數(shù)據(jù)庫的記錄數(shù)量,一個(gè)記錄覆蓋(修改)記錄的數(shù)量,然后返回給前端。
四.總結(jié)
以上就是今天要講的內(nèi)容,本文介紹了vue導(dǎo)入excel的使用,主要就是在前端把要導(dǎo)入的excel進(jìn)行處理,最后把整理好的數(shù)據(jù)傳給后端,添加進(jìn)數(shù)據(jù)庫中。
到此這篇關(guān)于如何使用vue實(shí)現(xiàn)前端導(dǎo)入excel數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue前端導(dǎo)入excel數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于vue狀態(tài)過渡transition不起作用的原因解決
這篇文章主要介紹了關(guān)于vue狀態(tài)過渡transition不起作用的原因解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04Vue3在router中使用pinia報(bào)錯(cuò)的簡單解決辦法
這篇文章主要給大家介紹了關(guān)于Vue3在router中使用pinia報(bào)錯(cuò)的簡單解決辦法,什么是pinia,可以理解為狀態(tài)管理工具,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08卸載vue2.0并升級vue_cli3.0的實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于卸載vue2.0并升級vue_cli3.0的實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02Vue-scoped(局部)樣式使用方法及實(shí)例代碼
這篇文章主要介紹了Vue-scoped(局部)樣式使用方法及實(shí)例代碼,文中示例代碼介紹了的非常詳細(xì)感興趣的同學(xué)可以參考閱讀一下2023-05-05詳解如何實(shí)現(xiàn)Element樹形控件Tree在懶加載模式下的動(dòng)態(tài)更新
這篇文章主要介紹了詳解如何實(shí)現(xiàn)Element樹形控件Tree在懶加載模式下的動(dòng)態(tài)更新,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04