C# Edit Excel File

Developers have to be careful when they set out to modify and edit Excel files in C# because it can be easy for one misstep to change the whole document. Being able to rely on simple and efficient lines of code helps reduce the risk of error, and makes it easier for us to edit or delete Excel files programmatically. Today we'll walk through the steps necessary to edit Excel files in C# correctly and quickly using tested functions.

Import and Edit SpreadSheet
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");//import Excel SpreadSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");//access specific workshet
    ws.Rows[3].Columns[1].Value = "New Value";//access specific cell and modify its value
    wb.SaveAs("sample.xlsx");//save changes
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'import Excel SpreadSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'access specific workshet
	ws.Rows(3).Columns(1).Value = "New Value" 'access specific cell and modify its value
	wb.SaveAs("sample.xlsx") 'save changes
End Sub
Jump to Article
Edit Full Row Values
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Rows[3].Value = "New Value";        
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Rows(3).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
Jump to Article
Edit Full Column Values
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Columns[1].Value = "New Value";
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Columns(1).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
Jump to Article
Edit Row Dynamic Values
using IronXL;
static void Main(string[] args)
{
 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    for (int i = 0; i < ws.Columns.Count(); i++)
    {
        ws.Rows[3].Columns[i].Value = "New Value "+i.ToString();
    }        
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)

	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	For i As Integer = 0 To ws.Columns.Count() - 1
		ws.Rows(3).Columns(i).Value = "New Value " & i.ToString()
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
Jump to Article
Edit Column Dynamic Values
using IronXL;
static void Main(string[] args)
{
 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    for (int i = 0; i < ws.Rows.Count(); i++)
    {
        if (i == 0)//it is for if our first column is used as a header
            continue;
        ws.Rows[i].Columns[1].Value = "New Value " + i.ToString();
    }
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)

	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	For i As Integer = 0 To ws.Rows.Count() - 1
		If i = 0 Then 'it is for if our first column is used as a header
			Continue For
		End If
		ws.Rows(i).Columns(1).Value = "New Value " & i.ToString()
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
Jump to Article
Replace Cell Values
ws.Replace("old value", "new value");
ws.Replace("old value", "new value")
Jump to Article
Remove Row
using IronXL;
static void Main(string[] args)
{ 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Rows[3].RemoveRow();
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Rows(3).RemoveRow()
	wb.SaveAs("sample.xlsx")
End Sub
Jump to Article
Remove Worksheet from File
wb.RemoveWorkSheet(1)// by sheet indexing
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'wb.RemoveWorkSheet(1) ' by sheet indexing
Jump to Article
Try IronXL free for development


Step 1

1. C# Edit Excel Files using the IronXL Library

For this tutorial, we'll be using the functions defined by IronXL, a C# Excel library. To use these functions you'll need to first download and install it into your project (free for development).

You can either Download IronXL.zip or read more and install via the Nuget package page.

Once you've installed it, let's get started!


 PM > Install-Package IronXL.Excel


How to Tutorial

2. Edit Specific Cell Values

First, we will look at how to edit the specific cell values of an Excel SpreadSheet.

For this purpose, we import the Excel SpreadSheet which is to be modified, and then access its WorkSheet. Then we can apply the modifications as shown below.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");//import Excel SpreadSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");//access specific workshet
    ws.Rows[3].Columns[1].Value = "New Value";//access specific cell and modify its value
    wb.SaveAs("sample.xlsx");//save changes
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");//import Excel SpreadSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");//access specific workshet
    ws.Rows[3].Columns[1].Value = "New Value";//access specific cell and modify its value
    wb.SaveAs("sample.xlsx");//save changes
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'import Excel SpreadSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'access specific workshet
	ws.Rows(3).Columns(1).Value = "New Value" 'access specific cell and modify its value
	wb.SaveAs("sample.xlsx") 'save changes
End Sub
VB   C#

Here are before and after screenshots of Excel SpreadSheet sample.xlsx:

Before After
before after

We can see how simple it is to modify the Excel SpreadSheet value.

If needed, there is also an alternative way to edit the specific cell value by cell address:

 ws["B4"].Value = "New Value"; //alternative way to access specific cell and apply changes
 ws["B4"].Value = "New Value"; //alternative way to access specific cell and apply changes
ws("B4").Value = "New Value" 'alternative way to access specific cell and apply changes
VB   C#


3. Edit Full Row Values

It is pretty simple to edit full row values of an Excel SpreadSheet with a static value.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Rows[3].Value = "New Value";        
    wb.SaveAs("sample.xlsx");
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Rows[3].Value = "New Value";        
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Rows(3).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

See the screenshots of sample.xlsx below:

Before After
before after

For this, we also can edit the value of a specific range of the row, by using range function:

ws["A3:E3"].Value = "New Value";
ws["A3:E3"].Value = "New Value";
ws("A3:E3").Value = "New Value"
VB   C#


4. Edit Full Column Values

In the same way as above, we can easily edit full column of Excel SpreadSheet values with a single value.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Columns[1].Value = "New Value";
    wb.SaveAs("sample.xlsx");
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Columns[1].Value = "New Value";
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Columns(1).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

Which will produce our sample.xlsx spreadsheet as such:

