public exportAsExcelFile(json: any[], excelFileName: string): void {
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
const workbook: XLSX.WorkBook = {Sheets: {'data': worksheet}, SheetNames: ['data']};
XLSX.writeFile(workbook, ExcelService.toExportFileName(excelFileName));
}
exportToExcel() {
this.accountCreationService.getExcelDataForExport().subscribe((response)=>{
if(response!=null && response.CaseList!=null && response.CaseList.length>0)
this.excelService.exportAsExcelFile(response.CaseList, 'TestData');
else
console.log('ERROR Fetching Excel Data From API')
},(error)=>{
console.log('ERROR : unable to export to EXCEL : ' + error);
})
}
我的要求是将我从API获得的数据导出到Excel工作表中。
Response Payload:
{
"pyStatusWork": "New",
"LegalCompanyName": "",
"EnablePend": null,
"DisplayId": "",
"IsLocked": false,
"taxExemptJurisdictionValue": "",
"pzInsKey": "",
"Origination": {
"BatchID": ""
},
"Source": "",
"AccountMaintenance": {
"AccountStatus": {
"RequestorType": "",
"RequestSource": "",
"CodeStatus": ""
},
"TaskType": "",
"modifiedByUserId": ""
}
}
像这样我得到100条记录,第一个问题是,如果我将相同的有效负载导出到excel工作表中,则无法看到accountmaintenacne {}
和{{1 }}对象在电子表格中。
第二个问题,我想在电子表格中看到不同的键(“键”:“值”)名称。
答案 0 :(得分:0)
您可以创建一个新函数以将json对象重新格式化为简单的对象键:值
赞:
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(myCustomFunction(json));
myCustomFunction(json:any[]){
let newJson:any[];
angular.forEach(json, function(item) {
newJson.push({
"pyStatusWork": item.pyStatusWork,
"LegalCompanyName": item.LegalCompanyName,
"EnablePend": item.EnablePend,
"DisplayId": item.DisplayId,
"IsLocked": item.IsLocked,
"taxExemptJurisdictionValue": item.taxExemptJurisdictionValue,
"pzInsKey": item.pzInsKey,
"Origination.BatchID": item.Origination.BatchID,
"Source": item.Source,
"AccountMaintenance.AccountStatus.RequestorType": item.AccountMaintenance.AccountStatus.RequestorType,
"AccountMaintenance.AccountStatus.RequestSource": item.AccountMaintenance.AccountStatus.RequestSource,
"AccountMaintenance.AccountStatus.CodeStatus": item.AccountMaintenance.AccountStatus.CodeStatus,
"TaskType": item.TaskType,
"modifiedByUserId": item.modifiedByUserId
}
});
});
return newJson;
}
如果对象字段是静态的,这应该可以工作。
祝你好运。