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.

// 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
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");
}
$vbLabelText   $csharpLabel

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:

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

// 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
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");
}
$vbLabelText   $csharpLabel

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:

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

// 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
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");
}
$vbLabelText   $csharpLabel

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:

// 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
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");
}
$vbLabelText   $csharpLabel

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.

// 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
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");
}
$vbLabelText   $csharpLabel

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 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");
$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");
$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");
$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");
$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");
$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");
$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.

// Import necessary namespaces
using IronXL;

static void Main(string[] args)
{ 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    // Remove a specific row
    ws.Rows[3].RemoveRow();
    wb.SaveAs("sample.xlsx");
}
// Import necessary namespaces
using IronXL;

static void Main(string[] args)
{ 
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    // Remove a specific row
    ws.Rows[3].RemoveRow();
    wb.SaveAs("sample.xlsx");
}
$vbLabelText   $csharpLabel

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 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
$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 specific cell values in an Excel file using C#?

To edit specific cell values in an Excel file using C#, you can use the IronXL library. Load the Excel workbook, access the desired worksheet, and modify the cell value using the appropriate row and column indices.

What library is recommended for editing Excel files in C#?

The IronXL library is recommended for editing Excel files in C#. It allows you to perform various operations like modifying cell values, rows, columns, and even removing worksheets without using Interop.

How do I replace spreadsheet values in a C# application?

You can replace spreadsheet values in a C# application using the IronXL library's Replace function. This function allows you to replace specific values in the entire worksheet, a specific row, column, or within a specified range.

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

To remove a worksheet from an Excel file using C#, you can utilize the IronXL library's RemoveWorkSheet method. You can remove a worksheet either by its index or by its name.

Can I edit full row values with dynamic content using C#?

Yes, you can edit full row values with dynamic content using C#. IronXL allows you to assign dynamic values to each cell in a row by iterating through the columns.

Is it possible to edit entire columns in an Excel file using C#?

Yes, using the IronXL library, you can edit entire columns in an Excel file with a single value. You can set a static value for the entire column or use dynamic values as needed.

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

To remove a specific row from an Excel worksheet in C#, use the IronXL library's RemoveRow method on the desired row.

What are the steps to start editing Excel files with IronXL in C#?

To start editing Excel files with IronXL in C#, first download and install the IronXL library. Then, load your Excel workbook, access the worksheet you want to edit, and use the provided functions to modify cells, rows, columns, or worksheets.

Is IronXL suitable for development environments?

Yes, IronXL is suitable for development environments and can be freely used for development purposes. It provides a robust set of functions for editing Excel files programmatically.

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.