Skip to content

Commit 09b7392

Browse files
committed
@ Adds filename property and fixes some bugs about duplicate sheet name.
1 parent 3549cef commit 09b7392

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/components/Json2Excel.vue

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,24 @@
2525
default() {
2626
return {};
2727
}
28-
}
28+
},
29+
filename: {
30+
type: String,
31+
default: 'vue-json2-excel'
32+
},
2933
},
3034
name: "Json2Excel",
3135
data() {
3236
return {
3337
workBook: {},
34-
workSheet: {}
38+
workSheet: {},
39+
counter: 0,
3540
}
3641
},
3742
methods: {
3843
init() {
44+
this.workBook = {};
45+
this.workSheet = {};
3946
this.workBook = xlsx.utils.book_new();
4047
},
4148
makeDetails() {
@@ -86,27 +93,26 @@
8693
8794
},
8895
makeExport() {
89-
this.workBook.SheetNames.push('test');
96+
let name = `${this.filename}${this.counter ? '('+ this.counter++ + ')' : ''}`;
97+
this.workBook.SheetNames = [name];
9098
this.workBook.Sheets.test = this.workSheet;
9199
92-
xlsx.writeFile(this.workBook, 'test.xlsx');
100+
xlsx.writeFile(this.workBook, `${name}.xlsx`);
93101
},
94102
exportToExcel() {
103+
this.init();
95104
this.makeDetails();
96105
this.makeHeader();
97106
this.makeData();
98107
this.makeRange();
99108
this.makeExport();
100109
}
101110
},
102-
created() {
103-
this.init();
104-
},
105111
}
106112
</script>
107113

108114
<style scoped>
109-
.main:hover{
115+
.main:hover {
110116
cursor: pointer;
111117
}
112118
</style>

0 commit comments

Comments
 (0)