1

Hi I recently started working on angular and I have a requirement to create and download excel file using asp.net web api and angular 8

I created excel file using Asp.net Web API and returning file as

return File(excelPackage.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test.xlsx");

I have to read this excel file using angular and download

GetExportExcel(editQuoteFilters: EditQuoteModel):Observable<any>{ 
      let postHeaders = new HttpHeaders();
      postHeaders.append('Content-Type', 'application/json');
      postHeaders.append('responseType', 'blob');
      return this.http.post<any>(this.ExcelExportLevel0ApiUrl, editQuoteFilters,{ headers: postHeaders, 
           responseType : 'arraybuffer' as 'json'})                                      
          .pipe(map((res) =>{
                  var blob = new Blob([res.blob()], {type: "application/vnd.ms-excel"})
                  return blob
                })
          );
    }
0

1 Answer 1

1

You can use file-saver

import { saveAs } from 'file-saver';

this.http.post(this.ExcelExportLevel0ApiUrl, editQuoteFilters, { responseType: 'blob' }).subscribe((resp: any) => {
   let fileName = "test";
   FileSaver.saveAs(resp, `${fileName}.xlsx`);
});

Stackbiltz code

Sign up to request clarification or add additional context in comments.

2 Comments

Following is working for me this.http.post<any>(this.ExcelExportLevel0ApiUrl, editQuoteFilters,{responseType : 'blob' as 'json'}) .pipe(map((res: any) => { return new Blob([res],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}) })).subscribe(data => saveAs(data,'InventoryUploadStatus'));
Great :) Please mark the answer as correct if this was useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.