You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Document-Processing/Excel/Spreadsheet/Blazor/open-and-save.md
+310Lines changed: 310 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,6 +72,316 @@ An Excel file encoded as a Base64 string can be loaded into the Spreadsheet comp
72
72
{% endhighlight %}
73
73
{% endtabs %}
74
74
75
+
### Open an Excel file from JSON data
76
+
77
+
The Blazor Spreadsheet component accepts data only as a byte array through the [DataSource](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_DataSource) property. To load JSON data into the Spreadsheet, convert the JSON data into an Excel file format using [XlsIO](https://help.syncfusion.com/file-formats/xlsio/overview), then convert it to a byte array. This approach allows importing JSON data from a local file or a remote URL.
78
+
79
+
#### Load an Excel file from a local JSON file
80
+
81
+
JSON data can be loaded from a local JSON file, converted to Excel format using XlsIO, and displayed in the Spreadsheet component. This approach is useful when working with static JSON data files within the application. The implementation reads the JSON file, converts it to Excel format using XlsIO, and binds it to the Spreadsheet as a byte array.
82
+
83
+
{% tabs %}
84
+
{% highlight razor tabtitle="Index.razor" %}
85
+
86
+
@using System.Text.Json
87
+
@using Syncfusion.XlsIO
88
+
@using Syncfusion.Blazor.Spreadsheet
89
+
90
+
<SfSpreadsheetDataSource="DataSourceBytes">
91
+
<SpreadsheetRibbon></SpreadsheetRibbon>
92
+
</SfSpreadsheet>
93
+
94
+
@code {
95
+
96
+
public byte[] DataSourceBytes { get; set; }
97
+
98
+
protected override void OnInitialized()
99
+
{
100
+
// Build the file path to the JSON data source
101
+
// Note: Replace "wwwroot" and "sample.json" with the actual folder and file name where your JSON is stored.
// If not an object, wrap the value in a dictionary with "value" key
210
+
if (jsonElement.ValueKind != JsonValueKind.Object)
211
+
{
212
+
return new Dictionary<string, JsonElement> { ["value"] = jsonElement };
213
+
}
214
+
215
+
// Enumerate all properties and convert to dictionary
216
+
return jsonElement.EnumerateObject()
217
+
.ToDictionary(
218
+
property => property.Name,
219
+
property => property.Value,
220
+
StringComparer.OrdinalIgnoreCase
221
+
);
222
+
}
223
+
}
224
+
225
+
{% endhighlight %}
226
+
{% endtabs %}
227
+
228
+
#### Load an Excel file from a remote JSON URL
229
+
230
+
Remote JSON data can be integrated into the Spreadsheet component by converting it into an Excel-compatible format. The process begins with asynchronous retrieval of JSON from the specified endpoint using HttpClient. The fetched data is then transformed into an Excel workbook through XlsIO, and the resulting byte array is passed to the Spreadsheet for rendering. This approach is particularly useful for integrating real-time data from REST APIs or other web services.
231
+
232
+
{% tabs %}
233
+
{% highlight razor tabtitle="Index.razor" %}
234
+
235
+
@using System.Text.Json
236
+
@using Syncfusion.XlsIO
237
+
@using Syncfusion.Blazor.Spreadsheet
238
+
@inject HttpClient HttpClient
239
+
240
+
@if (IsDataLoaded)
241
+
{
242
+
<SfSpreadsheetDataSource="DataSourceBytes">
243
+
<SpreadsheetRibbon></SpreadsheetRibbon>
244
+
</SfSpreadsheet>
245
+
}
246
+
@code {
247
+
248
+
public byte[] DataSourceBytes { get; set; }
249
+
250
+
// Flag to indicate whether the data has been loaded
0 commit comments