forked from t-chatoyan/vue-excel-xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVueExcelXlsx.vue
74 lines (66 loc) · 2.17 KB
/
VueExcelXlsx.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
73
74
<template>
<button @click="exportExcel">
<slot></slot>
</button>
</template>
<script>
import XLSX from 'xlsx/xlsx';
window.$ = window.jQuery = require('jquery');
export default {
name: "vue-excel-xlsx",
props: {
columns: {
type: Array,
default: []
},
data: {
type: Array,
default: []
},
filename: {
type: String,
default: 'excel'
},
sheetname: {
type: String,
default: 'SheetName'
}
},
methods: {
exportExcel() {
let createXLSLFormatObj = [];
let newXlsHeader = [];
let vm = this;
if (vm.columns.length === 0){
console.log("Add columns!");
return;
}
if (vm.data.length === 0){
console.log("Add data!");
return;
}
$.each(vm.columns, function(index, value) {
newXlsHeader.push(value.label);
});
createXLSLFormatObj.push(newXlsHeader);
$.each(vm.data, function(index, value) {
let innerRowData = [];
$.each(vm.columns, function(index, val) {
if (val.dataFormat && typeof val.dataFormat === 'function') {
innerRowData.push(val.dataFormat(value[val.field]));
}else {
innerRowData.push(value[val.field]);
}
});
createXLSLFormatObj.push(innerRowData);
});
let filename = vm.filename + ".xlsx";
let ws_name = vm.sheetname;
let wb = XLSX.utils.book_new(),
ws = XLSX.utils.aoa_to_sheet(createXLSLFormatObj);
XLSX.utils.book_append_sheet(wb, ws, ws_name);
XLSX.writeFile(wb, filename);
}
}
}
</script>