Write Excel .NET Functions

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.


Step 1

1. Download the Library

Download the Library directly or use NuGet package manager to install. From here, you have access to all the code functions provided by IronXL for C# which is free for development projects) and you can follow the next steps.

 PM > Install-Package IronXL.Excel

How to Tutorial

2. Access Excel Files

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

//load Excel file in the project
WorkBook wb = WorkBook.Load("path")
//load Excel file in the project
WorkBook wb = WorkBook.Load("path")
'load Excel file in the project
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkBook wb = WorkBook.Load("path")
VB   C#

The above will open the specified Excel file. Next, the Worksheet.

//open Excel WorkSheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//open Excel WorkSheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
'open Excel WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
VB   C#

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

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


3. Write Value in Specific Cell

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

WorkSheet["Cell Address"].Value="Assign the Value";
WorkSheet["Cell Address"].Value="Assign the Value";
WorkSheet("Cell Address").Value="Assign the Value"
VB   C#

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

/**
Write Value in Cell
anchor-write-value-in-specific-cell
**/
using IronXL;
static void Main(string[] args)
{ 
    //load Excel file 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet of sample.xlsx
    WorkSheet ws = wb.GetWorkSheet("Sheet1"); 
    //access A1 cell and write the value
    ws["A1"].Value = "new value"; 
    //save changes   
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
/**
Write Value in Cell
anchor-write-value-in-specific-cell
**/
using IronXL;
static void Main(string[] args)
{ 
    //load Excel file 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet of sample.xlsx
    WorkSheet ws = wb.GetWorkSheet("Sheet1"); 
    //access A1 cell and write the value
    ws["A1"].Value = "new value"; 
    //save changes   
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
'''
'''Write Value in Cell
'''anchor-write-value-in-specific-cell
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'load Excel file 
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'Open WorkSheet of sample.xlsx
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'access A1 cell and write the value
	ws("A1").Value = "new value"
	'save changes   
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfullt written in Excel File")
	Console.ReadKey()
End Sub
VB   C#

This code will write new value in the A1 cell 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.


4. Write Static Values in a Range

We can write new values in multiple cells, called a Range, as followings:

WorkSheet[From Cell Address:To Cell Address].Value="New Value";  
WorkSheet[From Cell Address:To Cell Address].Value="New Value";  
WorkSheet(From Cell Address:[To] Cell Address).Value="New Value"
VB   C#

In this way, we specify the range of cells From to To where the data will be written. After this, New Value will be written in all the cells which lie in this range. The 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.

/**
Write Static Values Range
anchor-write-static-values-in-a-range
**/
using IronXL;
static void Main(string[] args)
{
    //load Excel file 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet of sample.xlsx
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Specify range row wise and write new value
    ws["B2:B9"].Value = "new value";
    //Specify range column wise and write new value
    ws["C3:C7"].Value = "new value";
    //save changes  
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
/**
Write Static Values Range
anchor-write-static-values-in-a-range
**/
using IronXL;
static void Main(string[] args)
{
    //load Excel file 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet of sample.xlsx
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Specify range row wise and write new value
    ws["B2:B9"].Value = "new value";
    //Specify range column wise and write new value
    ws["C3:C7"].Value = "new value";
    //save changes  
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
'''
'''Write Static Values Range
'''anchor-write-static-values-in-a-range
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'load Excel file 
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'Open WorkSheet of sample.xlsx
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'Specify range row wise and write new value
	ws("B2:B9").Value = "new value"
	'Specify range column wise and write new value
	ws("C3:C7").Value = "new value"
	'save changes  
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfullt written in Excel File")
	Console.ReadKey()
End Sub
VB   C#

This code will write new value from B2 to B9 (Row Wise Range) and from C3 to C7 (Column Wise Range) of WorkSheet sheet1 of Excel file sample.xlsx. It used Static values for the Excel cells.


5. Write Dynamic Values in a Range

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

/**
Dynamic Values Range
anchor-write-dynamic-values-in-a-range
**/
using IronXL;
static void Main(string[] args)
{
    //load Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet of sample.xlsx
    WorkSheet ws = wb.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
        ws["B" + i].Value = "Value" + i;
        //write the Dynamic value in another row
        ws["D" + i].Value = "Value" + i;
    }
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
/**
Dynamic Values Range
anchor-write-dynamic-values-in-a-range
**/
using IronXL;
static void Main(string[] args)
{
    //load Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet of sample.xlsx
    WorkSheet ws = wb.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
        ws["B" + i].Value = "Value" + i;
        //write the Dynamic value in another row
        ws["D" + i].Value = "Value" + i;
    }
    wb.SaveAs("sample.xlsx"); 
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
'''
'''Dynamic Values Range
'''anchor-write-dynamic-values-in-a-range
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'load Excel file
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'Open WorkSheet of sample.xlsx
	Dim ws As WorkSheet = wb.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
		ws("B" & i).Value = "Value" & i
		'write the Dynamic value in another row
		ws("D" & i).Value = "Value" & i
	Next i
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfullt written in Excel File")
	Console.ReadKey()
End Sub
VB   C#

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


6. Replace Excel Cell Value

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

WorkSheet.Replace("old value", "new value");
WorkSheet.Replace("old value", "new value");
WorkSheet.Replace("old value", "new value")
VB   C#

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

6.1. Replace Cell Value in Specific Row

If we want to write a new value just in one specific row, then it can be done as follows:

WorkSheet.Rows[RowIndex].Replace("old value", "new value");
WorkSheet.Rows[RowIndex].Replace("old value", "new value");
WorkSheet.Rows(RowIndex).Replace("old value", "new value")
VB   C#

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

6.2. Replace Cell Value in Specific Column

In the same way, if we want to write new value over the old value within a specific column, then it can be done as follows:

WorkSheet.Columns[ColumnIndex].Replace("old value", "new Value")
WorkSheet.Columns[ColumnIndex].Replace("old value", "new Value")
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkSheet.Columns[ColumnIndex].Replace("old value", "new Value")
VB   C#

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.

6.3. Replace Cell Value in Specific Range

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

WorkSheet["From Cell Address : To Cell Address"].Replace("old value", "new value");
WorkSheet["From Cell Address : To Cell Address"].Replace("old value", "new value");
WorkSheet("From Cell Address : To Cell Address").Replace("old value", "new value")
VB   C#

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.

6.4. Example of all Functions

/**
Replace Excel Cell Values
anchor-example-of-all-functions
**/
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //write new above old in complete WorkSheet
    ws.Replace("old", "new");
    //write new above old just in row no 5 of WorkSheet
    ws.Rows[5].Replace("old", "new");
    //write new above old just in column no 4 of WorkSheet
    ws.Columns[4].Replace("old", "new");
    //write new above old just from A5 to H5 of WorkSheet
    ws["A5:H5"].Replace("old", "new");
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
/**
Replace Excel Cell Values
anchor-example-of-all-functions
**/
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //write new above old in complete WorkSheet
    ws.Replace("old", "new");
    //write new above old just in row no 5 of WorkSheet
    ws.Rows[5].Replace("old", "new");
    //write new above old just in column no 4 of WorkSheet
    ws.Columns[4].Replace("old", "new");
    //write new above old just from A5 to H5 of WorkSheet
    ws["A5:H5"].Replace("old", "new");
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfullt written in Excel File");
    Console.ReadKey();
}
'''
'''Replace Excel Cell Values
'''anchor-example-of-all-functions
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'write new above old in complete WorkSheet
	ws.Replace("old", "new")
	'write new above old just in row no 5 of WorkSheet
	ws.Rows(5).Replace("old", "new")
	'write new above old just in column no 4 of WorkSheet
	ws.Columns(4).Replace("old", "new")
	'write new above old just from A5 to H5 of WorkSheet
	ws("A5:H5").Replace("old", "new")
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfullt written in Excel File")
	Console.ReadKey()
End Sub
VB   C#

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