Comment lire les fichiers CSV en C# en utilisant IronXL

Read a CSV File in C#

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

When you need to read CSV files in C#, IronXL is an easy answer. You can read a CSV file with commas, or any other delimiter, as seen in the code segments below.

Quickstart: Load and convert a CSV file using IronXL in one line

This example shows how easy it is to read a CSV file using IronXL’s LoadCSV method and save it as an Excel workbook, letting you get started in seconds with minimal code.

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.

    WorkBook wb = WorkBook.LoadCSV("data.csv", ExcelFileFormat.XLSX, listDelimiter: ","); wb.SaveAs("output.xlsx");
  3. Deploy to test on your live environment

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

Reading CSV Files in .NET Applications

  • Install a C# library for Reading CSV Files (IronXL)
  • Read CSV files in C#
  • Specify file format and delimiter
How To Work related to Read a CSV File in C#

Step 1

1. Install the IronXL Library

Before you can make use of IronXL to read CSV files in MVC or ASP or dotnet core, you need to install it first. Here is a quick walk-through.

  • In Visual Studio, select the Project menu
  • Manage NuGet Packages
  • Search for IronXL.Excel
  • Install
IronXL.Excel NuGet Package
Figure 1 - IronXL.Excel NuGet Package

Or Download from the Iron Software website, here: https://ironsoftware.com/csharp/excel/packages/IronXL.zip


How to Tutorial

2. Read CSV Files Programmatically

Now for the project!

Add the IronXL Namespace

// This namespace is required to access the IronXL functionalities
using IronXL;
// This namespace is required to access the IronXL functionalities
using IronXL;
' This namespace is required to access the IronXL functionalities
Imports IronXL
$vbLabelText   $csharpLabel

Add code to read a CSV file programmatically with IronXL and C#

:path=/static-assets/excel/content-code-examples/how-to/csharp-read-csv-read.cs
// Load the CSV file into a WorkBook object, specifying the file path, format, and delimiter
WorkBook workbook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat: ExcelFileFormat.XLSX, listDelimiter: ",");

// Access the default worksheet within the loaded workbook
WorkSheet ws = workbook.DefaultWorkSheet;

// Save the workbook as an Excel file with a specified name
workbook.SaveAs("Csv_To_Excel.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
A CSV file opened in Notepad
Figure 2 - A CSV file opened in Notepad

A Workbook object is created. The LoadCSV method of the Workbook object is then used to specify the name of the CSV file to be read, the format to read it into, and what the delimiter is for the file. In this case, a comma is used as a separator.

A Worksheet object is then created. This is where the contents of the CSV file will be placed. Then the file is saved under a new name and format.

The CSV file opened in Excel
Figure 3 - The CSV file opened in Excel

Library Quick Access

IronXL API Reference Documentation

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.

IronXL API Reference Documentation
Documentation related to 2. Read CSV Files Programmatically

Questions Fréquemment Posées

Comment puis-je lire un fichier CSV en C# sans utiliser Interop?

Vous pouvez lire un fichier CSV en C# sans utiliser Interop en utilisant la bibliothèque IronXL. Tout d'abord, installez IronXL via le gestionnaire de packages NuGet dans Visual Studio. Ensuite, utilisez la méthode WorkBook.LoadCSV pour charger le fichier CSV dans un objet WorkBook.

Quelles étapes sont impliquées dans la lecture d'un fichier CSV avec IronXL en C#?

Pour lire un fichier CSV avec IronXL, installez d'abord la bibliothèque IronXL. Ensuite, créez un nouveau projet C#, ajoutez l'espace de noms using IronXL;, et utilisez la méthode WorkBook.LoadCSV pour charger et manipuler les données du fichier CSV.

IronXL peut-il gérer des fichiers CSV avec différents délimiteurs?

Oui, IronXL vous permet de spécifier différents délimiteurs lors de la lecture de fichiers CSV. Vous pouvez ajuster le délimiteur en le définissant dans les options de la méthode LoadCSV.

Comment puis-je enregistrer un fichier CSV en tant que fichier Excel en C#?

Après avoir chargé le fichier CSV dans un objet WorkBook avec IronXL, vous pouvez utiliser la méthode SaveAs pour l'enregistrer en tant que fichier Excel dans le format souhaité, tel que XLSX.

Pourquoi utiliser IronXL au lieu de Microsoft Interop pour les opérations CSV en C#?

IronXL est préféré à Microsoft Interop pour les opérations CSV en C# car il ne nécessite pas qu'Excel soit installé sur le serveur. Il offre une méthode plus efficace et simple pour gérer les fichiers CSV dans les applications C#.

Comment dois-je spécifier la feuille de calcul à manipuler dans un fichier CSV avec IronXL?

Lorsque vous chargez un fichier CSV avec IronXL, il est stocké dans un objet WorkBook. Vous pouvez accéder et manipuler les données à l'aide de l'objet WorkSheet par défaut, qui représente le contenu du fichier CSV.

Est-il possible d'intégrer des fonctionnalités de lecture CSV dans les applications ASP.NET avec IronXL?

Oui, vous pouvez intégrer des fonctionnalités de lecture CSV dans les applications ASP.NET ou .NET Core avec IronXL en installant la bibliothèque et en utilisant ses méthodes pour charger, lire et manipuler les données CSV.

Où puis-je trouver la documentation de référence de l'API IronXL ?

La documentation de référence de l'API IronXL est disponible sur le site web d'Iron Software. Elle fournit des informations détaillées sur toutes les méthodes disponibles, y compris celles pour lire et manipuler les fichiers CSV.

Quel est le but de la méthode LoadCSV dans IronXL?

La méthode LoadCSV dans IronXL est utilisée pour importer des données CSV dans un objet WorkBook, vous permettant de manipuler et d'interagir avec les données comme si elles étaient dans un format 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