forked from jecovier/vue-json-excel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonExcel.vue
173 lines (164 loc) · 4.41 KB
/
JsonExcel.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<template>
<a
href="#"
:id="id_name"
@click="generate">
<slot>
{{button_text}}
</slot>
</a>
</template>
<script>
export default {
data: function(){
return {
animate : true,
animation : '',
}
},
props: {
'type' : {
type: String,
default: "xls"
},
'button_text': {
type: String,
default: "Download Excel"
},
'data':{
type: Array,
required: true
},
'fields':{
type: Object,
required: true
},
'name':{
type: String,
default: "data.xls"
},
'meta':{
type: Array,
default: () => []
}
},
created: function () {
},
computed:{
id_name : function(){
var now = new Date().getTime();
return 'export_'+now;
}
},
methods: {
generate() {
if (this.type == 'csv') {
return this.exportCSV(this.data, this.name, this.fields);
}
return this.exportXLS(this.data, this.name, this.fields);
},
generate_excel: function () {
this.exportXLS(this.data, this.name, this.fields)
},
jsonToXLS: function (data, header) {
var xlsTemp = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>${table}</table></body></html>'
var xlsData = '', keys = []
if (header) {
xlsData += '<thead>'
for (var key in header) {
keys.push(key)
xlsData += '<th>' + key + '</th>'
}
xlsData += '</thead>'
xlsData += '<tbody>'
data.map(function (item, index) {
xlsData += '<tr>'
for (var i = 0; i < keys.length; i++) {
if (keys[i].indexOf(".") !== -1) {
var keyNestedSplit = keys[i].split(".");
var valueFromNestedKey = item[keyNestedSplit[0]];
for (var j = 1; j < keyNestedSplit.length; j++) {
valueFromNestedKey = valueFromNestedKey[keyNestedSplit[j]];
}
xlsData += '<td>' + valueFromNestedKey + '</td>'
} else {
xlsData += '<td>' + item[keys[i]] + '</td>'
}
}
xlsData += '</tr>'
})
xlsData += '</tbody>'
return xlsTemp.replace('${table}', xlsData)
}
data.map(function (item, index) {
xlsData += '<tbody><tr>'
for (var key in item) {
xlsData += '<td>' + item[key] + '</td>'
}
xlsData += '</tr></tbody>'
})
return xlsTemp.replace('${table}', xlsData)
},
jsonToCSV: function (data, header) {
var csvData = ''
if (header) {
for (var key in header) {
csvData += key + ','
}
csvData = csvData.slice(0, csvData.length - 1)
csvData += '\r\n'
}
data.map(function (item) {
for (var k in item) {
csvData += item[k] + ','
}
csvData = csvData.slice(0, csvData.length - 1)
csvData += '\r\n'
})
return csvData
},
base64: function (s) {
return window.btoa(window.unescape(encodeURIComponent(s)))
},
exportXLS: function (data, fileName, header) {
var XLSData = 'data:application/vnd.ms-excel;base64,' + this.base64(this.jsonToXLS(data, header))
this.download(XLSData, fileName)
},
exportCSV: function (data, fileName, keys) {
var CSVData = 'data:application/csv;base64,' + this.base64(this.jsonToCSV(data, keys))
this.download(CSVData, fileName)
},
base64ToBlob: function (base64Data) {
var arr = base64Data.split(',')
var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(arr[1])
var n = bstr.length
var u8arr = new Uint8ClampedArray(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: mime })
},
download: function (base64data, fileName) {
if (window.navigator.msSaveBlob) {
var blob = this.base64ToBlob(base64data)
window.navigator.msSaveBlob(blob, filename)
return false;
}
var a = document.getElementById(this.id_name);
if (window.URL.createObjectURL) {
var blob = this.base64ToBlob(base64data)
var blobUrl = window.URL.createObjectURL(blob)
a.href = blobUrl;
a.download = fileName;
return
}
if (alink.download === '') {
a.href = base64data
a.download = fileName;
return
}
}//end download
}
}
</script>