I am attempting to add an export to excel button on my blazor server side app. So far after combing the internet this is what I have done.
My button
<div class="row text-right">
<div class="col-12 p-3">
<button class="btn btn-outline-success" @onclick="@(() =>DownloadExcel(formValues.Region, formValues.startDate, formValues.endDate))">
Export to Excel
<i class="fa fa-file-excel" aria-hidden="true"></i>
</button>
</div>
</div>
My method in my .razor page
public FileResult DownloadExcel(string Region, DateTime StartDate, DateTime EndDate)
{
FileResult ExcelFile = searchService.ExportToExcel(Region, StartDate, EndDate);
return ExcelFile;
}
And Finally my logic in my service
public FileResult ExportToExcel(string Region, DateTime StartDate, DateTime EndDate)
{
var queryable = context.AuditCardPinrecords.Where(s => Region == s.RegionRecordId)
.Where(s => s.AuditComplete == true)
.Where(s => s.DateTime >= StartDate && s.DateTime <= EndDate).AsQueryable();
var stream = new MemoryStream();
using (var package = new ExcelPackage(stream))
{
var workSheet = package.Workbook.Worksheets.Add("Sheet1");
workSheet.Cells.LoadFromCollection(queryable, true);
package.Save();
}
string excelName = $"AuditPinRecords-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName);
}
My expected result is to download the excel file. Unfortunately nothing happens on button click. Any advice would be greatly appreciated. Thanks!