0

I have an issue with finding the proper jQuery or JavaScript code to allow users to download an excel file that was generated in the C# API controller, tried many different ways and none of them worked, My backend code: (used ClosedXML.Excel package)

public ApiResponse DownloadAllMembersFile(IEnumerable<IMember> members)
{


    using (var workbook = new XLWorkbook())
    {
        var worksheet = workbook.Worksheets.Add("AllMembers");
        var currentRow = 1;

        #region Header
        worksheet.Cell(currentRow, 1).Value = "StudentId";
        worksheet.Cell(currentRow, 2).Value = "Name";
        worksheet.Cell(currentRow, 3).Value = "Email";
        #endregion

        #region Body
        foreach (var member in members)
        {
            currentRow++;
            worksheet.Cell(currentRow, 1).Value = member.Id;
            worksheet.Cell(currentRow, 2).Value = member.Name;
            worksheet.Cell(currentRow, 3).Value = member.Email;
        }
        #endregion

        using (var stream = new MemoryStream())
        {
            workbook.SaveAs(stream);
            var content = stream.ToArray();
            return new ApiResponse( true,"done",File(
                content,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "AllMembers.xlsx"
                ));

        }

    }
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Ziad
  • 33
  • 1
  • 4
  • What steps do users have to go through to request and API response in the first place - is it form submission, clicking a download link or something else? How is the user intended to view the the downloaded spreadsheet? "I tried many different ways and none of them worked" provides no information for us to help you with. – traktor Jun 29 '22 at 08:33
  • it's just downloading the file when the button is clicked , when it's downloaded as xlsx file the user will be able to view the file using excel – Ziad Jun 29 '22 at 08:47

0 Answers0