Search code examples
c#asp.net-mvcexcelepplus

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);

     }
}

Solution

  • 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)
     }