Before After
before after


5. Edit Full Row with Dynamic Values

Using IronXL, it is also possible to edit specific rows with dynamic values. This means we can edit a full row by assigning dynamic values for each cell. Let's see the example:

using IronXL;
static void Main(string[] args)
{

    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    for (int i = 0; i < ws.Columns.Count(); i++)
    {
        ws.Rows[3].Columns[i].Value = "New Value "+i.ToString();
    }        
    wb.SaveAs("sample.xlsx");
}
using IronXL;
static void Main(string[] args)
{

    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    for (int i = 0; i < ws.Columns.Count(); i++)
    {
        ws.Rows[3].Columns[i].Value = "New Value "+i.ToString();
    }        
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)

	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	For i As Integer = 0 To ws.Columns.Count() - 1
		ws.Rows(3).Columns(i).Value = "New Value " & i.ToString()
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

In the table below, we see the screenshots of Excel SpreadSheet sample.xlsx from this output:

Before After
before after


6. Edit Full Column with Dynamic Values

It is also simple to edit specific columns with dynamic values.

using IronXL;
static void Main(string[] args)
{

    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    for (int i = 0; i < ws.Rows.Count(); i++)
    {
        if (i == 0)//it is for if our first column is used as a header
            continue;
        ws.Rows[i].Columns[1].Value = "New Value " + i.ToString();
    }
    wb.SaveAs("sample.xlsx");
}
using IronXL;
static void Main(string[] args)
{

    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    for (int i = 0; i < ws.Rows.Count(); i++)
    {
        if (i == 0)//it is for if our first column is used as a header
            continue;
        ws.Rows[i].Columns[1].Value = "New Value " + i.ToString();
    }
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)

	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	For i As Integer = 0 To ws.Rows.Count() - 1
		If i = 0 Then 'it is for if our first column is used as a header
			Continue For
		End If
		ws.Rows(i).Columns(1).Value = "New Value " & i.ToString()
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

With the table results of sample.xlsx below:

Before After
before after


7. Replace Spreadsheet Values

If we want to replace any type of value with an updated value in an Excel SpreadSheet, we can use the function named Replace. Using this function, we can replace the data of Excel SpreadSheet in any required situtation.

7.1. Replace Specific Value of Complete WorkSheet

To replace a specific value of a complete Excel WorkSheet with an updated value, we just access the WorkSheet ws (same as in the above examples) and apply the Replace function like this.

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

This function will replace old value with new value in a complete Excel WorkSheet.

Don't forget to save the file after any changing, as shown in the examples above.

7.2. Replace the Values of Specific Row

If you only want to make changes to a specific row instead of the whole rest of the worksheet, use this code.

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

The above code will replace old value with new value only in row number 2. The rest of the WorkSheet remains the same.

7.3. Replace the Values of Row Range

We also can replace the values within a specific range as follows:

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

Suppose, if we want to replace old value with new value, just in the range from B4 to E4 of row no 4, then we would write it like this:

ws["B4:E4"].Replace("old value", "new value");
ws["B4:E4"].Replace("old value", "new value");
ws("B4:E4").Replace("old value", "new value")
VB   C#

7.4. Replace the Values of Specific Column

We can also replace the values of a specific column, and the rest of the worksheet remains the same.

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

The above code will replace old value with new value just for column number 1.

7.5. Replace the Values of Column Range

By the following way, we also can use the range function to replace within a range of a specific column.

 ws["B5:B10"].Replace("old value", "new value");
 ws["B5:B10"].Replace("old value", "new value");
ws("B5:B10").Replace("old value", "new value")
VB   C#

Above code will replace old value with new value just within range from B5 to B10 for column B.


8. Remove Row from Excel WorkSheet

IronXL povides a very simple function to remove a specific row of an Excel WorkSheet. Let's see the example.

using IronXL;
static void Main(string[] args)
{ 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Rows[3].RemoveRow();
    wb.SaveAs("sample.xlsx");
}
using IronXL;
static void Main(string[] args)
{ 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    ws.Rows[3].RemoveRow();
    wb.SaveAs("sample.xlsx");
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Rows(3).RemoveRow()
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

The above code will remove row number 3 of sample.xlsx as shown in the following table:

Before After
before after


9. Remove WorkSheet from Excel File

If we want to remove a complete WorkSheet of an Excel file, we can use the following method:

wb.RemoveWorkSheet(1)// by sheet indexing
wb.RemoveWorkSheet(1)// by sheet indexing
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'wb.RemoveWorkSheet(1) ' by sheet indexing
VB   C#

wb is the WorkBook, same as in the above examples. If we want to remove woeksheet by name, then:

wb.RemoveWorkSheet("Sheet1")//by sheet name
wb.RemoveWorkSheet("Sheet1")//by sheet name
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'wb.RemoveWorkSheet("Sheet1") 'by sheet name
VB   C#

IronXL is rich with many more functions by which we can easily perform any type of editing and deletion in Excel SpreadSheets. Please reach out to our dev team if you have any questions for use in your project.


Library Quick Access

IronXL Library Documentation

Explore the full capabilities of IronXL C# Library with various functions for editing, deleting, styling, and perfecting your Excel workbooks.

IronXL Library Documentation