-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportXLSX.vue
72 lines (71 loc) · 1.81 KB
/
ImportXLSX.vue
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
<template>
<div>
<el-upload action=""
:before-upload="handleImportXLSX"
:show-file-list="false">
<el-tooltip content="只能导入xls/xlsx文件"
placement="top"
effect="dark">
<el-button size="small"
type="primary">批量导入</el-button>
</el-tooltip>
</el-upload>
</div>
</template>
<script>
import XLSX from 'xlsx'
export default {
props: {
onImport: {
type: Function,
default: () => {}
}
},
methods: {
async handleImportXLSX(file) {
const { checkFileType, readXLSX } = this
if (checkFileType(file.name)) {
this.$props.onImport(
await readXLSX(file).catch(e => {
console.log(e)
})
)
} else {
this.$props.onImport({
message: '文件类型不支持,请上传 xls/xlsx 文件'
})
}
return Promise.reject()
},
checkFileType(name) {
return /\.xlsx?$/.test(name)
},
readXLSX(file) {
return new Promise((res, rej) => {
const fileReader = new FileReader()
const onLoadXLS = e => {
try {
const workbook = XLSX.read(e.target.result, {
type: 'binary'
})
const data = []
let fromTo = ''
for (let sheet in workbook.Sheets) {
if (workbook.Sheets.hasOwnProperty(sheet)) {
fromTo = workbook.Sheets[sheet]['!ref']
console.log(fromTo)
data.push(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]))
}
}
res(data)
} catch (e) {
rej({ message: '读取失败,请重试', ...e })
}
}
fileReader.onload = onLoadXLS
fileReader.readAsBinaryString(file)
})
}
}
}
</script>