ahmed elbarbary

ahmed elbarbary

  • 637
  • 1.6k
  • 281.9k

when export data to excel error excel file not readable cont

May 9 2020 5:45 PM
problem
 
Note main issue related to type script because i can get data but i cannot display on Excel when export 
I work on angular 7 app I click button to export data from database to Excel it create file when i open it
give me error ( excel file not readable content ) why and how to solve issue this my problem .
I pass parameter value to stored procedure and execute it and get result from database then export it to excel .
  1. Exec exec dbo.getRevision 19253747  
Main issue why not export data to excel and how to make export correct this actually what i need .
web API
  1. [HttpPost]  
  2.   
  3. public IActionResult GetDataBasedOnDynamicSp([FromBody] dynamic DataObjectRevision)  
  4. {  
  5.   
  6. string SpName = DataObjectRevision[0].downLoadProcedureName;  
  7.   
  8. string revid = DataObjectRevision[0].revisionID;  
  9. var PostRevision = _reportservice.GetSpByRevisionId(revid, SpName);  
  10.   
  11. return Ok(PostRevision);  
  12.   
  13.   
  14.   
  15. }  
Angular service.ts
  1. GetSpByRevisionId(datarevision : any){  
  2. return this.http.post<any>('http://localhost:61265/api/report',datarevision)  
}
component.ts
  1. public exportJsonAsExcelFile(json: any[], excelFileName: string): void {  
  2. const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);  
  3. const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };  
  4. const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });  
  5. this.saveAsExcelFile(excelBuffer, excelFileName);  
  6. }  
  7. private saveAsExcelFile(buffer: any, fileName: string): void {  
  8. const data: Blob = new Blob([buffer], { type: EXCEL_TYPE });  
  9. FileSaver.saveAs(data, fileName + new Date().getTime() + EXCEL_EXTENSION);  
  10. }  
  11. getDataByRevision(RevId : any)  
  12. {  
  13.   
  14. this.ReportData.forEach(function(e) { e.revisionID=RevId });  
  15. this._displayreport.GetSpByRevisionId(this.ReportData).subscribe(data=>{  
  16. console.log(data)  
  17. this.exportJsonAsExcelFile(this.ReportData ,'datarevision');  
  18. });  
  19.   
  20.   
  21. }  
component .html
  1. <td> <button (click)="getDataByRevision(rep[0])">Download</button> </td>  
Data below is result of API and angular above and this data Exactly must show on Excel But this Not Done
  1. (3) [{…}, {…}, {…}]  
  2. 0: {revisionid: 19253747, revisionname: "parts", revisioncity: "usa"}  
  3. 1: {revisionid: 19253747, revisionname: "company", revisioncity: "russia"}  
  4. 2: {revisionid: 19253747, revisionname: "family", revisioncity: "german"}  
  5. length: 3  
  6. __proto__: Array(0)  

Answers (1)