|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <nav-bar/> |
| 4 | + <el-upload |
| 5 | + ref="upload" |
| 6 | + :on-preview="handlePreview" |
| 7 | + :on-remove="handleRemove" |
| 8 | + :before-upload="beforeUpload" |
| 9 | + :file-list="fileList" |
| 10 | + class="upload-demo" |
| 11 | + action=""> |
| 12 | + <el-button slot="trigger" size="small" type="primary">选取文件</el-button> |
| 13 | + <div slot="tip" class="el-upload__tip">只能上传 Excel 文件,且不超过 500kb</div> |
| 14 | + </el-upload> |
| 15 | + </div> |
| 16 | +</template> |
| 17 | + |
| 18 | +<script> |
| 19 | +import XLSX from 'xlsx' |
| 20 | +export default { |
| 21 | + data() { |
| 22 | + return { |
| 23 | + fileList: [] |
| 24 | + } |
| 25 | + }, |
| 26 | + methods: { |
| 27 | + submitUpload() { |
| 28 | + this.$refs.upload.submit() |
| 29 | + }, |
| 30 | + handleRemove(file, fileList) { |
| 31 | + console.log(file, fileList) |
| 32 | + }, |
| 33 | + handlePreview(file) { |
| 34 | + console.log(file) |
| 35 | + }, |
| 36 | + beforeUpload(file) { |
| 37 | + const _this = this |
| 38 | + // 使返回的值变成Promise对象,如果校验不通过,则reject,校验通过,则resolve |
| 39 | + return new Promise(function(resolve, reject) { |
| 40 | + // readExcel方法也使用了Promise异步转同步,此处使用then对返回值进行处理 |
| 41 | + _this.readExcel(file).then(result => { // 此时标识校验成功,为resolve返回 |
| 42 | + if (result) resolve(result) |
| 43 | + }) |
| 44 | + }) |
| 45 | + }, |
| 46 | + readExcel(file) { // 解析Excel |
| 47 | + console.log(file) |
| 48 | + const _this = this |
| 49 | + return new Promise(function(resolve, reject) { // 返回Promise对象 |
| 50 | + const reader = new FileReader() |
| 51 | + reader.onload = (e) => { // 异步执行 |
| 52 | + try { |
| 53 | + // 以二进制流方式读取得到整份excel表格对象 |
| 54 | + const data = e.target.result |
| 55 | + const workbook = XLSX.read(data, { type: 'binary' }) |
| 56 | + const batteryArr = [] |
| 57 | + console.log(workbook.SheetNames) |
| 58 | + for (const item in workbook.SheetNames) { |
| 59 | + console.log(item) |
| 60 | + const SheetName = workbook.SheetNames[item] |
| 61 | + const sheetInfos = workbook.Sheets[SheetName] |
| 62 | + for (const battery in sheetInfos) { |
| 63 | + if (battery !== '!ref') { |
| 64 | + batteryArr.push(sheetInfos[battery]) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + const outdata = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]) |
| 69 | + console.log(outdata) |
| 70 | + // console.log(batteryArr) |
| 71 | + if (batteryArr.length > _this.upLoadNumber) { |
| 72 | + console.log('上传电芯数量不能超过6条') |
| 73 | + resolve(false) |
| 74 | + } else { |
| 75 | + resolve(true) |
| 76 | + } |
| 77 | + } catch (e) { |
| 78 | + reject(e.message) |
| 79 | + } |
| 80 | + } |
| 81 | + reader.readAsBinaryString(file) |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +</script> |
| 87 | + |
| 88 | +<style scoped> |
| 89 | + .el-input { |
| 90 | + width: 97%; |
| 91 | + margin-bottom: 3% |
| 92 | + } |
| 93 | +
|
| 94 | + .input-with-select .el-input-group__prepend { |
| 95 | + background-color: #fff |
| 96 | + } |
| 97 | +</style> |
0 commit comments