Last active
December 30, 2019 06:29
-
-
Save railsstudent/de33c01f1d44ff1adedd56d621e62556 to your computer and use it in GitHub Desktop.
use sheetjs to write and save a xlsx file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/SheetJS/sheetjs/tree/6551dd0e051acac5031ffb728a16932bbf34c80a | |
// npm install xlsx | |
import * as XLSX from 'xlsx'; | |
const { writeFile, utils } = XLSX; | |
console.log('Create workbook'); | |
const wb = utils.book_new(); | |
const scoreData = [ { | |
col1: 'A1', | |
col2: 'B2', | |
col3: 'C3' | |
}, { | |
col1: 'A1', | |
col2: 'B2', | |
col3: 'C3' | |
}, { | |
col1: 'A1', | |
col2: 'B2', | |
col3: 'C3' | |
}]; | |
const documentData = [ { | |
col1: 'D1', | |
col2: 'E2', | |
col3: 8 | |
}, { | |
col1: 'D1', | |
col2: 'E2', | |
col3: 9 | |
}, { | |
col1: 'D1', | |
col2: 'E2', | |
col3: 10 | |
}] | |
const header = { header: ['col1', 'col2', 'col3'] }; | |
const score_ws = utils.json_to_sheet(scoreData, header); | |
const document_ws = utils.json_to_sheet(documentData, header); | |
utils.book_append_sheet(wb, score_ws, 'Scores'); | |
utils.book_append_sheet(wb, document_ws, 'Documents'); | |
writeFile(wb, `hello_${Date.now()}.xlsx`, { bookType: 'xlsx' }); | |
console.log('Done') |
// return stream to browser to force client download
var buffer = XLSX.write(new_wb, {type:'buffer', bookType:'xlsx'});
return bufer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tsc sheetjs_write_xlsx.ts
node ./sheetjs_write_xlsx.js