Blazor Excel Library Overview
The Infragistics Blazor Excel Library allows you to work with spreadsheet data using familiar Microsoft® Excel® spreadsheet objects like Workbook
, Worksheet
, Cell
, Formula
and many more. The Infragistics Blazor Excel Library makes it easy for you to represent the data of your application in an Excel spreadsheet as well as transfer data from Excel into your application.
Blazor Excel Library Example
Requirements
In order to use the Blazor excel library, you need to add the following using statement:
@using Infragistics.Documents.Excel
If you are using a Web Assembly (WASM) Blazor project, there are a couple of extra steps:
- Add a reference to the following script in the wwwroot/index.html file:
<script src="_content/IgniteUI.Blazor.Documents.Excel/excel.js"></script>
- Set the static
Workbook.InProcessRuntime
to the current runtime. This can be done by using the following code:
@using Microsoft.JSInterop
@code {
[Inject]
public IJSRuntime Runtime { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
Workbook.InProcessRuntime = (IJSInProcessRuntime)this.Runtime;
}
}
Supported Versions of Microsoft Excel
The following is a list of the supported versions of Excel.**
Microsoft Excel 97
Microsoft Excel 2000
Microsoft Excel 2002
Microsoft Excel 2003
Microsoft Excel 2007
Microsoft Excel 2010
Microsoft Excel 2013
Microsoft Excel 2016
Load and Save Workbooks
Now that the Excel Library module is imported, next step is to load a workbook.
In order to load and save Workbook
objects, you can utilize the save method of the actual Workbook
object, as well as its static Load
method.
protected override void OnInitialized()
{
var memoryStream = new System.IO.MemoryStream();
workbook.Save(memoryStream);
memoryStream.Position = 0;
var bytes = memoryStream.ToArray();
this.SaveFile(bytes, "fileName.xlsx", string.Empty);
}
private void SaveFile(byte[] bytes, string fileName, string mime)
{
if (this.Runtime is WebAssemblyJSRuntime wasmRuntime)
wasmRuntime.InvokeUnmarshalled<string, string, byte[], bool>("BlazorDownloadFileFast", fileName, mime, bytes);
else if (this.Runtime is IJSInProcessRuntime inProc)
inProc.InvokeVoid("BlazorDownloadFile", fileName, mime, bytes);
}
API References
Load
WorkbookInProcessRuntime
Worksheet
Workbook