javascriptc#asp.netexcelexcelpackage

.NET C# exporting to excel via JS post using ExcelPackage


I'm not sure what I'm missing here. I've got a button that when clicked, I'm using javascript to call a controller. This controller should create an excel file and return it to the user giving them the ability to download/save the file. I've tried a few different methods, but can't manage to get it to work. Here's my javascript side:

function exportList() {
    var val = $("#team-dropdown").val();
    const date = new Date().toISOString();
    const param = {
        "Date": date,
        "GroupID": 1
    }

    $.ajax({
        url: "@Url.Action("ExportToExcel", "Home")",
        type: "POST",
        data: param
    });
}

Here's my server side:

public FileResult ExportToExcel(DateTime date, int groupID)
        {
            Load l = new Load();
            List<Load> loadList = l.GetLoadsForGroup(date, groupID);

            var fileDownloadName = "fileName.xlsx";
            var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            ExcelPackage pck = new ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("New workbook");
            ws.View.ShowGridLines = true;
            ws.DefaultColWidth = 25;
            ws.Cells[1, 1].Value = "Order #";

            var currRow = 2;
            foreach (var load in loadList)
            {
                ws.Cells[2, 2].Value = load.LoadNumber;                    
            }



            var fs = new MemoryStream();
            pck.SaveAs(fs);
            fs.Position = 0;
            var fsr = new FileStreamResult(fs, contentType);
            fsr.FileDownloadName = fileDownloadName;
            return (fsr);
        }

Not sure what the best way to do this is. If there's a better way, please feel free to elaborate.


Solution

  • Your method looks fine. In that case you just need to use a html form to post instead of using the js function. Alternatively, if you would like to use a ActionResult you can write:

    public ActionResult ExportToExcel()
        {
            Load l = new Load();
            List<Load> loadList = l.GetLoadsForGroup(date, groupID);
    
            var fileDownloadName = "fileName.xlsx";
            var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
            ExcelPackage pck = new ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("New workbook");
            ws.View.ShowGridLines = true;
            ws.DefaultColWidth = 25;
            ws.Cells[1, 1].Value = "Order #";
    
            var currRow = 2;
            foreach (var load in loadList)
            {
                ws.Cells[2, 2].Value = load.LoadNumber;
            }
    
            Response.Clear();
            Response.ContentType = contentType;
            Response.AddHeader("content-disposition", "attachment; filename=\"" + fileDownloadName + "\"");
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.Flush();
            Response.End();
            return View();
        }
    

    And you get the same result as your method.