Read Excel Files in ASP.NET MVC Using IronXL

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

Quickstart: Load and Convert Excel Sheet to DataTable in MVC

This example shows how to get started in seconds: load an Excel workbook, pick its first worksheet, and convert it to a System.Data.DataTable using IronXL — no Interop, no hassle.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    var dataTable = IronXL.WorkBook.Load("CustomerData.xlsx").DefaultWorkSheet.ToDataTable(true);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

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

Commencez à utiliser IronXL dans votre projet aujourd'hui avec un essai gratuit.

Première étape :
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

Questions Fréquemment Posées

Comment puis-je lire des fichiers Excel dans ASP.NET MVC sans utiliser Interop?

Vous pouvez lire des fichiers Excel dans ASP.NET MVC sans utiliser Interop en utilisant la bibliothèque IronXL. Tout d'abord, installez IronXL via NuGet puis utilisez la méthode WorkBook.Load pour charger le fichier Excel. Accédez à la feuille de calcul et convertissez-la en DataTable pour un traitement ultérieur.

Quelles sont les étapes nécessaires pour afficher des données Excel sur une page web avec ASP.NET MVC?

Pour afficher les données Excel sur une page web avec ASP.NET MVC, chargez le fichier Excel avec IronXL, convertissez-le en DataTable, et passez-le à la vue. Utilisez la syntaxe Razor dans la vue pour itérer sur le DataTable et afficher les données en tant que tableau HTML.

Comment installer la bibliothèque nécessaire pour gérer les fichiers Excel dans une application ASP.NET?

Pour gérer les fichiers Excel dans une application ASP.NET, installez la bibliothèque IronXL en ouvrant la console du gestionnaire de packages NuGet dans Visual Studio et en exécutant la commande Install-Package IronXL.Excel.

Quel est l'avantage d'utiliser IronXL pour les opérations Excel dans ASP.NET MVC?

IronXL simplifie les opérations Excel dans ASP.NET MVC en fournissant une API facile à utiliser qui permet de lire, écrire et manipuler des fichiers Excel sans avoir besoin d'Excel Interop, améliorant ainsi les performances et réduisant la complexité.

Comment puis-je convertir une feuille de calcul Excel en un DataTable dans ASP.NET MVC?

Dans ASP.NET MVC, après avoir chargé un fichier Excel avec IronXL, vous pouvez convertir une feuille de calcul en DataTable en utilisant la méthode ToDataTable, ce qui facilite la gestion et la manipulation des données.

IronXL peut-il être utilisé dans les applications ASP.NET Core MVC pour le traitement Excel?

Oui, IronXL est compatible avec les applications ASP.NET Core MVC et peut être utilisé pour lire et manipuler efficacement les fichiers Excel sans dépendre d'Excel Interop.

Quelles modifications de code sont requises dans le contrôleur pour lire des fichiers Excel?

Modifiez le contrôleur par défaut, comme HomeController.cs, en utilisant WorkBook.Load d'IronXL pour charger le classeur Excel et convertir les données de la feuille en un DataTable pour les passer à la vue.

Comment IronXL améliore-t-il le processus de lecture des fichiers Excel dans ASP.NET?

IronXL améliore le processus de lecture des fichiers Excel dans ASP.NET en offrant une API simple qui élimine le besoin d'Excel Interop, permettant une lecture, une écriture et une manipulation sans faille des données Excel.

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir