C# Excel Merge Cells
Published: 2022-02-02 04:30:10

---
canonical_url: https://ironsoftware.com/csharp/excel/docs/questions/csharp-excel-merge-cells
---

We're always looking for the easiest way to work with Excel data in C#. Today, we'll be using the functions provided by IronXL to merge cells in Excel using C#. Learn about merging the cells of rows and columns in an Excel spreadsheet programmatically, as well as unmerging cells when necessary. No complicated lines of code, just simple functions.

## Merge and Unmerge Cells in Excel using C#

---
**Step 1**

## 1. Download IronXL to Merge Cells Easily

With simplified lines of code for working with Excel documents, IronXL provides a smooth experience for merging cells programmatically. Try it free for development and follow along with the tutorial step-by-step.

To get started, access IronXL in your project either by [direct library download](https://ironsoftware.com/csharp/excel/packages/IronXL.Package.For.Merge.Cells.Excel.Csharp.zip) or by using a familiar software route, the [Nuget package manager](https://www.nuget.org/packages/IronXL.Excel).

`PM > Install-Package IronXL.Excel`

---
**How to Tutorial**

## 2. C# Merge Cells in Excel Row

Let's start by merging the cells in a row of Excel data. We can use the `WorkSheet.Merge()` function of IronXL. This function is defined with a string parameter for the Range of rows to be merged.

**C#:**
```
WorkSheet.Merge("From Cell Address:To Cell Address");
```

**VB:**
```
WorkSheet.Merge("From Cell Address:To Cell Address")
```

In this example, we will merge the 4th row of an Excel file:

**C#:**
```
using IronXL;
using System.Data; 
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");           
    ws.Merge("B4:G4"); //merge row no 4 from Cell Address B4 to G4            
    wb.SaveAs("sample.xlsx");
}
```

**VB:**
```
Imports IronXL
Imports System.Data
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Merge("B4:G4") 'merge row no 4 from Cell Address B4 to G4
	wb.SaveAs("sample.xlsx")
End Sub
```

We see the result of the above code on `sample.xlsx` :

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

## 3. C# Unmerge Cells in Excel Row

Alternatively, we can also unmerge the cells in a row. For this purpose we can use the `WorkSheet.Unmerge()` function as follows:

**C#:**
```
WorkSheet.Unmerge("from Cell adress:to cell address");
```

**VB:**
```
WorkSheet.Unmerge("from Cell adress:to cell address")
```

Let's see the example of how to unmerge the cells of an Excel SpreadSheet in C# row:

**C#:**
```
using IronXL;
using System.Data; 
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");            
    ws.Unmerge("B4:E4");            
    wb.SaveAs("sample.xlsx");
}
```

**VB:**
```
Imports IronXL
Imports System.Data
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Unmerge("B4:E4")
	wb.SaveAs("sample.xlsx")
End Sub
```

The result of the above code on `sample.xlsx` looks like this:

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

We can see how easy it was to unmerge the row `4` from `B4` to `E4`. 

## 4. C# Merge Cells in Excel Column

Same as row merging, we also can merge the columns of an Excel SpreadSheet using IronXL.

**C#:**
```
WorkSheet.Merge("From Cell Address:To Cell Address");
```

**VB:**
```
WorkSheet.Merge("From Cell Address:To Cell Address")
```

Let's see an example of how to the use above function to merge the columns of Excel SpreadSheet in C#.

**C#:**
```
using IronXL;
using System.Data; 
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");           
    ws.Merge("B3:B7");            
    wb.SaveAs("sample.xlsx");
}
```

**VB:**
```
Imports IronXL
Imports System.Data
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Merge("B3:B7")
	wb.SaveAs("sample.xlsx")
End Sub
```

This will merge the column `B` of `sample.xlsx` from `B3` to `B7`. Let's see the how the above code changed the Excel file `sample.xlsx` :

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

## 5. C# Unmerge Cells in Excel Column

And just as with the rows, it is also possible to unmerge cells in a column of an Excel SpreadSheet by using the WorkSheet.Unmerge() function of IronXL as follows:

**C#:**
```
WorkSheet.Unmerge("from Cell adress:to cell address");
```

**VB:**
```
WorkSheet.Unmerge("from Cell adress:to cell address")
```

Let's see the example.

**C#:**
```
using IronXL;
using System.Data; 
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");    
    WorkSheet ws = wb.GetWorkSheet("Sheet1");  
    ws.Unmerge("C3:C7");            
    wb.SaveAs("sample.xlsx");
}
```

**VB:**
```
Imports IronXL
Imports System.Data
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	ws.Unmerge("C3:C7")
	wb.SaveAs("sample.xlsx")
End Sub
```

The above code will unmerge column `B` of `sample.xlsx` from `C3` to `C7`.

We can see it below with `sample.xlsx` :

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

---