0

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

     }
}
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Ray Koren
  • 814
  • 14
  • 25

1 Answers1

5

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)
 }
Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • Thank you @Christian . Unfortunately those additions did not give me any more result. ;( – Ray Koren May 10 '17 at 22:10
  • Could you define *not returning a File* more specificaly @RayKoren? Do you get a white browser page (then what does the browser networktab say)? Ist the excelfile corrupt? – Christian Gollhardt May 10 '17 at 22:14
  • When I click my button I watch the network tab and the function runs and I see: DownloadExcel 200 xhr VM391:1 1.3 KB Request URL:https://localhost/Custom/Ticket/DownloadExcel Request Method:POST Status Code:200 OK Response Headers Cache-Control:no-cache, no-store, must-revalidate Content-Disposition:attachment; filename=NGLTICKETS Content-Length:0 Content-Security-Policy:default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet – Ray Koren May 10 '17 at 22:18
  • The page does not refresh. Literally nothing visibly happens on the front end. There is no message returned and no file download dialogue triggered. I appreciate the help. I have tried many many variations on this so far... – Ray Koren May 10 '17 at 22:18
  • @RayKoren see my updated answer. Looks like `MemoryStream` overload does not work the same as `FileName` overload I used. Does this version work? – Christian Gollhardt May 10 '17 at 22:25
  • SO that gave me a new result, but it is an issue I encountered in the past and thought I had mitigated. I get a popup with random characters in it. [screenshot](https://ibb.co/nQFFLk) looks like I am getting 13k of data back in that request tho... – Ray Koren May 10 '17 at 22:31
  • Seems like you are loading the excel file via ajax. Not sure about the topic binaryfiles and ajax @RayKoren – Christian Gollhardt May 10 '17 at 22:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143923/discussion-between-ray-koren-and-christian-gollhardt). – Ray Koren May 10 '17 at 22:37