Return Excel file from core API

Viewed 223

I have dotnet Core API which returns excel file(using ClosedXML) data and consuming this API in angular API Method

   using (MemoryStream stream = new MemoryStream())
        {
            var workbook = new XLWorkbook();

            var SheetNames = new List<string>() { "15-16", "16-17", "17-18", "18-19", "19-20" };

            foreach (var sheetname in SheetNames)
            {
                var worksheet = workbook.Worksheets.Add(sheetname);

                worksheet.Cell("A1").Value = sheetname;
            }

            workbook.SaveAs(stream);
            stream.Seek(0, SeekOrigin.Begin);

            return this.File(
                fileContents: stream.ToArray(),
                contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",

                // By setting a file download name the framework will
                // automatically add the attachment Content-Disposition header
                fileDownloadName: "ERSheet.xlsx"
            );
        }

Passing Header for API request in angular

   'Content-Type': 'aapplication/vnd.ms-excel',
    'Authorization': 'Bearer ' + token
 

returned data in the console Api Response

And here is the code once I get the response from API in angular

  download(data: Blob) { 
const contentType = 'application/vnd.openxmlformats-ficedocument.spreadsheetml.sheet'; 
const blob = new Blob([data], { type: contentType });
const url = window.URL.createObjectURL(blob);
window.open(url);

}

but this is not working. Any idea how to return the excel file data from API and download on the angular side on a button click.

0 Answers
Related