# A Guide to Reading and Writing Excel Files in C#
La lecture et la création de fichiers Excel (XLS, XLSX et CSV) en C# et dans d'autres langages .NET sont facilitées par la bibliothèque logicielle IronXL d'Iron Software.
IronXL ne nécessite pas l'installation d'Excel Interop sur votre serveur. IronXL offre une API plus rapide et plus intuitive que **Microsoft.Office.Interop.Excel** .
IronXL fonctionne sur les plateformes suivantes :
* .NET Framework 4.6.2 et versions ultérieures pour Windows et Azure
* .NET Core 2 et versions ultérieures pour Windows, Linux, macOS et Azure
* .NET 5, .NET 6, .NET 7, .NET 8, Mono, Maui et Xamarin
## Installer IronXL
Commencez par installer IronXL, soit en utilisant notre package NuGet, soit en <a class="js-modal-open" href="/csharp/excel/packages/IronXL.zip" data-modal-id="trial-license-after-download">téléchargeant la DLL</a> . Les classes IronXL se trouvent dans le namespace `IronXL`.
La méthode la plus simple pour installer IronXL consiste à utiliser le gestionnaire de packages NuGet pour Visual Studio :
Le nom du package est **IronXL.Excel** .
```shell
:ProductInstall
```
<a class="js-modal-open" href="https://www.nuget.org/packages/IronXL.Excel/" target="_blank" data-modal-id="trial-license-after-download">https://www.nuget.org/packages/IronXL.Excel/</a>
## Lecture d'un document Excel
Avec IronXL, l'extraction de données à partir d'un fichier Excel peut se faire en quelques lignes de code seulement.
```csharp
:path=/static-assets/excel/content-code-examples/get-started/get-started-1.cs
```
## Création de nouveaux documents Excel
IronXL offre une interface rapide et facile pour générer des documents Excel à l'aide de C# ou VB.NET.
```csharp
:path=/static-assets/excel/content-code-examples/get-started/get-started-2.cs
```
## Exporter en CSV, XLS, XLSX, JSON ou XML
IronXL vous permet également d'enregistrer ou d'exporter des données vers divers formats de tableurs structurés populaires.
```csharp
:path=/static-assets/excel/content-code-examples/get-started/get-started-3.cs
```
## Cellules de style et gammes
Vous pouvez appliquer une mise en forme aux cellules et plages Excel à l'aide de l'objet IronXL.Range.Style.
```csharp
:path=/static-assets/excel/content-code-examples/get-started/get-started-4.cs
```
## Plages de tri
Avec IronXL, vous pouvez facilement trier une plage de cellules Excel à l'aide de l'objet Range.
```csharp
:path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs
```
## Modification des formules
Modifier une formule Excel est aussi simple que d'attribuer une valeur commençant par le signe " = ". La formule sera calculée instantanément.
```csharp
:path=/static-assets/excel/content-code-examples/get-started/get-started-6.cs
```
## Pourquoi choisir IronXL ?
IronXL propose une API conviviale pour les développeurs permettant de lire et d'écrire des documents Excel en .NET.
Il fonctionne sans nécessiter l'installation de Microsoft Excel ou d'Excel Interop sur le serveur, ce qui rend la gestion des fichiers Excel rapide, légère et sans tracas.
## Aller de l'avant
Pour découvrir davantage de fonctionnalités et de capacités, nous vous recommandons de consulter la [documentation de référence de l'API .NET,](/csharp/excel/object-reference/) formatée de manière similaire à la documentation MSDN.
La lecture et la création de fichiers Excel (XLS, XLSX et CSV) en C# et dans d'autres langages .NET sont facilitées par la bibliothèque logicielle IronXL d'Iron Software.
IronXL ne nécessite pas l'installation d'Excel Interop sur votre serveur. IronXL offre une API plus rapide et plus intuitive que Microsoft.Office.Interop.Excel .
IronXL fonctionne sur les plateformes suivantes :
.NET Framework 4.6.2 et versions ultérieures pour Windows et Azure
.NET Core 2 et versions ultérieures pour Windows, Linux, macOS et Azure
Commencez par installer IronXL, soit en utilisant notre package NuGet, soit en téléchargeant la DLL . Les classes IronXL se trouvent dans le namespace IronXL.
La méthode la plus simple pour installer IronXL consiste à utiliser le gestionnaire de packages NuGet pour Visual Studio :
Le nom du package est IronXL.Excel .
Avec IronXL, l'extraction de données à partir d'un fichier Excel peut se faire en quelques lignes de code seulement.
using IronXL;// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSVWorkBook workBook = WorkBook.Load("data.xlsx");WorkSheet workSheet = workBook.WorkSheets.First();// Select cells easily in Excel notation and return the calculated value, date, text or formulaint cellValue = workSheet["A2"].IntValue;// Read from Ranges of cells elegantly.foreach (var cell in workSheet["A2:B10"]){Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);}
using IronXL;
// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workBook = WorkBook.Load("data.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();
// Select cells easily in Excel notation and return the calculated value, date, text or formula
int cellValue = workSheet["A2"].IntValue;
// Read from Ranges of cells elegantly.
foreach (var cell in workSheet["A2:B10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
ImportsIronXL' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSVPrivate workBook AsWorkBook = WorkBook.Load("data.xlsx")Private workSheet AsWorkSheet = workBook.WorkSheets.First()' Select cells easily in Excel notation and return the calculated value, date, text or formulaPrivate cellValue AsInteger = workSheet("A2").IntValue' Read from Ranges of cells elegantly.For Each cell In workSheet("A2:B10")Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)Next cell
Imports IronXL
' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("data.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Select cells easily in Excel notation and return the calculated value, date, text or formula
Private cellValue As Integer = workSheet("A2").IntValue
' Read from Ranges of cells elegantly.
For Each cell In workSheet("A2:B10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
Création de nouveaux documents Excel
IronXL offre une interface rapide et facile pour générer des documents Excel à l'aide de C# ou VB.NET.
using IronXL;// Create new Excel WorkBook document.WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);workBook.Metadata.Author = "IronXL";// Add a blank WorkSheetWorkSheet workSheet = workBook.CreateWorkSheet("main_sheet");// Add data and styles to the new worksheetworkSheet["A1"].Value = "Hello World";workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;// Save the excel fileworkBook.SaveAs("NewExcelFile.xlsx");
using IronXL;
// Create new Excel WorkBook document.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
workBook.Metadata.Author = "IronXL";
// Add a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("main_sheet");
// Add data and styles to the new worksheet
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
// Save the excel file
workBook.SaveAs("NewExcelFile.xlsx");
ImportsIronXL' Create new Excel WorkBook document.Private workBook AsWorkBook = WorkBook.Create(ExcelFileFormat.XLSX)workBook.Metadata.Author = "IronXL"' Add a blank WorkSheetDim workSheet AsWorkSheet = workBook.CreateWorkSheet("main_sheet")' Add data and styles to the new worksheetworkSheet("A1").Value = "Hello World"workSheet("A2").Style.BottomBorder.SetColor("#ff6600")workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double' Save the excel fileworkBook.SaveAs("NewExcelFile.xlsx")
Imports IronXL
' Create new Excel WorkBook document.
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
workBook.Metadata.Author = "IronXL"
' Add a blank WorkSheet
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("main_sheet")
' Add data and styles to the new worksheet
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
' Save the excel file
workBook.SaveAs("NewExcelFile.xlsx")
Exporter en CSV, XLS, XLSX, JSON ou XML
IronXL vous permet également d'enregistrer ou d'exporter des données vers divers formats de tableurs structurés populaires.
// Export to many formats with fluent savingworkSheet.SaveAs("NewExcelFile.xls");workSheet.SaveAs("NewExcelFile.xlsx");workSheet.SaveAsCsv("NewExcelFile.csv");workSheet.SaveAsJson("NewExcelFile.json");workSheet.SaveAsXml("NewExcelFile.xml");
// Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls");
workSheet.SaveAs("NewExcelFile.xlsx");
workSheet.SaveAsCsv("NewExcelFile.csv");
workSheet.SaveAsJson("NewExcelFile.json");
workSheet.SaveAsXml("NewExcelFile.xml");
' Export to many formats with fluent savingworkSheet.SaveAs("NewExcelFile.xls")workSheet.SaveAs("NewExcelFile.xlsx")workSheet.SaveAsCsv("NewExcelFile.csv")workSheet.SaveAsJson("NewExcelFile.json")workSheet.SaveAsXml("NewExcelFile.xml")
' Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls")
workSheet.SaveAs("NewExcelFile.xlsx")
workSheet.SaveAsCsv("NewExcelFile.csv")
workSheet.SaveAsJson("NewExcelFile.json")
workSheet.SaveAsXml("NewExcelFile.xml")
Cellules de style et gammes
Vous pouvez appliquer une mise en forme aux cellules et plages Excel à l'aide de l'objet IronXL.Range.Style.
// Set cell's value and stylesworkSheet["A1"].Value = "Hello World";workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
// Set cell's value and styles
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
' Set cell's value and stylesworkSheet("A1").Value = "Hello World"workSheet("A2").Style.BottomBorder.SetColor("#ff6600")workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
' Set cell's value and styles
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
Plages de tri
Avec IronXL, vous pouvez facilement trier une plage de cellules Excel à l'aide de l'objet Range.
using IronXL;using Range = IronXL.Range;WorkBook workBook = WorkBook.Load("test.xls");WorkSheet workSheet = workBook.WorkSheets.First();// This is how we get range from Excel worksheetRange range = workSheet["A2:A8"];// Sort the range in the sheetrange.SortAscending();workBook.Save();
using IronXL;
using Range = IronXL.Range;
WorkBook workBook = WorkBook.Load("test.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// This is how we get range from Excel worksheet
Range range = workSheet["A2:A8"];
// Sort the range in the sheet
range.SortAscending();
workBook.Save();
ImportsIronXLDim workBook AsWorkBook = WorkBook.Load("test.xls")Dim workSheet AsWorkSheet = workBook.WorkSheets.First()' This is how we get range from Excel worksheetDim range AsRange = workSheet("A2:A8")' Sort the range in the sheetrange.SortAscending()workBook.Save()
Imports IronXL
Dim workBook As WorkBook = WorkBook.Load("test.xls")
Dim workSheet As WorkSheet = workBook.WorkSheets.First()
' This is how we get range from Excel worksheet
Dim range As Range = workSheet("A2:A8")
' Sort the range in the sheet
range.SortAscending()
workBook.Save()
Modification des formules
Modifier une formule Excel est aussi simple que d'attribuer une valeur commençant par le signe " = ". La formule sera calculée instantanément.
// Set a formulaworkSheet["A1"].Value = "=SUM(A2:A10)";// Get the calculated valuedecimal sum = workSheet["A1"].DecimalValue;
// Set a formula
workSheet["A1"].Value = "=SUM(A2:A10)";
// Get the calculated value
decimal sum = workSheet["A1"].DecimalValue;
' Set a formulaworkSheet("A1").Value = "=SUM(A2:A10)"' Get the calculated valueDim sum AsDecimal = workSheet("A1").DecimalValue
' Set a formula
workSheet("A1").Value = "=SUM(A2:A10)"
' Get the calculated value
Dim sum As Decimal = workSheet("A1").DecimalValue
Pourquoi choisir IronXL ?
IronXL propose une API conviviale pour les développeurs permettant de lire et d'écrire des documents Excel en .NET.
Il fonctionne sans nécessiter l'installation de Microsoft Excel ou d'Excel Interop sur le serveur, ce qui rend la gestion des fichiers Excel rapide, légère et sans tracas.
Aller de l'avant
Pour découvrir davantage de fonctionnalités et de capacités, nous vous recommandons de consulter la documentation de référence de l'API .NET, formatée de manière similaire à la documentation MSDN.
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 et créer des manuels bien structurés et visuellement attrayants.