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

Replace x.x.x with the appropriate version number as needed.


How to Tutorial

2. Edit Specific Cell Values

First, we will look at how to edit 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.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-specific-cell-value.cs
// Import necessary namespaces
using IronXL;

static void Main(string[] args)
{
    // Load the Excel workbook
    WorkBook wb = WorkBook.Load("sample.xlsx");
    // Access a specific worksheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    // Access specific cell by identifying its row and column, then modify its value
    ws.Rows[3].Columns[1].Value = "New Value";
    // Save changes to the workbook
    wb.SaveAs("sample.xlsx");
}
' Import necessary namespaces
Imports IronXL

Shared Sub Main(ByVal args() As String)
	' Load the Excel workbook
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	' Access a specific worksheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	' Access specific cell by identifying its row and column, then modify its value
	ws.Rows(3).Columns(1).Value = "New Value"
	' Save changes to the workbook
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

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:

// 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
ws("B4").Value = "New Value"
$vbLabelText   $csharpLabel

3. Edit Full Row Values

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

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-row-value.cs
// Import necessary namespaces
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    // Setting a static value for the entire row
    ws.Rows[3].Value = "New Value";        
    wb.SaveAs("sample.xlsx");
}
' Import necessary namespaces
Imports IronXL

Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	' Setting a static value for the entire row
	ws.Rows(3).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

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:

// Editing a specific range of a row
ws["A3:E3"].Value = "New Value";
// Editing a specific range of a row
ws["A3:E3"].Value = "New Value";
' Editing a specific range of a row
ws("A3:E3").Value = "New Value"
$vbLabelText   $csharpLabel

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.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-full-column.cs
// Import necessary namespaces
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    // Setting a static value for the entire column
    ws.Columns[1].Value = "New Value";
    wb.SaveAs("sample.xlsx");
}
' Import necessary namespaces
Imports IronXL

Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	' Setting a static value for the entire column
	ws.Columns(1).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

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:

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-full-row-dynamic.cs
// Import necessary namespaces
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++)
    {
        // Assign dynamic values to each cell in the row
        ws.Rows[3].Columns[i].Value = "New Value " + i.ToString();
    }        
    wb.SaveAs("sample.xlsx");
}
' Import necessary namespaces
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
		' Assign dynamic values to each cell in the row
		ws.Rows(3).Columns(i).Value = "New Value " & i.ToString()
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

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.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-full-column-dynamic.cs
// Import necessary namespaces
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++)
    {
        // Skip the first row if it's used as a header
        if (i == 0)
            continue;
        // Assign dynamic values to each cell in the column
        ws.Rows[i].Columns[1].Value = "New Value " + i.ToString();
    }
    wb.SaveAs("sample.xlsx");
}
' Import necessary namespaces
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
		' Skip the first row if it's used as a header
		If i = 0 Then
			Continue For
		End If
		' Assign dynamic values to each cell in the column
		ws.Rows(i).Columns(1).Value = "New Value " & i.ToString()
	Next i
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

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 situation.

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 a specific value in the entire worksheet
ws.Replace("old value", "new value");
// Replace a specific value in the entire worksheet
ws.Replace("old value", "new value");
' Replace a specific value in the entire worksheet
ws.Replace("old value", "new value")
$vbLabelText   $csharpLabel

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

Don't forget to save the file after any change, 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 worksheet, use this code.

// Replace a specific value in a specific row
ws.Rows[2].Replace("old value", "new value");
// Replace a specific value in a specific row
ws.Rows[2].Replace("old value", "new value");
' Replace a specific value in a specific row
ws.Rows(2).Replace("old value", "new value")
$vbLabelText   $csharpLabel

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:

// Replace specific values in a row range
ws["From Cell Address : To Cell Address"].Replace("old value", "new value");
// Replace specific values in a row range
ws["From Cell Address : To Cell Address"].Replace("old value", "new value");
' Replace specific values in a row range
ws("From Cell Address : To Cell Address").Replace("old value", "new value")
$vbLabelText   $csharpLabel

