C# Edit Excel File
Published: 2022-02-16 06:45:05

---
canonical_url: https://ironsoftware.com/csharp/excel/docs/questions/csharp-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**

**C#:**
```
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
}
```

**VB:**
```
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
```

---

## 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](https://ironsoftware.com/csharp/excel/packages/IronXL.Package.For.Edit.Excel.Csharp.zip) or read more and install via [NuGet package page](https://www.nuget.org/packages/IronXL.Excel).

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.

**C#:**
```
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
}
```

**VB:**
```
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
```

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

![Image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4b6pc2eoqmwpqlwdp54.png)
 
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:

**C#:**
```
 ws["B4"].Value = "New Value"; //alternative way to access specific cell and apply changes
```

**VB:**
```
ws("B4").Value = "New Value" 'alternative way to access specific cell and apply changes
```

---

## 3. Edit Full Row Values

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

**C#:**
```
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");
}
```

**VB:**
```
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
```

See the screenshots of `sample.xlsx` below:

![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mmsmttjm0xzfpa8rq8i.png)
 
For this, we also can edit the value of a specific range of the row, by using range function:

**C#:**
```
ws["A3:E3"].Value = "New Value";
```

**VB:**
```
ws("A3:E3").Value = "New Value"
```

---

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

**C#:**
```
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");
}
```

**VB:**
```
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
```

Which will produce our sample.xlsx spreadsheet as such:

![Image 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cad8qy2puw6vlkkrhef0.png)
 
---

## 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:

**C#:**
```
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");
}
```

**VB:**
```
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
```

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

![Image 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w28ighkzqnoaqxs4tpep.png)
 
---

## 6. Edit Full Column with Dynamic Values

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

**C#:**
```
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");
}
```

**VB:**
```
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
```

With the table results of `sample.xlsx` below:

![Image 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvw4jbvht3xsyv856dii.png)
 
---

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

**C#:**
```
ws.Replace("old value", "new value");
```

**VB:**
```
ws.Replace("old value", "new value")
```

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.

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

**VB:**
```
ws.Rows(2).Replace("old value", "new value")
```

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:

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

**VB:**
```
ws("From Cell Address : To Cell Address").Replace("old value", "new value")
```

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:

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

**VB:**
```
ws("B4:E4").Replace("old value", "new value")
```

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

**C#:**
```
ws.Columns[1].Replace("old value", "new Value")
```

**VB:**
```
'ws.Columns(1).Replace("old value", "new Value")
```

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.

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

**VB:**
```
ws("B5:B10").Replace("old value", "new value")
```

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.

**C#:**
```
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");
}
```

**VB:**
```
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
```

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

![Image 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wkqre5a1pl40tnd0izi.png)
 
---

## 9. Remove WorkSheet from Excel File

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

**C#:**
```
wb.RemoveWorkSheet(1)// by sheet indexing
```

**VB:**
```
'wb.RemoveWorkSheet(1) ' by sheet indexing
```

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

**C#:**
```
wb.RemoveWorkSheet("Sheet1")//by sheet name
```

**VB:**
```
'wb.RemoveWorkSheet("Sheet1") 'by sheet name
```

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.

---