i have following code, should encode json object xlsx , download it:
this.data = { foo: "xyz" } let json2xls = require('json2xls'); var data = json2xls(this.data); let blob = new blob([data], { type: "binary" }); let = angular.element("a"); a.attr("href", this.$window.url.createobjecturl(blob)); a.attr("download", "myfile.xlsx"); a[0].click();
it indeed creates , downloads file, excel cannot open it. know sure below converting method works, because can send this.data
server, save using fs.writefile()
, download file.
var data = json2xls(this.data);
how can parse json xls, , save xls in browser?
this can done server side:
- install exceljs
meteor npm install -s exceljs
- then can generate file way:
import excel 'exceljs';
webapp.connecthandlers.use('/your-download-url', function(req, res, next) { // use params specify parameters xls generation process... const params = req.url.split('/'); // ... example /your-download-url/your-user-id // params[0] 'your-download-url' const user = meteor.users.findone(params[1]); const workbook = new excel.stream.xlsx.workbookwriter({}); workbook.created = new date(); workbook.modified = new date(); const sheet = workbook.addworksheet('your sheet name'); res.writehead(200, { 'content-disposition': `attachment;filename=${filename}`, 'content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }); workbook.stream.pipe(res); const headers: [ { header: 'column 1', key: 'col1', width: 20 }, { header: 'column 2', key: 'col2', width: 15 }, ]; sheet.columns = headers; // user sheet.addrow(rowdata) add each row in sheet. workbook.commit(); });
Comments
Post a Comment