Comment écrire dans des fichiers Excel en .NET avec C#

Write Excel .NET Functions

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

Many C# application projects require us to update files and write new data in Excel spreadsheets programmatically. Excel .NET capabilities can sometimes be complicated, but using the IronXL library, this task is quite simple and allows working with Excel spreadsheets in any format. No bulk lines of code, just access to the specific cells and the custom values you assign.

Quickstart: Write a Value to a Specific Cell

This example shows how effortlessly you can write a value into a single cell using IronXL and save the Excel file in just a couple of lines—get started without boilerplate or complexity.

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.

    worksheet["A1"].Value = "Hello, IronXL!";
    workbook.SaveAs("output.xlsx");
  3. Deploy to test on your live environment

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

Write Excel .NET Instructions

  • Download the Library for Excel .NET
  • Write values in specific cells
  • Write static values in multiple cells
  • Write dynamic values in cell range
  • Replace cell values in specific row, column, range, and more
How To Work related to Write Excel .NET Functions

Access Excel Files

Let's start by accessing the Excel file in which we want to write the data. Let's open the Excel file in our project, and then open its specific worksheet using the following code.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-load-file.cs
// Load Excel file in the project
WorkBook workBook = WorkBook.Load("path");
' Load Excel file in the project
Dim workBook As WorkBook = WorkBook.Load("path")
$vbLabelText   $csharpLabel

The above will open the specified Excel file. Next, access the worksheet.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-get-sheet.cs
// Open Excel WorkSheet
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
' Open Excel WorkSheet
Dim workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
$vbLabelText   $csharpLabel

The Excel worksheet will open in worksheet and we can use it to write any type of data in the Excel file. Learn more about how to load Excel files and access worksheets in different ways through the examples in the link.

Note: Don't forget to add the reference to IronXL in your project and import the library by using using IronXL.


Write Value in Specific Cell

We can write in an Excel file using many different methods, but the basic approach is using ExcelCell. For this purpose, any cell of the opened Excel worksheet can be accessed and a value written in it as follows:

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-cell.cs
workSheet["Cell Address"].Value="Assign the Value";
workSheet("Cell Address").Value="Assign the Value"
$vbLabelText   $csharpLabel

Here's an example of how to use the above function to write in an Excel Cell in our C# project.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-cell-full.cs
using IronXL;

// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Open WorkSheet of sample.xlsx
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Access A1 cell and write the value
workSheet["A1"].Value = "new value";

// Save changes
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Open WorkSheet of sample.xlsx
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Access A1 cell and write the value
Private workSheet("A1").Value = "new value"

' Save changes
workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

This code will write new value in cell A1 of the worksheet Sheet1 in the Excel file sample.xlsx. In the same way, we can insert values in any cell address of an Excel file.

Note: Don't forget to save the Excel file after writing new values in the worksheet, as shown in the example above.

Force Assign the Exact Value

When setting the Value property, IronXL will try to convert it to its corresponding value type. Sometimes, this evaluation is undesirable since the user wants to force assign the exact value to the cell without conversion. To do this, assign the value as a string. In IronXL, simply use StringValue instead of Value to achieve the same effect.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-stringvalue.cs
// Assign value as string
workSheet["A1"].StringValue = "4402-12";
' Assign value as string
workSheet("A1").StringValue = "4402-12"
$vbLabelText   $csharpLabel

Write Static Values in a Range

We can write new values in multiple cells, called a range, as follows:

// Assign a static value to a range of cells
worksheet["B2:C5"].Value = "static value";
// Assign a static value to a range of cells
worksheet["B2:C5"].Value = "static value";
' Assign a static value to a range of cells
worksheet("B2:C5").Value = "static value"
$vbLabelText   $csharpLabel

In this way, we specify the range of cells From to To where the data will be written. The new value will be written in all the cells which lie in this range. To understand more about C# Excel Range, check out the examples here.

Let's see how to write a range in action using the example below.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-cell-range-full.cs
using IronXL;

// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Open WorkSheet of sample.xlsx
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Specify range row wise and write new value
workSheet["B2:B9"].Value = "new value";

// Specify range column wise and write new value
workSheet["C3:C7"].Value = "new value";

// Save changes
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Open WorkSheet of sample.xlsx
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Specify range row wise and write new value
Private workSheet("B2:B9").Value = "new value"

' Specify range column wise and write new value
Private workSheet("C3:C7").Value = "new value"

' Save changes
workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

This code will write new value from B2 to C5 in the worksheet Sheet1 of the Excel file sample.xlsx. It uses static values for the Excel cells.


Write Dynamic Values in a Range

We can also add dynamic values to a range, as seen below.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-dynamic-value.cs
using IronXL;

// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Open WorkSheet of sample.xlsx
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Specify range in which we want to write the values
for (int i = 2; i <= 7; i++)
{
    // Write the Dynamic value in one row
    workSheet["B" + i].Value = "Value" + i;

    // Write the Dynamic value in another row
    workSheet["D" + i].Value = "Value" + i;
}

// Save changes
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Open WorkSheet of sample.xlsx
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Specify range in which we want to write the values
For i As Integer = 2 To 7
	' Write the Dynamic value in one row
	workSheet("B" & i).Value = "Value" & i

	' Write the Dynamic value in another row
	workSheet("D" & i).Value = "Value" & i
Next i

' Save changes
workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

The above code will write dynamic values in columns B from 2 to 7 in the Excel file sample.xlsx. We can see the result of the code on sample.xlsx.

1excel related to Write Dynamic Values in a Range

Replace Excel Cell Value

Using IronXL, we can easily write a new value to replace the old value, using the Replace() function as follows:

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace.cs
workSheet.Replace("old value", "new value");
workSheet.Replace("old value", "new value")
$vbLabelText   $csharpLabel

The above function will write new value overwriting the old value in the complete Excel worksheet.

Replace Cell Value in Specific Row

If we want to write a new value only in one specific row, do as follows:

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-row.cs
workSheet.Rows[RowIndex].Replace("old value", "new value");
workSheet.Rows(RowIndex).Replace("old value", "new value")
$vbLabelText   $csharpLabel

This will write new value over the old value in only the specified row index.

Replace Cell Value in Specific Column

Similarly, if we want to write new value over the old value within a specific column, do as follows:

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-column.cs
workSheet.Columns[ColumnIndex].Replace("old value", "new Value");
workSheet.Columns(ColumnIndex).Replace("old value", "new Value")
$vbLabelText   $csharpLabel

The above code will write new value to replace the old value, but only in the specified column index. The rest of the worksheet remains the same.

Replace Cell Value in Specific Range

IronXL also provides a way to write a new value replacing the old value, only in a specified range.

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-range.cs
workSheet["From Cell Address : To Cell Address"].Replace("old value", "new value");
workSheet("From Cell Address : To Cell Address").Replace("old value", "new value")
$vbLabelText   $csharpLabel

This will write new value above old value, just in the cells which lie in the specified range.

Let's see the example of how to use all of the above functions to write new values to replace old values in an Excel worksheet.

Example of all Functions

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-replace-full.cs
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Write new above old in complete WorkSheet
workSheet.Replace("old", "new");

// Write new above old just in row no 6 of WorkSheet
workSheet.Rows[5].Replace("old", "new");

// Write new above old just in column no 5 of WorkSheet
workSheet.Columns[4].Replace("old", "new");

// Write new above old just from A5 to H5 of WorkSheet
workSheet["A5:H5"].Replace("old", "new");

