Skip to content

Commit dcd327a

Browse files
committed
@ Adds main component.
1 parent f33e6b2 commit dcd327a

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/components/Json2Excel.vue

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<template>
2+
<div @click="exportToExcel" class="main">
3+
<slot></slot>
4+
</div>
5+
</template>
6+
7+
<script>
8+
import xlsx from 'xlsx';
9+
export default {
10+
props: {
11+
header: {
12+
type: Array,
13+
default() {
14+
return [];
15+
}
16+
},
17+
data: {
18+
type: Array,
19+
default() {
20+
return [];
21+
}
22+
},
23+
details: {
24+
type: Object,
25+
default() {
26+
return {};
27+
}
28+
}
29+
},
30+
name: "Json2Excel",
31+
data() {
32+
return {
33+
workBook: {},
34+
workSheet: {}
35+
}
36+
},
37+
methods: {
38+
init() {
39+
this.workBook = xlsx.utils.book_new();
40+
},
41+
makeDetails() {
42+
let width = this.header.length, height = Object.keys(this.details).length;
43+
44+
this.workSheet['!merges'] = [];
45+
this.workSheet['!merges'].push({s: {r: 0, c: 0}, e: {r: height - 1, c: width - 1}});
46+
47+
let header = xlsx.utils.encode_cell({r: 0, c: 0});
48+
49+
let value = '___';
50+
for (let k in this.details)
51+
if (this.details.hasOwnProperty(k))
52+
value += k + ' : ' + this.details[k] + '___\n';
53+
54+
this.workSheet[header] = {v: value};
55+
},
56+
makeHeader() {
57+
this.header.forEach((h, index) => {
58+
let cellRef = xlsx.utils.encode_cell({
59+
r: Object.keys(this.details).length,
60+
c: index
61+
});
62+
this.workSheet[cellRef] = {v: h};
63+
})
64+
},
65+
makeData() {
66+
this.data.forEach((row, rowIndex) => {
67+
row.forEach((col, colIndex) => {
68+
let cellRef = xlsx.utils.encode_cell({
69+
r: rowIndex + Object.keys(this.details).length + 1,
70+
c: colIndex
71+
});
72+
this.workSheet[cellRef] = {v: col}
73+
})
74+
})
75+
},
76+
makeRange() {
77+
let rows = Object.keys(this.details).length + this.data.length,
78+
cols = this.header.length;
79+
80+
let range = {
81+
s: {r: 0, c: 0},
82+
e: {r: rows, c: cols}
83+
};
84+
85+
this.workSheet['!ref'] = xlsx.utils.encode_range(range);
86+
87+
},
88+
makeExport() {
89+
this.workBook.SheetNames.push('test');
90+
this.workBook.Sheets.test = this.workSheet;
91+
92+
xlsx.writeFile(this.workBook, 'test.xlsx');
93+
},
94+
exportToExcel() {
95+
this.makeDetails();
96+
this.makeHeader();
97+
this.makeData();
98+
this.makeRange();
99+
this.makeExport();
100+
}
101+
},
102+
created() {
103+
this.init();
104+
},
105+
}
106+
</script>
107+
108+
<style scoped>
109+
.main:hover{
110+
cursor: pointer;
111+
}
112+
</style>

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Json2Excel from './components/Json2Excel'
2+
3+
export default Json2Excel;

0 commit comments

Comments
 (0)