使用?XLSX 插件??npm install xlsx --save
<el-upload
? ? ? ? ? class="upload-demo"
? ? ? ? ? action=""
? ? ? ? ? :http-request="uploadExcel"
? ? ? ? ? accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
? ? ? ? >
? ? ? ? ? <el-button size="small" type="primary">批量導(dǎo)入賬號(hào)</el-button>
? ? ? ? </el-upload>
//批量導(dǎo)入賬戶
? ? uploadExcel(file) {
? ? ? const _file = file;
? ? ? const fileReader = new FileReader();
? ? ? fileReader.onload = (ev) => {
? ? ? ? try {
? ? ? ? ? const data = ev.target.result;
? ? ? ? ? const workbook = XLSX.read(data, {
? ? ? ? ? ? type: "binary",
? ? ? ? ? });
? ? ? ? ? for (let sheet in workbook.Sheets) {
? ? ? ? ? ? //循環(huán)讀取每個(gè)文件
? ? ? ? ? ? const sheetArray = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]); //若當(dāng)前sheet沒有數(shù)據(jù)决摧,則continue
? ? ? ? ? ? if (sheetArray.length == 0) {
? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? console.log("讀取文件");
? ? ? ? ? ? for (let item in sheetArray) {
? ? ? ? ? ? ? let rowTable = {};
? ? ? ? ? ? ? //這里的rowTable的屬性名注意要與上面表格的prop一致
? ? ? ? ? ? ? //sheetArray的屬性名與上傳的表格的列名一致
? ? ? ? ? ? ? rowTable.advertiser_id = sheetArray[item].媒體賬戶;
? ? ? ? ? ? ? this.selectIDPageArr.push(rowTable);
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? } catch (e) {
? ? ? ? ? this.$message.warning("文件類型不正確壹士!");
? ? ? ? }
? ? ? };
? ? ? console.log(_file)? //注意這里是使用vue封裝了一層的? 所以可能會(huì)報(bào)錯(cuò)? 需要console 一下這個(gè)_file? 看看內(nèi)容是什么字段
? ? ? fileReader.readAsBinaryString(_file.file);
? ? }
tips
想要?jiǎng)h除文件列表內(nèi)容?this.$refs.upload.uploadFiles.splice(idx, 1)
如果想要中斷上傳可以調(diào)用?this.$refs.uploader.abort()??
before-upload 方法不觸發(fā)問題:
before-upload 與 auto-upload的狀態(tài)是有關(guān)系的
當(dāng)auto-upload為true時(shí) before-upload方法才會(huì)被觸發(fā)