Ray Koren
Ray Koren

Reputation: 864

EPPLus Not Returning File

EPPLus is not returning a File. No errors on build or console errors.

Network Tab is showing completed 200 ok.

public ActionResult DownloadExcel(EntityReportModel erModel, string filename)
{
     var dataResponse = iEntityViewService.LoadEntityView(new EntityViewInput
     {
          SecurityContext = SessionCache.Instance.SecurityContext,
          EntityViewName = "Ticket",
          Parameters = new Dictionary<string, object> {
              {"MinTicketDateTime", "04/26/16"}

          }
     });

     var table = dataResponse.DataSet.Tables[0];

     filename = "NGLTICKETS";
     MemoryStream stream = new MemoryStream();
     using (ExcelPackage pack = new ExcelPackage())
     {
          ExcelWorksheet ws = pack.Workbook.Worksheets.Add(filename);
          ws.Cells["A1"].LoadFromDataTable(table, true);

          return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename);

     }
}

Upvotes: 0

Views: 1729

Answers (1)

Christian Gollhardt
Christian Gollhardt

Reputation: 17034

You do not manipulate your output stream, hence you will get no result.

You can fill your stream by doing var stream = new MemoryStream(pack.GetAsByteArray()). As alternative pack.SaveAs(stream).

So your code should look something like that:

 using (ExcelPackage pack = new ExcelPackage())
 {
      ExcelWorksheet ws = pack.Workbook.Worksheets.Add(filename);
      ws.Cells["A1"].LoadFromDataTable(table, true);

      var stream = new MemoryStream(pack.GetAsByteArray()); //Get updated stream
      return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename);    

      //or simple return File(excelPackage.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename)
 }

Upvotes: 5

Related Questions