|
@@ -0,0 +1,147 @@
|
|
1
|
+<template>
|
|
2
|
+ <div>
|
|
3
|
+ <input id="excel-project-input" ref="excel-project-input" type="file" accept=".xlsx, .xls, .xltx" @change="handleClick">
|
|
4
|
+ <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">导入模板
|
|
5
|
+ </el-button>
|
|
6
|
+ </div>
|
|
7
|
+</template>
|
|
8
|
+
|
|
9
|
+<script>
|
|
10
|
+ import XLSX from 'xlsx'
|
|
11
|
+
|
|
12
|
+ export default {
|
|
13
|
+ props: {
|
|
14
|
+ beforeUpload: Function,
|
|
15
|
+ onSuccess: Function
|
|
16
|
+ },
|
|
17
|
+ data() {
|
|
18
|
+ return {
|
|
19
|
+ loading: false,
|
|
20
|
+ excelData: {
|
|
21
|
+ header: null,
|
|
22
|
+ results: null
|
|
23
|
+ }
|
|
24
|
+ }
|
|
25
|
+ },
|
|
26
|
+ methods: {
|
|
27
|
+ generateDate({ header, results }) {
|
|
28
|
+ this.excelData.header = header
|
|
29
|
+ this.excelData.results = results
|
|
30
|
+ this.onSuccess && this.onSuccess(this.excelData)
|
|
31
|
+ },
|
|
32
|
+ handleDrop(e) {
|
|
33
|
+ e.stopPropagation()
|
|
34
|
+ e.preventDefault()
|
|
35
|
+ if (this.loading) return
|
|
36
|
+ const files = e.dataTransfer.files
|
|
37
|
+ if (files.length !== 1) {
|
|
38
|
+ this.$message.error('Only support uploading one file!')
|
|
39
|
+ return
|
|
40
|
+ }
|
|
41
|
+ const rawFile = files[0] // only use files[0]
|
|
42
|
+
|
|
43
|
+ if (!this.isExcel(rawFile)) {
|
|
44
|
+ this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
|
|
45
|
+ return false
|
|
46
|
+ }
|
|
47
|
+ this.upload(rawFile)
|
|
48
|
+ e.stopPropagation()
|
|
49
|
+ e.preventDefault()
|
|
50
|
+ },
|
|
51
|
+ handleDragover(e) {
|
|
52
|
+ e.stopPropagation()
|
|
53
|
+ e.preventDefault()
|
|
54
|
+ e.dataTransfer.dropEffect = 'copy'
|
|
55
|
+ },
|
|
56
|
+ handleUpload() {
|
|
57
|
+ document.getElementById('excel-project-input').click()
|
|
58
|
+ },
|
|
59
|
+ handleClick(e) {
|
|
60
|
+ const files = e.target.files
|
|
61
|
+ const rawFile = files[0] // only use files[0]
|
|
62
|
+ if (!rawFile) return
|
|
63
|
+ this.upload(rawFile)
|
|
64
|
+ },
|
|
65
|
+ upload(rawFile) {
|
|
66
|
+ this.$refs['excel-project-input'].value = null // fix can't select the same excel
|
|
67
|
+
|
|
68
|
+ if (!this.beforeUpload) {
|
|
69
|
+ this.readerData(rawFile)
|
|
70
|
+ return
|
|
71
|
+ }
|
|
72
|
+ const before = this.beforeUpload(rawFile)
|
|
73
|
+ if (before) {
|
|
74
|
+ this.readerData(rawFile)
|
|
75
|
+ }
|
|
76
|
+ },
|
|
77
|
+ readerData(rawFile) {
|
|
78
|
+ this.loading = true
|
|
79
|
+ return new Promise((resolve, reject) => {
|
|
80
|
+ const reader = new FileReader()
|
|
81
|
+ reader.onload = e => {
|
|
82
|
+ const data = e.target.result
|
|
83
|
+ const fixedData = this.fixdata(data)
|
|
84
|
+ const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
|
|
85
|
+ const firstSheetName = workbook.SheetNames[0]
|
|
86
|
+ const worksheet = workbook.Sheets[firstSheetName]
|
|
87
|
+ const header = this.get_header_row(worksheet)
|
|
88
|
+ const results = XLSX.utils.sheet_to_json(worksheet)
|
|
89
|
+ this.generateDate({ header, results })
|
|
90
|
+ this.loading = false
|
|
91
|
+ resolve()
|
|
92
|
+ }
|
|
93
|
+ reader.readAsArrayBuffer(rawFile)
|
|
94
|
+ })
|
|
95
|
+ },
|
|
96
|
+ fixdata(data) {
|
|
97
|
+ let o = ''
|
|
98
|
+ let l = 0
|
|
99
|
+ const w = 10240
|
|
100
|
+ for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
|
|
101
|
+ o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
|
|
102
|
+ return o
|
|
103
|
+ },
|
|
104
|
+ get_header_row(sheet) {
|
|
105
|
+ if(sheet['!ref'] == undefined){
|
|
106
|
+ this.loading = false
|
|
107
|
+ return
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ const headers = []
|
|
111
|
+ const range = XLSX.utils.decode_range(sheet['!ref'])
|
|
112
|
+ let C
|
|
113
|
+ const R = range.s.r /* start in the first row */
|
|
114
|
+ for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
|
|
115
|
+ var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
|
|
116
|
+ var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
|
|
117
|
+ if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
|
|
118
|
+ headers.push(hdr)
|
|
119
|
+ }
|
|
120
|
+ return headers
|
|
121
|
+ },
|
|
122
|
+ isExcel(file) {
|
|
123
|
+ return /\.(xlsx|xls|csv)$/.test(file.name)
|
|
124
|
+ }
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+</script>
|
|
128
|
+
|
|
129
|
+<style scoped>
|
|
130
|
+ #excel-project-input {
|
|
131
|
+ display: none;
|
|
132
|
+ z-index: -9999;
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ #drop {
|
|
136
|
+ border: 2px dashed #bbb;
|
|
137
|
+ width: 600px;
|
|
138
|
+ height: 160px;
|
|
139
|
+ line-height: 160px;
|
|
140
|
+ margin: 0 auto;
|
|
141
|
+ font-size: 24px;
|
|
142
|
+ border-radius: 5px;
|
|
143
|
+ text-align: center;
|
|
144
|
+ color: #bbb;
|
|
145
|
+ position: relative;
|
|
146
|
+ }
|
|
147
|
+</style>
|