I am currently working on populating an excel file by utilizing the JSON data provided below. This JSON data is retrieved from an HTTP response and I intend to utilize it for downloading an excel file.
{
"dynaModel":[
{
"map":{
"UNIT/SUBUNIT":"sdasd",
"SUBUNIT/ISU/GEO":"sasd",
"REVENUEINR-RS":"₹87,sdd",
"COSTINR-RS":"₹47,33",
"GMINR-RSUSD-$":46,
"REVENUEINR-RS":"₹87,64,",
"COSTINR-RS":"₹47,33,",
"GMINR-RSUSD-$":46
}
},
{
"map":{
"UNIT/SUBUNIT":"fghf",
"SUBUNIT/ISU/GEO":"CMghhfI",
"REVENUEINR-RS":"₹59,06",
"COSTINR-RS":"₹30,43",
"GMINR-RSUSD-$":48.47,
"REVENUEINR-RS":"₹59",
"COSTINR-RS":"₹30,43",
"GMINR-RSUSD-$":48.47
}
},
{
"map":{
"UNIT/SUBUNIT":"hfgh",
"SUBUNIT/ISU/GEO":"fghh",
"'APR-16'_REVENUEINR-RS":"₹29,72",
"'APR-16'_COSTINR-RS":"₹11,43",
"'APR-16'_GMINR-RSUSD-$":61.53,
"'Total'_REVENUEINR-RS":"₹29,72",
"'Total'_COSTINR-RS":"₹11,43",
"'Total'_GMINR-RSUSD-$":61.53
}
}
]
}
The code snippet of my components can be seen below:
excelDownload(){
this._isuGeoSubunitReportService.excelDownload(this.isugeosubunitTO)
.subscribe(data =>this.responseStatus = data,
err => console.log(err),
() => console.log('Request Completed')
);
const ws_name = 'SomeSheet';
const wb: WorkBook = { SheetNames: [], Sheets: {} };
const ws: any = utils.json_to_sheet(this.responseStatus.dynamoModel);
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
const wbout = write(wb, { bookType: 'xlsx', bookSST: true, type: 'binary' });
function s2ab(s) {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i !== s.length; ++i) {
view[i] = s.charCodeAt(i) & 0xFF;
};
return buf;
}
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), 'exported.xlsx');
}
I am attempting to download the result in xlsx
format using the xlsx
module of json. While it works well for simple JSON data, I am encountering challenges with my complex JSON data.
const ws: any = utils.json_to_sheet(this.responseStatus);
If I directly use this.responseStatus
, only one map
value is returned in the excel sheet.