Skip to content

Commit a55b149

Browse files
authored
perf[excel]: refactor excel (#536)
1 parent fc19121 commit a55b149

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

src/vendor/Export2Excel.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,45 +116,43 @@ export function export_table_to_excel(id) {
116116
saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), "test.xlsx")
117117
}
118118

119-
export function export_json_to_excel(th, jsonData, defaultTitle) {
120-
119+
export function export_json_to_excel({header, data, filename='excel-list', autoWidth=true}={}) {
121120
/* original data */
122-
123-
var data = jsonData;
124-
data.unshift(th);
121+
data=[...data]
122+
data.unshift(header);
125123
var ws_name = "SheetJS";
126-
127124
var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);
128125

129-
/*设置worksheet每列的最大宽度*/
130-
const colWidth = data.map(row => row.map(val => {
131-
/*先判断是否为null/undefined*/
132-
if (val == null) {
133-
return {'wch': 10};
134-
}
135-
/*再判断是否为中文*/
136-
else if (val.toString().charCodeAt(0) > 255) {
137-
return {'wch': val.toString().length * 2};
138-
} else {
139-
return {'wch': val.toString().length};
140-
}
141-
}))
142-
/*以第一行为初始值*/
143-
let result = colWidth[0];
144-
for (let i = 1; i < colWidth.length; i++) {
145-
for (let j = 0; j < colWidth[i].length; j++) {
146-
if (result[j]['wch'] < colWidth[i][j]['wch']) {
147-
result[j]['wch'] = colWidth[i][j]['wch'];
126+
if(autoWidth){
127+
/*设置worksheet每列的最大宽度*/
128+
const colWidth = data.map(row => row.map(val => {
129+
/*先判断是否为null/undefined*/
130+
if (val == null) {
131+
return {'wch': 10};
132+
}
133+
/*再判断是否为中文*/
134+
else if (val.toString().charCodeAt(0) > 255) {
135+
return {'wch': val.toString().length * 2};
136+
} else {
137+
return {'wch': val.toString().length};
138+
}
139+
}))
140+
/*以第一行为初始值*/
141+
let result = colWidth[0];
142+
for (let i = 1; i < colWidth.length; i++) {
143+
for (let j = 0; j < colWidth[i].length; j++) {
144+
if (result[j]['wch'] < colWidth[i][j]['wch']) {
145+
result[j]['wch'] = colWidth[i][j]['wch'];
146+
}
148147
}
149148
}
149+
ws['!cols'] = result;
150150
}
151-
ws['!cols'] = result;
152151

153152
/* add worksheet to workbook */
154153
wb.SheetNames.push(ws_name);
155154
wb.Sheets[ws_name] = ws;
156155

157156
var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});
158-
var title = defaultTitle || 'excel-list'
159-
saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx")
157+
saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), filename + ".xlsx");
160158
}

src/views/excel/exportExcel.vue

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
<template>
2-
<!-- $t is vue-i18n global function to translate lang -->
2+
<!-- $t is vue-i18n global function to translate lang -->
33
<div class="app-container">
4+
5+
<label class="radio-label" style="padding-left:0;">Filename: </label>
46
<el-input style='width:340px;' :placeholder="$t('excel.placeholder')" prefix-icon="el-icon-document" v-model="filename"></el-input>
5-
<el-button style='margin-bottom:20px;' type="primary" icon="document" @click="handleDownload" :loading="downloadLoading">{{$t('excel.export')}} excel</el-button>
7+
<label class="radio-label">Cell Auto Width: </label>
8+
<el-radio-group v-model="autoWidth">
9+
<el-radio :label="true" border>True</el-radio>
10+
<el-radio :label="false" border>False</el-radio>
11+
</el-radio-group>
12+
<el-button style='margin:0 0 20px 20px;' type="primary" icon="document" @click="handleDownload" :loading="downloadLoading">{{$t('excel.export')}} excel</el-button>
13+
614
<el-table :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row>
715
<el-table-column align="center" label='Id' width="95">
816
<template slot-scope="scope">
@@ -45,7 +53,8 @@ export default {
4553
list: null,
4654
listLoading: true,
4755
downloadLoading: false,
48-
filename: ''
56+
filename: '',
57+
autoWidth: true
4958
}
5059
},
5160
created() {
@@ -66,7 +75,12 @@ export default {
6675
const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
6776
const list = this.list
6877
const data = this.formatJson(filterVal, list)
69-
excel.export_json_to_excel(tHeader, data, this.filename)
78+
excel.export_json_to_excel({
79+
header: tHeader,
80+
data,
81+
filename: this.filename,
82+
autoWidth: this.autoWidth
83+
})
7084
this.downloadLoading = false
7185
})
7286
},
@@ -82,3 +96,13 @@ export default {
8296
}
8397
}
8498
</script>
99+
100+
<style>
101+
.radio-label {
102+
font-size: 14px;
103+
color: #606266;
104+
line-height: 40px;
105+
padding: 0 12px 0 30px;
106+
}
107+
</style>
108+

src/views/excel/selectExcel.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export default {
7272
const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
7373
const list = this.multipleSelection
7474
const data = this.formatJson(filterVal, list)
75-
excel.export_json_to_excel(tHeader, data, this.filename)
75+
excel.export_json_to_excel({
76+
header: tHeader,
77+
data,
78+
filename: this.filename
79+
})
7680
this.$refs.multipleTable.clearSelection()
7781
this.downloadLoading = false
7882
})

0 commit comments

Comments
 (0)