Blazor Read Excel File in C# Using IronXL (Example Tutorial)

Introduction

Blazor is an open-source .NET Web framework that was created by Microsoft. A Blazor application works by compiling the C# code into browser-compliant JavaScript and HTML. In this tutorial, I'll share knowledge about the best and easy method for reading Excel document/worksheets in Blazor serverside application using the IronXL C# library.

Demonstration of IronXL Viewing Excel in Blazor

Step 1 - Create a Blazor Project in Visual Studio

I have an XLSX file containing the following data that I will read into and open in the Blazor Server App:

Input XLSX Excel SheetResult in Blazor Server Browser
First nameLast nameID
JohnApplesmith1
RichardSmith2
SherryRobins3

Start off by creating a Blazor Project from the Visual Studio IDE:

Choose the Blazor Server App Project type:

Go ahead and run the Application without changing the solution with the F5 key. Navigate to the Fetch data tab of the Application like so:

Our goal will be to load out Excel file into the Blazor app with an upload button and then display it on this page.

Step 2 - Add IronXL to your Solution

IronXL: .NET Excel Library (Installation Instructions):

IronXL is a .NET library that allows you to treat the spreadsheet in Microsoft Excel like an object, enabling the developer to use the full power of C# and the .NET Framework to manipulate data stream. As a developer, we want a nice way through which we can get every row cells and column information from Excel document/worksheet into our applications or database.

With IronXL, it is possible to get all sorts of information from a worksheet such as cell values, content of cells, images, references and formatting. IronXL is better than NPOI in many aspects. IronXL provides more functions and can make writing complex logic easier. It also has more preferable licenses and the support team is more competent.

IronXL supports all the latest versions of .NET (8, 7, and 6) and .NET Core Framework 4.6.2+.

Add IronXL to your solution using one of the methods below then build the solution.

Option 2A - Use NuGet Package Manager

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.

C# NuGet Library for Excel nuget.org/packages/IronXL.Excel/
Install-Package IronXL.Excel

Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip

Manually install into your project

Download DLL

Option 2B - Add PackageReference in the csproj file

You can add IronXL directly to your project by adding the following line to any <ItemGroup> in the .csproj file of your solution:

<PackageReference Include="IronXL.Excel" Version="*" />
XML

As shown here in Visual Studio:

Step 3 - Coding the File Upload and View

In the Visual Studio Solution View, go to the Pages/ folder and find the FetchData.razor file. You may use any other razor file but we will use this one because it comes with the Blazor Server App Template.

Replace the file replace contents with the following code:

@using IronXL;
@using System.Data;

@page "/fetchdata"

<PageTitle>Excel File Viewer</PageTitle>

<h1>Open Excel File to View</h1>

<InputFile OnChange="@OpenExcelFileFromDisk" />

<table>
    <thead>
        <tr>
            @foreach (DataColumn column in displayDataTable.Columns)
            {
                <th>
                    @column.ColumnName
                </th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in displayDataTable.Rows)
        {
            <tr>
                @foreach (DataColumn column in displayDataTable.Columns)
                {
                    <td>
                        @row [column.ColumnName].ToString()
                    </td>
                }
            </tr>
        }
    </tbody>
</table>

@code {
    // Create a DataTable
    private DataTable displayDataTable = new DataTable();

    // When a file is uploaded to the App using the InputFile, trigger:
    async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e)
    {
        IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY";

        // Open the File to a MemoryStream object
        MemoryStream ms = new MemoryStream();

        await e.File.OpenReadStream().CopyToAsync(ms);
        ms.Position = 0;

        // Define variables for IronXL
        WorkBook loadedWorkBook = WorkBook.FromStream(ms);
        WorkSheet loadedWorkSheet = loadedWorkBook.DefaultWorkSheet; // Or use .GetWorkSheet()

        // Add header Columns to the DataTable
        RangeRow headerRow = loadedWorkSheet.GetRow(0);
        for (int col = 0 ; col < loadedWorkSheet.ColumnCount ; col++)
        {
            displayDataTable.Columns.Add(headerRow.ElementAt(col).ToString());
        }

        // Populate the DataTable
        for (int row = 1 ; row < loadedWorkSheet.RowCount ; row++)
        {
            IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().Select(c => c.ToString());
            displayDataTable.Rows.Add(excelRow.ToArray());
        }
    }
}
@using IronXL;
@using System.Data;