workBook.SaveAs("sample.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Write new above old in complete WorkSheet
workSheet.Replace("old", "new")

' Write new above old just in row no 6 of WorkSheet
workSheet.Rows(5).Replace("old", "new")

' Write new above old just in column no 5 of WorkSheet
workSheet.Columns(4).Replace("old", "new")

' Write new above old just from A5 to H5 of WorkSheet
workSheet("A5:H5").Replace("old", "new")

workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

For more information about how to write Excel .NET applications and more, check out our full tutorial on how to Open and Write Excel Files C#.


Tutorial Quick Access

Read API Reference

Read the IronXL documentation including lists of all functions, features, namespaces, classes, and enums available to you in the library.

Read API Reference
Documentation related to Tutorial Quick Access

Questions Fréquemment Posées

Comment puis-je commencer à écrire des fichiers Excel en .NET?

Pour commencer avec IronXL, téléchargez la bibliothèque et incluez-la dans votre projet. Importez la bibliothèque en utilisant using IronXL dans votre code C#.

Comment puis-je écrire une valeur dans une cellule spécifique d'une feuille Excel?

Pour écrire une valeur dans une cellule spécifique en utilisant IronXL, accédez à la cellule en utilisant son adresse, comme worksheet['A1'], et assignez une valeur en utilisant la propriété .Value.

Comment puis-je assigner une valeur statique à une plage de cellules dans Excel?

Pour assigner une valeur statique à une plage en utilisant IronXL, spécifiez la plage en utilisant les adresses de cellules, comme worksheet['B2:C5'], et définissez la propriété .Value à votre valeur statique souhaitée.

Puis-je écrire des valeurs dynamiques dans une plage de cellules dans Excel?

Oui, avec IronXL, vous pouvez écrire des valeurs dynamiques dans une plage en utilisant des boucles ou une logique personnalisée. Par exemple, vous pouvez parcourir les adresses de cellules et assigner des valeurs dynamiques en fonction de vos besoins.

Comment puis-je remplacer les valeurs existantes des cellules dans une feuille Excel?

En utilisant IronXL, vous pouvez utiliser la méthode Replace() sur la feuille de calcul, la ligne, la colonne, ou la plage spécifiée pour remplacer 'ancienne valeur' par 'nouvelle valeur'.

Quelle est la différence entre l'utilisation de Value et StringValue?

Dans IronXL, la propriété .Value peut convertir la valeur assignée à un format spécifique de type, tandis que .StringValue assigne la valeur en tant que chaîne, évitant la conversion.

Comment puis-je ouvrir et accéder à une feuille de calcul spécifique dans un fichier Excel?

Utilisez WorkBook.Load('path/to/your/excel-file.xlsx') de IronXL pour charger le fichier et workbook.GetWorkSheet('SheetName') pour accéder à une feuille de calcul spécifique.

Est-il nécessaire de sauvegarder le fichier Excel après avoir écrit de nouvelles valeurs?

Oui, en utilisant IronXL, vous devez sauvegarder le fichier Excel après avoir effectué des modifications en utilisant la méthode workbook.SaveAs('filename.xlsx') pour conserver vos modifications.

Comment puis-je écrire une nouvelle valeur pour remplacer une ancienne valeur dans une ligne spécifique?

Avec IronXL, accédez à la ligne en utilisant worksheet.Rows[index] puis appelez la méthode Replace() avec les anciennes et nouvelles valeurs.

Puis-je remplacer les valeurs dans une colonne spécifique?

Oui, en utilisant IronXL, accédez à la colonne en utilisant worksheet.Columns['A'] et utilisez la méthode Replace() pour remplacer les anciennes valeurs par des nouvelles dans cette colonne.

Comment puis-je m'assurer que mon projet C# peut interagir avec des fichiers Excel?

Assurez-vous que votre projet C# référence la bibliothèque IronXL en la téléchargeant et en l'ajoutant aux références de votre projet. Cela facilite l'interaction avec les fichiers Excel.

Quelles sont les problèmes courants lors de l'écriture vers des fichiers Excel en .NET?

Les problèmes courants incluent l'absence de référencement correct de la bibliothèque IronXL, le format incorrect des adresses de cellules, et l'oubli de sauvegarder les modifications. Assurez-vous d'une configuration et d'une syntaxe appropriées.

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