Blazor Server - downloading excel in the controller

Cenk 956 Reputation points
2023-06-06T06:49:55.87+00:00

Hi,

In my Blazor Server application, I am trying to write data into Excel and download it in the controller. Here is the controller;

var existingRecords = await _stockUniqueSerialPinUseCase.ExecuteAsync(customerList);
if (existingRecords.Count > 0)
{
       await Export(existingRecords);
}

private async Task Export(List<GameBank> existingRecords)
    {
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
        var stream = new MemoryStream();

        using (var package = new ExcelPackage(stream))
        {
            var workSheet = package.Workbook.Worksheets.Add("Duplicate Codes");
            workSheet.Protection.IsProtected = true;

            var recordIndex = 2;

            workSheet.Row(1).Style.Font.Bold = true;
            var headerCells = workSheet.Cells["A1:B1"];
            headerCells.Style.Font.Bold = true;
            headerCells.Style.Font.Size = 13;
            headerCells.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            headerCells.Style.Border.Top.Style = ExcelBorderStyle.Thin;
            headerCells.Style.Border.Left.Style = ExcelBorderStyle.Thin;
            headerCells.Style.Border.Right.Style = ExcelBorderStyle.Thin;
            headerCells.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
            headerCells.Style.Font.Color.SetColor(System.Drawing.Color.Black);
            headerCells = workSheet.Cells["A1:B1"];
            // Set their background color to DarkBlue.
            headerCells.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            headerCells.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);

            workSheet.Cells[1, 1].Value = "Serial";
            workSheet.Cells[1, 2].Value = "Pin";

            foreach (var duplicate in existingRecords)
            {
                workSheet.Cells[recordIndex, 1].Value = duplicate.coupons.Serial;
                workSheet.Cells[recordIndex, 2].Value = duplicate.coupons.Pin;
                recordIndex++;
            }

            //Make all text fit the cells
            workSheet.Cells[workSheet.Dimension.Address].AutoFitColumns();
            await package.SaveAsync();
        }
        stream.Position = 0;
        var excelName = $"RazerDuplicateCodesList-{DateTime.Now:ddMMyyyyHHmm}.xlsx";


        using var streamRef = new DotNetStreamReference(stream: stream);

        await _jsRuntime.InvokeVoidAsync("downloadFileFromStream", excelName, streamRef);
    }

But I am getting this error; how can I fix this?

JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,404 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2023-06-06T17:31:08.79+00:00

    as the error suggests, move the js interop code to the OnAfterRenderAsync event. as the stream is outside the blazor compoent render process, it makes sense.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more