# 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 documents/worksheets in a Blazor server-side application using the IronXL C# library.
<img src="/static-assets/excel/how-to/blazor-read-excel-file-tutorial/demo.gif" alt="Demonstration of IronXL Viewing Excel in Blazor" class="img-responsive add-shadow" style="margin-top: 10px ; margin-bottom: 30px;" />
<div class="hsg-featured-snippet">
<h2>How to Read Excel File in Blazor</h2>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronXL.Excel/">Install C# library to read Excel file in Blazor</a></li>
<li>Create a File Upload button on your Blazor Application</li>
<li>Use C# library to read Excel file from disk</li>
<li>Configure Blazor app to display the read data in a table on the window</li>
</ol>
</div>
<h3>Get started with IronXL</h3>
----------------------------------
## 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:
<div>
<table style="margin: 0 auto 0 auto">
<tr>
<th style="border: 1px solid black ; padding: 5px;">Input XLSX Excel Sheet</th>
<th style="border: 1px solid black ; padding: 5px;">Result in Blazor Server Browser</th>
</tr>
<tr>
<td style="border: 1px solid black ; padding: 5px;">
<table style="border: 1px solid black ; margin: 0 auto 0 auto">
<tr>
<th style="border: 2px solid black ; padding: 5px;">First name</th>
<th style="border: 2px solid black ; padding: 5px;">Last name</th>
<th style="border: 2px solid black ; padding: 5px;">ID</th>
</tr>
<tr>
<td style="border: 1px solid black ; padding: 5px;">John</td>
<td style="border: 1px solid black ; padding: 5px;">Applesmith</td>
<td style="border: 1px solid black ; padding: 5px;">1</td>
</tr>
<tr>
<td style="border: 1px solid black ; padding: 5px;">Richard</td>
<td style="border: 1px solid black ; padding: 5px;">Smith</td>
<td style="border: 1px solid black ; padding: 5px;">2</td>
</tr>
<tr>
<td style="border: 1px solid black ; padding: 5px;">Sherry</td>
<td style="border: 1px solid black ; padding: 5px;">Robins</td>
<td style="border: 1px solid black ; padding: 5px;">3</td>
</tr>
</table>
</td>
<td style="border: 1px solid black ; padding: 5px;">
<img src="/static-assets/excel/how-to/blazor-read-excel-file-tutorial/browser-view.webp" alt="" class="img-responsive add-shadow" style="margin-bottom: 30px;"/>
</td>
</tr>
</table>
</div>
Start off by creating a Blazor Project from the Visual Studio IDE:
<img src="/static-assets/excel/how-to/blazor-read-excel-file-tutorial/new-project.webp" alt="" class="img-responsive add-shadow" style="margin-bottom: 30px;"/>
Choose the **`Blazor Server App`** Project type:
<img src="/static-assets/excel/how-to/blazor-read-excel-file-tutorial/choose-blazor-project-type.webp" alt="" class="img-responsive add-shadow" style="margin-bottom: 30px;"/>
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:
<img src="/static-assets/excel/how-to/blazor-read-excel-file-tutorial/first-run.webp" alt="" class="img-responsive add-shadow" style="margin-bottom: 30px;"/>
Our goal will be to load our 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 streams. As a developer, we want a nice way through which we can get every row's cells and column information from Excel documents/worksheets into our applications or databases.
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
```shell
:ProductInstall
```
### 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:
```xml
<PackageReference Include="IronXL.Excel" Version="*" />
```
As shown here in Visual Studio:
<img src="/static-assets/excel/how-to/blazor-read-excel-file-tutorial/add-ironxl-csproj.webp" alt="" class="img-responsive add-shadow" style="margin-bottom: 30px;"/>
## 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 contents with the following code:
```cshtml
@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 instance
private DataTable displayDataTable = new DataTable();
// This method is triggered when a file is uploaded
async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e)
{
IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY";
// Load the uploaded file into a MemoryStream
MemoryStream ms = new MemoryStream();
await e.File.OpenReadStream().CopyToAsync(ms);
ms.Position = 0;
// Create an IronXL workbook from the MemoryStream
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 with data from the Excel sheet
for (int row = 1; row < loadedWorkSheet.RowCount; row++)
{
IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().Select(c => c.ToString());
displayDataTable.Rows.Add(excelRow.ToArray());
}
}
}
```
## Summary
The `<InputFile>` component allows you to upload a file on this webpage. 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 a standalone .NET software library for reading a wide variety of spreadsheet formats. It does not require [Microsoft Excel](https://products.office.com/en-us/excel) to be installed, and is not dependent on Interop.
<hr class="separator" />
<h4 class="tutorial-segment-title">Further Reading</h4>
<div class="tutorial-section">
<div class="row">
<div class="col-sm-4">
<div class="tutorial-image">
<img style="max-width: 110px; width: 100px; height: 140px;" alt="" class="img-responsive add-shadow" src="/img/svgs/documentation.svg" width="100" height="140" />
</div>
</div>
<div class="col-sm-8">
<h3>View the API Reference</h3>
<p>Explore the API Reference for IronXL, outlining the details of all of IronXL's features, namespaces, classes, methods, fields, and enums.</p>
<a class="doc-link" href="https://ironsoftware.com/csharp/excel/object-reference/api/" target="_blank">View the API Reference <i class="fa fa-chevron-right"></i></a>
</div>
</div>
</div>
*[Download](https://ironsoftware.com/csharp/excel/get-started/blazor-read-excel-file-tutorial/) the software product.*
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 documents/worksheets in a Blazor server-side application using the IronXL C# library.
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 streams. As a developer, we want a nice way through which we can get every row's cells and column information from Excel documents/worksheets into our applications or databases.
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
PM > Install-Package IronXL.Excel
Install-Package IronXL.Excel
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:
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 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 instance private DataTable displayDataTable = new DataTable(); // This method is triggered when a file is uploaded async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e) { IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY"; // Load the uploaded file into a MemoryStream MemoryStream ms = new MemoryStream(); await e.File.OpenReadStream().CopyToAsync(ms); ms.Position = 0; // Create an IronXL workbook from the MemoryStream 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 with data from the Excel sheet 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 instance
private DataTable displayDataTable = new DataTable();
// This method is triggered when a file is uploaded
async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e)
{
IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY";
// Load the uploaded file into a MemoryStream
MemoryStream ms = new MemoryStream();
await e.File.OpenReadStream().CopyToAsync(ms);
ms.Position = 0;
// Create an IronXL workbook from the MemoryStream
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 with data from the Excel sheet
for (int row = 1; row < loadedWorkSheet.RowCount; row++)
{
IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().Select(c => c.ToString());
displayDataTable.Rows.Add(excelRow.ToArray());
}
}
}
Text
Summary
The <InputFile> component allows you to upload a file on this webpage. 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 a standalone .NET software library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed, and is not dependent 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.
What is the best method to read Excel files in a Blazor application?
The best and easiest method to read Excel files in a Blazor server-side application is by using the IronXL C# library.
Is Interop required to read Excel files using IronXL?
No, IronXL does not require Interop to read Excel files. It is a standalone .NET library that reads a wide variety of spreadsheet formats.
How can I install IronXL to read Excel files in my Blazor project?
You can install IronXL using the NuGet Package Manager or by adding a PackageReference in the .csproj file of your Blazor project.
What file formats does IronXL support?
IronXL supports a wide variety of spreadsheet formats including XLSX, CSV, and others without the need for Microsoft Excel to be installed.
Can I use IronXL with the latest .NET versions?
Yes, IronXL supports all the latest versions of .NET (8, 7, and 6) as well as .NET Core Framework 4.6.2+.
How does IronXL compare to NPOI?
IronXL is better than NPOI in many aspects, offering more functions, easier implementation of complex logic, preferable licenses, and competent support.
What steps are involved in reading an Excel file in Blazor using IronXL?
To read an Excel file in Blazor using IronXL, you need to install the IronXL library, create a file upload button, use the library to read the Excel file from disk, and configure your Blazor app to display the data in a table.
What is the purpose of the <InputFile> component in the tutorial example?
The component in the tutorial example is used to upload the Excel file to the Blazor application, which is then read and displayed using IronXL.
Can I get detailed information from an Excel worksheet using IronXL?
Yes, with IronXL, you can retrieve various types of information from an Excel worksheet such as cell values, images, formatting, and more.
Where can I find the API Reference for IronXL?
You can explore the API Reference for IronXL by visiting their official documentation site, which provides detailed information on all features, namespaces, methods, and classes.
Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.