1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| <template> <div class="uploadBox"> <div class="buttonBox"> <el-upload action accept=".xlsx,.xls" :show-file-list="false" :on-change="handleChange" :auto-upload="false" > <el-button slot="trigger" type="primary">选取Excel文件</el-button> <el-button type="success" :disabled="disabled" @click="submit">提交到服务器</el-button> </el-upload> </div>
<div v-show="show" class="tableBox"> <h3> <i class="el-icon-info">请您检查无误后,再点击“提交到服务器”按钮</i> </h3> <el-table :data="tempData" border style="width: 100%" :height="height"> <el-table-column prop="name" label="姓名" min-width="50%" /> <el-table-column prop="phone" label="电话" min-width="50%" /> </el-table> </div> </div> </template>
<script> import xlsx from 'xlsx' import { Loading } from 'element-ui' import uploadExcel from '@/api' export default { name: 'Upload', data() { return { height: document.documentElement.clientHeight - 130, tempData: [], show: false, disabled: false, character: { name: { text: '姓名', type: 'string' }, phone: { text: '电话', type: 'string' } } } }, methods: { async handleChange(file) { const originData = file.raw if (!originData) return this.show = false const loadingInstance = Loading.service({ text: '努力加载中!!!', background: 'rgba(0, 0, 0, 0.8)' }) const binaryData = await this.readFile(originData) const workbook = xlsx.read(binaryData, { type: 'binary' }) const worksheet = workbook.Sheets[workbook.SheetNames[0]] const data = xlsx.utils.sheet_to_json(worksheet) this.tempData = this.handleData(data) await this.delay(300) this.show = true loadingInstance.close() }, handleData(data) { const arr = [] const char = this.character data.forEach(item => { const obj = {} for (const key in char) { if (Object.hasOwnProperty.call(char, key)) { const el = char[key] let val = item[el.text] || '' const type = el.type type === 'string' ? (val = String(val)) : null type === 'number' ? (val = Number(val)) : null obj[key] = val } } arr.push(obj) }) return arr }, async submit() { if (this.tempData.length <= 0) { this.$message({ message: '请先选择Excel文件', type: 'warning', showClose: true }) return } this.disabled = true const loadingInstance = Loading.service({ text: '努力加载中!!!', background: 'rgba(0, 0, 0, 0.8)' }) await this.delay(300) uploadExcel(this.tempData).then(() => { this.$message({ message: 'Excel文件已上传完毕', type: 'success', showClose: true }) this.show = false this.disabled = false loadingInstance.close() }) }, readFile(file) { return new Promise(resolve => { const reader = new FileReader() reader.readAsBinaryString(file) reader.onload = e => { resolve(e.target.result) } }) }, delay(interval = 0) { return new Promise(resolve => { const timer = setTimeout(_ => { clearTimeout(timer) resolve() }, interval) }) } } } </script>
<style scoped> .buttonBox { padding: 15px; display: flex; } .el-button { margin-right: 20px !important; } .tableBox { padding: 0 15px; } h3 { font-size: 18px; color: #f56c6c; padding-bottom: 15px; } </style>
|