@page "/fetchdata"

<PageTitle>Excel File Viewer</PageTitle>

<h1>Open Excel File to View</h1>

<InputFile OnChange="@OpenExcelFileFromDisk" />

<table>
    <thead>
        <tr>
            @foreach (DataColumn column in displayDataTable.Columns)
            {
                <th>
                    @column.ColumnName
                </th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in displayDataTable.Rows)
        {
            <tr>
                @foreach (DataColumn column in displayDataTable.Columns)
                {
                    <td>
                        @row [column.ColumnName].ToString()
                    </td>
                }
            </tr>
        }
    </tbody>
</table>

@code {
    // Create a DataTable
    private DataTable displayDataTable = new DataTable();

    // When a file is uploaded to the App using the InputFile, trigger:
    async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e)
    {
        IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY";

        // Open the File to a MemoryStream object
        MemoryStream ms = new MemoryStream();

        await e.File.OpenReadStream().CopyToAsync(ms);
        ms.Position = 0;

        // Define variables for IronXL
        WorkBook loadedWorkBook = WorkBook.FromStream(ms);
        WorkSheet loadedWorkSheet = loadedWorkBook.DefaultWorkSheet; // Or use .GetWorkSheet()

        // Add header Columns to the DataTable
        RangeRow headerRow = loadedWorkSheet.GetRow(0);
        for (int col = 0 ; col < loadedWorkSheet.ColumnCount ; col++)
        {
            displayDataTable.Columns.Add(headerRow.ElementAt(col).ToString());
        }

        // Populate the DataTable
        for (int row = 1 ; row < loadedWorkSheet.RowCount ; row++)
        {
            IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().Select(c => c.ToString());
            displayDataTable.Rows.Add(excelRow.ToArray());
        }
    }
}
Private IronXL As [using]
Private System As [using]

'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'@page "/fetchdata" (Of PageTitle) Excel File Viewer</PageTitle> (Of h1) Open Excel File @to View</h1> <InputFile OnChange="@OpenExcelFileFromDisk" /> (Of table) (Of thead) (Of tr) @foreach(DataColumn column in displayDataTable.Columns)
'			{
'				<th> @column.ColumnName </th>
'			}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'		</tr> </thead> (Of tbody) @foreach(DataRow row in displayDataTable.Rows)
'		{
'			<tr> @foreach(DataColumn column in displayDataTable.Columns)
'				{
'					<td> @row [column.ColumnName].ToString() </td>
'				}
'			</tr>
'		}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'	</tbody> </table> @code
'	{
'	' Create a DataTable
'	private DataTable displayDataTable = New DataTable();
'
'	' When a file is uploaded to the App using the InputFile, trigger:
'	async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e)
'	{
'		IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY";
'
'		' Open the File to a MemoryStream object
'		MemoryStream ms = New MemoryStream();
'
'		await e.File.OpenReadStream().CopyToAsync(ms);
'		ms.Position = 0;
'
'		' Define variables for IronXL
'		WorkBook loadedWorkBook = WorkBook.FromStream(ms);
'		WorkSheet loadedWorkSheet = loadedWorkBook.DefaultWorkSheet; ' Or use .GetWorkSheet()
'
'		' Add header Columns to the DataTable
'		RangeRow headerRow = loadedWorkSheet.GetRow(0);
'		for (int col = 0 ; col < loadedWorkSheet.ColumnCount ; col++)
'		{
'			displayDataTable.Columns.Add(headerRow.ElementAt(col).ToString());
'		}
'
'		' Populate the DataTable
'		for (int row = 1 ; row < loadedWorkSheet.RowCount ; row++)
'		{
'			IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().@Select(c => c.ToString());
'			displayDataTable.Rows.Add(excelRow.ToArray());
'		}
'	}
'}
VB   C#

Summary

The <InputFile> component will allow you to upload a file on this webpage. And we have set the invoked event callback to call OpenExcelFileFromDisk which is the async method in the @code block at the bottom. The HTML will render your Excel sheet as a Table on the tab.

IronXL.Excel is alone .NET software library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed, and is not dependant on Interop.


Further Reading

View the API Reference

Explore the API Reference for IronXL, outlining the details of all of IronXL’s features, namespaces, classes, methods fields and enums.

View the API Reference

Download the software product.