Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple sheets support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 32 additions & 42 deletions VueExcelXlsx.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,58 @@

<script>
import XLSX from 'xlsx/xlsx';
window.$ = window.jQuery = require('jquery');

export default {
name: "vue-excel-xlsx",

props: {
columns: {
sheets: {
type: Array,
default: []
},
data: {
type: Array,
default: []
default: () => {
return [ { name: 'SheetName', columns: [], data: [] } ]
}
},
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);
});
let wb = XLSX.utils.book_new()

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]);
}
this.sheets.forEach(sheet => {
if (sheet.columns.length === 0 || sheet.data.length === 0){
console.log(`Columns or data of sheet '${sheet.name}' missing! Sheet skipped.`);
} else {
let createXLSLFormatObj = [];
let newXlsHeader = [];
sheet.columns.forEach(value => {
newXlsHeader.push(value.label);
});
createXLSLFormatObj.push(innerRowData);
});

let filename = vm.filename + ".xlsx";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to re-add this part, user isn't going to specify the file extension since he's expecting it to be an xlsx.

createXLSLFormatObj.push(newXlsHeader);
sheet.data.forEach(value => {
let innerRowData = [];
sheet.columns.forEach(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 ws_name = vm.sheetname;
let ws_name = sheet.name;

let ws = XLSX.utils.aoa_to_sheet(createXLSLFormatObj);
XLSX.utils.book_append_sheet(wb, ws, ws_name);
}
})

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);
XLSX.writeFile(wb, this.filename);
}
}
}
Expand Down
Loading