Read Excel Files in ASP.NET MVC Using IronXL

This tutorial guides developers on implementing Excel file parsing in ASP.NET MVC apps using IronXL.

Create an ASP.NET Project

Using Visual Studio 2022 (or similar product versions), create a new ASP.NET project. Add additional NuGet packages and source code as needed for the particular project.

Install IronXL Library

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

First Step:
green arrow pointer


After creating the new project, we have to install the IronXL library. Follow the steps below to install the IronXL library. Open the NuGet Package Manager Console and run the following command:

Install-Package IronXL.Excel

Reading Excel file

Open the default controller in your ASP.NET project (e.g., HomeController.cs) and replace the Index method with the following code:

public ActionResult Index()
{
    // Load the Excel workbook from a specified path.
    WorkBook workBook = WorkBook.Load(@"C:\Files\Customer Data.xlsx");

    // Access the first worksheet from the workbook.
    WorkSheet workSheet = workBook.WorkSheets.First();

    // Convert the worksheet data to a DataTable object.
    var dataTable = workSheet.ToDataTable();

    // Send the DataTable to the view for rendering.
    return View(dataTable);
}
public ActionResult Index()
{
    // Load the Excel workbook from a specified path.
    WorkBook workBook = WorkBook.Load(@"C:\Files\Customer Data.xlsx");

    // Access the first worksheet from the workbook.
    WorkSheet workSheet = workBook.WorkSheets.First();

    // Convert the worksheet data to a DataTable object.
    var dataTable = workSheet.ToDataTable();

    // Send the DataTable to the view for rendering.
    return View(dataTable);
}
Public Function Index() As ActionResult
	' Load the Excel workbook from a specified path.
	Dim workBook As WorkBook = WorkBook.Load("C:\Files\Customer Data.xlsx")

	' Access the first worksheet from the workbook.
	Dim workSheet As WorkSheet = workBook.WorkSheets.First()

	' Convert the worksheet data to a DataTable object.
	Dim dataTable = workSheet.ToDataTable()

	' Send the DataTable to the view for rendering.
	Return View(dataTable)
End Function
$vbLabelText   $csharpLabel

In the Index action method, we load the Excel file using IronXL's Load method. The path of the Excel file (including the filename) is provided as a parameter to the method call. Next, we select the first Excel sheet as the working sheet and load the data contained in it into a DataTable object. Lastly, the DataTable is sent to the frontend.

Display Excel Data on a Web Page

The next example shows how to display the DataTable returned in the previous example in a web browser.

The working Excel file that will be used in this example is depicted below:

Read Excel Files in ASP.NET MVC Using IronXL, Figure 1: Excel file

Excel file

Open the index.cshtml (index view) and replace the code with the following HTML code.

@{
    ViewData["Title"] = "Home Page";
}

@using System.Data
@model DataTable

<div class="text-center">
    <h1 class="display-4">Welcome to IronXL Read Excel MVC</h1>
</div>
<table class="table table-dark">
    <tbody>
        @foreach (DataRow row in Model.Rows)
        {
            <tr>
                @for (int i = 0; i < Model.Columns.Count; i++)
                {
                    <td>@row[i]</td>
                }
            </tr>
        }

    </tbody>
</table>
@{
    ViewData["Title"] = "Home Page";
}

@using System.Data
@model DataTable

<div class="text-center">
    <h1 class="display-4">Welcome to IronXL Read Excel MVC</h1>
</div>
<table class="table table-dark">
    <tbody>
        @foreach (DataRow row in Model.Rows)
        {
            <tr>
                @for (int i = 0; i < Model.Columns.Count; i++)
                {
                    <td>@row[i]</td>
                }
            </tr>
        }

    </tbody>
</table>
@
If True Then
	ViewData("Title") = "Home Page"
End If

'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: using System.Data model DataTable <div class="text-center"> <h1 class="display-4"> Welcome to IronXL Read Excel MVC</h1> </div> <table class="table table-dark"> <tbody> foreach(DataRow row in Model.Rows)
"display-4"> Welcome [to] IronXL Read Excel MVC</h1> </div> <table class="table table-dark"> (Of tbody) foreach(DataRow row in Model.Rows)
		If True Then
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: using System.Data model DataTable <div class="text-center"> <h1 class="display-4"> Welcome to IronXL Read Excel MVC</h1> </div> <table class
"text-center"> <h1 class="display-4"> Welcome [to] IronXL Read Excel MVC</h1> </div> <table class
[using] System.Data model DataTable <div class="text-center"> <h1 class
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'			(Of tr) @for(int i = 0; i < Model.Columns.Count; i++)
'				{
'					<td> @row[i]</td>
'				}
			</tr>
		End If

'INSTANT VB TODO TASK: The following line uses invalid syntax:
'	</tbody> </table>
$vbLabelText   $csharpLabel

The above code uses the DataTable returned from the Index method as a model. Each row from the table is printed on the webpage using a @for loop, including Bootstrap formatting for decoration.

Running the project will produce the results shown below.

Read Excel Files in ASP.NET MVC Using IronXL, Figure 2: Bootstrap Table

Bootstrap Table

Frequently Asked Questions

How can I read Excel files in ASP.NET MVC without using Interop?

You can read Excel files in ASP.NET MVC without using Interop by utilizing the IronXL library. First, install IronXL via NuGet and then use the WorkBook.Load method to load the Excel file. Access the worksheet and convert it to a DataTable for further processing.

What steps are needed to display Excel data on a web page using ASP.NET MVC?

To display Excel data on a web page using ASP.NET MVC, load the Excel file with IronXL, convert it to a DataTable, and pass it to the view. Use Razor syntax in the view to iterate over the DataTable and render the data as an HTML table.

How do I install the necessary library to handle Excel files in an ASP.NET application?

To handle Excel files in an ASP.NET application, install the IronXL library by opening the NuGet Package Manager Console in Visual Studio and executing the command Install-Package IronXL.Excel.

What is the advantage of using IronXL for Excel operations in ASP.NET MVC?

IronXL simplifies Excel operations in ASP.NET MVC by providing an easy-to-use API that allows reading, writing, and manipulating Excel files without needing Excel Interop, thus enhancing performance and reducing complexity.

How can I convert an Excel worksheet into a DataTable in ASP.NET MVC?

In ASP.NET MVC, after loading an Excel file with IronXL, you can convert a worksheet to a DataTable using the ToDataTable method, which facilitates easy data handling and manipulation.

Can IronXL be used in ASP.NET Core MVC applications for Excel processing?

Yes, IronXL is compatible with ASP.NET Core MVC applications and can be used to efficiently read and manipulate Excel files without relying on Excel Interop.

What code modifications are required in the controller to read Excel files?

Modify the default controller, such as HomeController.cs, by using IronXL's WorkBook.Load to load the Excel workbook and convert the worksheet data into a DataTable to pass it to the view.

How does IronXL improve the process of reading Excel files in ASP.NET?

IronXL enhances the process of reading Excel files in ASP.NET by offering a straightforward API that eliminates the need for Excel Interop, allowing for seamless reading, writing, and manipulation of Excel data.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.