C# 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 Cells in Row
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");
}
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
Jump to Article
Unmerge Cells in Row
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");
}
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
Jump to Article
Merge Cells in Column
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");
}
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
Jump to Article
Unmerge Cells in Column
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");
}
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
Jump to Article
Try IronXL free for development


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 or by using a familiar software route, the Nuget package manager.


 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.

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

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

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");
}
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");
}
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
VB   C#

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

Before After
before after

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:

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

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

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");
}
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");
}
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
VB   C#

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

Before After
before after

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

4. C# Merge Cells in Excel Column

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

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

Let's see an example of how to the use above function to merge the columns of Excel SpreadSheet in 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");
}
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");
}
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
VB   C#

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 :

Before After
before after

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:

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

Let's see the example.

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");
}
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");
}
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
VB   C#

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

We can see it below with sample.xlsx :

Before After
before after


Library Quick Access

IronXL Object Reference Documentation

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL Object Reference Documentation.

IronXL Object Reference Documentation