Dynamic loading of spreadsheet data
Hi all,
Is it possible to load Excel data into the SfSpreadsheet component on the fly, outside OnInitialized()?
Been trying this for hours now, no success.
Thanks!
Diego
Hi Diego van Beek,
Thank you for reaching out !
We have validated the reported query regarding loading Excel data into the Syncfusion SfSpreadsheet component. To meet your requirement, we have prepared a Blazor application that dynamically loads Excel data using both a file uploader and an external button.
- The SfUploader allows users to upload Excel files, which are read into a byte array (DataSourceBytes) using Syncfusion’s XlsIO library. This byte array is then used to render the spreadsheet.
- Alternatively, we have added the "Open Spreadsheet" button in which we have loads a predefined Excel file from the server into the same byte array, enabling flexible data loading outside the OnInitialized() lifecycle method.
Please refer to the code snippet and the attached sample for your reference.
<SfUploader AutoUpload="true"> <UploaderEvents ValueChange="@OnChange"></UploaderEvents> </SfUploader>
<Syncfusion.Blazor.Buttons.SfButton CssClass="e-primary" OnClick="ShowSpreadsheet">Open Spreadsheet</Syncfusion.Blazor.Buttons.SfButton>
@if ( DataSourceBytes != null) { <SfSpreadsheet @ref="SpreadsheetObj" DataSource="@DataSourceBytes"> <SpreadsheetRibbon></SpreadsheetRibbon> </SfSpreadsheet> }
@code { private SfSpreadsheet SpreadsheetObj; private byte[] DataSourceBytes;
private async Task OnChange(UploadChangeEventArgs args) { try { foreach (var file in args.Files) { var stream = new MemoryStream(); await file.File.OpenReadStream(long.MaxValue).CopyToAsync(stream); stream.Position = 0;
using var engine = new ExcelEngine(); var app = engine.Excel; var workbook = app.Workbooks.Open(stream);
using var exportStream = new MemoryStream(); workbook.SaveAs(exportStream); DataSourceBytes = exportStream.ToArray();
} } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } }
private void ShowSpreadsheet() {
string filePath = "wwwroot/Sample.xlsx"; DataSourceBytes = File.ReadAllBytes(filePath);
} } |
Blazor application: Please refer to the attachment.
Additionally, the Blazor Spreadsheet component supports Open and Save functionalities, allowing users to efficiently manage Excel files—open existing files for analysis or editing, and save updates or new files in compatible formats. For more information, please refer to the below documentation.
https://blazor.syncfusion.com/documentation/spreadsheet/open-and-save
Please review the above shared details and get back to us if you have any further questions or concerns.
Regards,
Guhanathan R
Attachment: Spreadsheet_(4)_54410004.zip
- 1 Reply
- 2 Participants
-
DV Diego van Beek
- Aug 13, 2025 08:16 AM UTC
- Aug 15, 2025 08:37 AM UTC