Skip to content

Instantly share code, notes, and snippets.

@stuwilli
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuwilli/16c9a6aa8bdfcc12e0b5 to your computer and use it in GitHub Desktop.
Save stuwilli/16c9a6aa8bdfcc12e0b5 to your computer and use it in GitHub Desktop.
JS-XLSX Create Workook
var ws_name = 'SheetJS';
/* set up workbook objects -- some of these will not be required in the future */
var wb = {};
wb.Sheets = {};
wb.Props = {};
wb.SSF = {};
wb.SheetNames = [];
/* create worksheet: */
var ws = {}
/* the range object is used to keep track of the range of the sheet */
var range = {s: {c:0, r:0}, e: {c:0, r:0 }};
for (var r=0; r !== data.length; r++) {
if (range.e.r < r) {
range.e.r = r;
}
var c = 0;
for (var k in data[r]) {
if(range.e.c < c) {
range.e.c = c;
}
var cell = { v: data[r][k].toString() };
if (cell.v === null) {
continue;
}
/* create the correct cell reference */
var cell_ref = XLSX.utils.encode_cell({c:c,r:r});
/* determine the cell type */
if(typeof cell.v === 'number') {
cell.t = 'n';
} else if ( typeof cell.v === 'boolean') {
cell.t = 'b';
} else {
cell.t = 's';
}
/* add to structure */
ws[cell_ref] = cell;
c++;
}
}
ws['!ref'] = XLSX.utils.encode_range(range);
/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
/* write file */
XLSX.writeFile(wb, '/Users/stuwilli/Projects/fw-cli/test.xlsx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment