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

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

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

Install-Package IronXL.Excel

Reading Excel file

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

public ActionResult Index()
{
    WorkBook workbook = WorkBook.Load(@"C:\Files\Customer Data.xlsx");
    WorkSheet sheet = workbook.WorkSheets.First();

    var dataTable = sheet.ToDataTable();

    return View(dataTable);
}
public ActionResult Index()
{
    WorkBook workbook = WorkBook.Load(@"C:\Files\Customer Data.xlsx");
    WorkSheet sheet = workbook.WorkSheets.First();

    var dataTable = sheet.ToDataTable();

    return View(dataTable);
}
Public Function Index() As ActionResult
	Dim workbook As WorkBook = WorkBook.Load("C:\Files\Customer Data.xlsx")
	Dim sheet As WorkSheet = workbook.WorkSheets.First()

	Dim dataTable = sheet.ToDataTable()

	Return View(dataTable)
End Function
VB   C#

In the Index action method, we load the Excel file using IronXL's Load. 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>
VB   C#

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