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.


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!


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.

/**
Import and Edit SpreadSheet
anchor-edit-specific-cell-values
**/
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
}
/**
Import and Edit SpreadSheet
anchor-edit-specific-cell-values
**/
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
}
'''
'''Import and Edit SpreadSheet
'''anchor-edit-specific-cell-values
'''*
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:

BeforeAfter
beforeafter

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.

/**
Edit Full Row Values
anchor-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");
}
/**
Edit Full Row Values
anchor-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");
}
'''
'''Edit Full Row Values
'''anchor-edit-full-row-values
'''*
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:

BeforeAfter
beforeafter

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 columns of Excel SpreadSheet values with a single value.

/**
Edit Full Column Values
anchor-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");
}
/**
Edit Full Column Values
anchor-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");
}
'''
'''Edit Full Column Values
'''anchor-edit-full-column-values
'''*
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:

BeforeAfter
beforeafter

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:

/**
Edit Row Dynamic Values
anchor-edit-full-row-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.Columns.Count(); i++)
    {
        ws.Rows [3].Columns [i].Value = "New Value "+i.ToString();
    }        
    wb.SaveAs("sample.xlsx");
}
/**
Edit Row Dynamic Values
anchor-edit-full-row-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.Columns.Count(); i++)
    {
        ws.Rows [3].Columns [i].Value = "New Value "+i.ToString();
    }        
    wb.SaveAs("sample.xlsx");
}
'''
'''Edit Row Dynamic Values
'''anchor-edit-full-row-with-dynamic-values
'''*
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:

BeforeAfter
beforeafter

6. Edit Full Column with Dynamic Values

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

/**
Edit Column Dynamic Values
anchor-edit-full-column-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");
}
/**
Edit Column Dynamic Values
anchor-edit-full-column-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");
}
'''
'''Edit Column Dynamic Values
'''anchor-edit-full-column-with-dynamic-values
'''*
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:

BeforeAfter
beforeafter

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.

/**
Replace Cell Values
anchor-replace-specific-value-of-complete-worksheet
**/
ws.Replace("old value", "new value");
/**
Replace Cell Values
anchor-replace-specific-value-of-complete-worksheet
**/
ws.Replace("old value", "new value");
'''
'''Replace Cell Values
'''anchor-replace-specific-value-of-complete-worksheet
'''*
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 provides a very simple function to remove a specific row of an Excel WorkSheet. Let's see the example.

/**
Remove Row
anchor-remove-row-from-excel-worksheet
**/
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");
}
/**
Remove Row
anchor-remove-row-from-excel-worksheet
**/
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");
}
'''
'''Remove Row
'''anchor-remove-row-from-excel-worksheet
'''*
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:

BeforeAfter
beforeafter

9. Remove WorkSheet from Excel File

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

/**
Remove Worksheet from File
anchor-remove-worksheet-from-excel-file
**/
wb.RemoveWorkSheet(1); // by sheet indexing
/**
Remove Worksheet from File
anchor-remove-worksheet-from-excel-file
**/
wb.RemoveWorkSheet(1); // by sheet indexing
'''
'''Remove Worksheet from File
'''anchor-remove-worksheet-from-excel-file
'''*
wb.RemoveWorkSheet(1) ' by sheet indexing
VB   C#

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

wb.RemoveWorkSheet("Sheet1"); //by sheet name
wb.RemoveWorkSheet("Sheet1"); //by sheet name
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
Documentation related to 9. Remove WorkSheet from Excel File