In this sample I am using a node module called ‘ ExcelJS‘. This will stream the data to the file instead of big data in memory (if we are keeping large amount of data in memory, we may get `JavaScript heap out of memory` exception ) this will write to the file
Install the module using npm
npm install exceljs --save
then in the node application just import the module
const Excel = require('exceljs'); const options = { filename: 'myfile.xlsx', useStyles: true, useSharedStrings: true }; const workbook = new Excel.stream.xlsx.WorkbookWriter(options); const worksheet = workbook.addWorksheet('my sheet'); worksheet.columns = [ { header: 'Id', key: 'id' }, { header: 'First Name', key: 'first name' }, { header: 'Phone', key: 'ph' } ] var data; for(let i = 1; i<= 10; i++){ data = { id: i, 'first name': "name "+i, ph: "012014520"+i }; worksheet.addRow(data).commit(); } workbook.commit().then(function() { console.log('excel file cretaed'); });
we can use the same module in typescript also
refer : exceljs