Suppose, if we want to replace an old value with a 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")
$vbLabelText   $csharpLabel

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.

// Replace specific values in a column
ws.Columns[1].Replace("old value", "new value");
// Replace specific values in a column
ws.Columns[1].Replace("old value", "new value");
' Replace specific values in a column
ws.Columns(1).Replace("old value", "new value")
$vbLabelText   $csharpLabel

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.

// Replace specific values in a column range
ws["B5:B10"].Replace("old value", "new value");
// Replace specific values in a column range
ws["B5:B10"].Replace("old value", "new value");
' Replace specific values in a column range
ws("B5:B10").Replace("old value", "new value")
$vbLabelText   $csharpLabel

The 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.

:path=/static-assets/excel/content-code-examples/how-to/csharp-edit-excel-file-row-value.cs
// Import necessary namespaces
using IronXL;

static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    // Setting a static value for the entire row
    ws.Rows[3].Value = "New Value";        
    wb.SaveAs("sample.xlsx");
}
' Import necessary namespaces
Imports IronXL

Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	' Setting a static value for the entire row
	ws.Rows(3).Value = "New Value"
	wb.SaveAs("sample.xlsx")
End Sub
$vbLabelText   $csharpLabel

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:

// Remove a worksheet by its index
wb.RemoveWorkSheet(1); // by sheet indexing
// Remove a worksheet by its index
wb.RemoveWorkSheet(1); // by sheet indexing
' Remove a worksheet by its index
wb.RemoveWorkSheet(1) ' by sheet indexing
$vbLabelText   $csharpLabel

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

// Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1"); //by sheet name
// Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1"); //by sheet name
' Remove a worksheet by its name
wb.RemoveWorkSheet("Sheet1") 'by sheet name
$vbLabelText   $csharpLabel

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

Frequently Asked Questions

How can I edit Excel files in C# without using Interop?

You can edit Excel files in C# without using Interop by utilizing the IronXL library. IronXL provides a variety of methods for modifying Excel documents, such as editing cells, rows, and columns, and even removing worksheets.

How do I modify cell values in an Excel file using IronXL?

To modify cell values in an Excel file using IronXL, load the Excel workbook and access the specific worksheet. You can change the cell value by specifying the row and column indices and then saving the workbook.

What is the method to replace values in a C# Excel application?

In a C# Excel application, you can replace values using IronXL's Replace function. This allows you to replace specific values across the entire worksheet, within a particular row, column, or range.

How can I remove a worksheet from an Excel file programmatically in C#?

You can remove a worksheet from an Excel file programmatically in C# using IronXL's RemoveWorkSheet method. You can specify the worksheet to be removed by its name or index.

Can I edit entire rows in an Excel file using C#?

Yes, with IronXL, you can edit entire rows in an Excel file using C#. You can assign either static or dynamic values to the cells within a row by iterating through the columns.

Is it possible to set a static value for an entire column in an Excel file?

Yes, IronXL allows you to set a static value for an entire column in an Excel file. You can also use dynamic values if needed, by iterating over each cell in the column.

How do I remove a specific row from an Excel worksheet using C#?

To remove a specific row from an Excel worksheet using C#, use the IronXL library's RemoveRow method, specifying the row you wish to delete.

What are the initial steps to start editing Excel files using C#?

To begin editing Excel files using C#, first download and install the IronXL library. Then, load your Excel workbook, select the worksheet you wish to edit, and utilize IronXL's functions to modify cells, rows, columns, or worksheets as needed.

Is IronXL suitable for development environments?

Yes, IronXL is highly suitable for development environments. It can be freely used for development purposes and offers a comprehensive suite of functions for programmatically editing Excel files.

How can I convert an Excel file to a different format using C#?

IronXL allows conversion of Excel files to different formats using its export functions. You can save your modified Excel workbook to formats like CSV, HTML, or PDF using IronXL's methods.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.