1

I am new to Angular and I have a problem. I'm trying to export data when I click a button.

Here is my component code:

exportExcel() {
    this.orderService.exportExcel().subscribe(
      () => {
        alert("The downloading process has started");
      }
    );
}

Here is my service code

  exportExcel = (): Observable<any> => {
    return this.http.put(this.configuration.Server + "ExcelExport/", {headers:this.headers});
  }

And here is my api controller code

[HttpPut]
public async Task<IActionResult> Put()
{
    var user = await GetCurrentUserAsync();
    var orders = await _orderRepository.GetOrderStatus(user);

    StringBuilder sb = new StringBuilder();
    sb.Append("<table border=`" + "1px" + "`b>");
    sb.Append("<tr>");
    sb.Append("<td><b><font face=Arial Narrow size=3>FName</font></b></td>");
    sb.Append("</tr>");

    foreach (var data in orders)
    {
        sb.Append("<tr>");
        sb.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + data.Price + "</font></td>");
        sb.Append("<tr>");
    }

    sb.Append("</table>");

    HttpContext.Response.Headers.Add("content-disposition", "attachment; filename=Information.xls");
    Response.ContentType = "application/vnd.ms-excel";
    byte[] temp = Encoding.UTF8.GetBytes(sb.ToString());

    return File(temp, "application/excel");
}

When I click the button, I just want to start downloading the export - but it's not doing that. Please help me - thank you

Laoujin
  • 9,962
  • 7
  • 42
  • 69

0 Answers0