Skip to content

Commit ffec6b6

Browse files
committed
add:upload excel
1 parent f0afbf7 commit ffec6b6

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

README-en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- 401, 404 error page
3434
- Error log
3535
- Exporting to Excel
36+
- Upload Excel
3637
- Table example
3738
- Interactive table example
3839
- Drag & drop table example

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- 401,404错误页面
6464
- 错误日志
6565
- 导出excel
66+
- 前端可视化excel
6667
- table example
6768
- 动态table example
6869
- 拖拽table example

src/components/UploadExcel/index.vue

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<template>
2+
<div>
3+
<el-button :loading="loading" type="primary" @click="handleUpload">select excel file</el-button>
4+
<input id="excel-upload-input" type="file" accept=".xlsx, .xls" class="c-hide" @change="handkeFileChange">
5+
</div>
6+
</template>
7+
8+
<script>
9+
import XLSX from 'xlsx'
10+
11+
export default {
12+
data() {
13+
return {
14+
loading: false,
15+
excelData: {
16+
header: null,
17+
results: null
18+
}
19+
}
20+
},
21+
methods: {
22+
generateDate({ header, results }) {
23+
this.excelData.header = header
24+
this.excelData.results = results
25+
this.loading = false
26+
this.$emit('on-selected-file', this.excelData)
27+
},
28+
handleUpload() {
29+
document.getElementById('excel-upload-input').click()
30+
},
31+
handkeFileChange(e) {
32+
this.loading = true
33+
const files = e.target.files
34+
const itemFile = files[0] // only use files[0]
35+
const reader = new FileReader()
36+
reader.onload = e => {
37+
const data = e.target.result
38+
const fixedData = this.fixdata(data)
39+
const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
40+
const firstSheetName = workbook.SheetNames[0]
41+
const worksheet = workbook.Sheets[firstSheetName]
42+
const header = this.get_header_row(worksheet)
43+
const results = XLSX.utils.sheet_to_json(worksheet)
44+
this.generateDate({ header, results })
45+
}
46+
reader.readAsArrayBuffer(itemFile)
47+
},
48+
fixdata(data) {
49+
let o = ''
50+
let l = 0
51+
const w = 10240
52+
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
53+
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
54+
return o
55+
},
56+
get_header_row(sheet) {
57+
const headers = []
58+
const range = XLSX.utils.decode_range(sheet['!ref'])
59+
let C
60+
const R = range.s.r /* start in the first row */
61+
for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
62+
var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
63+
var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
64+
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
65+
headers.push(hdr)
66+
}
67+
return headers
68+
}
69+
}
70+
}
71+
</script>
72+
73+
<style scoped>
74+
#excel-upload-input{
75+
display: none;
76+
z-index: -9999;
77+
}
78+
</style>

src/router/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ export const asyncRouterMap = [
150150
icon: 'EXCEL',
151151
children: [
152152
{ path: 'download', component: _import('excel/index'), name: '导出excel' },
153-
{ path: 'download2', component: _import('excel/selectExcel'), name: '导出已选择项' }
153+
{ path: 'download2', component: _import('excel/selectExcel'), name: '导出已选择项' },
154+
{ path: 'upload', component: _import('excel/uploadExcel'), name: 'upload excel' }
154155
]
155156
},
156157
{

src/views/excel/uploadExcel.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<div class="app-container">
3+
<upload-excel @on-selected-file='selected'></upload-excel>
4+
<el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
5+
<el-table-column v-for='item of tableHeader' :prop="item" :label="item" :key='item'>
6+
</el-table-column>
7+
</el-table>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import uploadExcel from 'components/UploadExcel/index.vue'
13+
14+
export default {
15+
components: { uploadExcel },
16+
data() {
17+
return {
18+
tableData: [],
19+
tableHeader: []
20+
}
21+
},
22+
methods: {
23+
selected(data) {
24+
this.tableData = data.results
25+
this.tableHeader = data.header
26+
}
27+
}
28+
}
29+
</script>

0 commit comments

Comments
 (0)