express - How to download created excel file in node.js using exceljs -
i using exceljs module creating excel file. problem is neither getting created nor getting saved in path.
var excel = require('exceljs'); var options = { filename: './streamed-workbook.xlsx', usestyles: true, usesharedstrings: true }; var workbook = new excel.stream.xlsx.workbookwriter(options); var sheet = workbook.addworksheet('my sheet'); worksheet.columns = [ { header: 'id', key: 'id', width: 10 }, { header: 'name', key: 'name', width: 32 }, { header: 'd.o.b.', key: 'dob', width: 10 } ]; worksheet.addrow({id: 1, name: 'john doe', dob: new date(1970,1,1)}); worksheet.addrow({id: 2, name: 'jane doe', dob: new date(1965,1,7)}); worksheet.commit(); workbook.commit().then(function(){ console.log('xls file written.'); });
but when run code nothing happens. excel not created. missing here?
*********************** edit ************************** made following changes code still not working.
var excel = require('exceljs'); var workbook = new excel.workbook(); var worksheet = workbook.addworksheet('my sheet'); worksheet.columns = [ { header: 'id', key: 'id', width: 10 }, { header: 'name', key: 'name', width: 32 }, { header: 'd.o.b.', key: 'dob', width: 10 } ]; worksheet.addrow({id: 1, name: 'john doe', dob: new date(1970,1,1)}); worksheet.addrow({id: 2, name: 'jane doe', dob: new date(1965,1,7)}); workbook.commit(); workbook.xlsx.writefile('./temp.xlsx').then(function() { // done console.log('file written'); });
so figured out getting error because of workbook.commit(). removed commit , started working charm. here entire working code of creating , downloading excel file:
note: using npm module called tempfile create temporary file path created excel file. file path automatically removed. hope helps.
try { var workbook = new excel.workbook(); var worksheet = workbook.addworksheet('my sheet'); worksheet.columns = [ { header: 'id', key: 'id', width: 10 }, { header: 'name', key: 'name', width: 32 }, { header: 'd.o.b.', key: 'dob', width: 10 } ]; worksheet.addrow({id: 1, name: 'john doe', dob: new date(1970,1,1)}); worksheet.addrow({id: 2, name: 'jane doe', dob: new date(1965,1,7)}); var tempfilepath = tempfile('.xlsx'); workbook.xlsx.writefile(tempfilepath).then(function() { console.log('file written'); res.sendfile(tempfilepath, function(err){ console.log('---------- error downloading file: ' + err); }); }); } catch(err) { console.log('ooooooo error: ' + err); }
Comments
Post a Comment