How to Merge and Unmerge Cells
Merging cells refers to the process of combining two or more adjacent cells into a single larger cell. Unmerging cells, on the other hand, is the opposite process where a merged cell is divided back into its original individual cells. This feature allows for flexibility, consistent alignment, and better data analysis.
IronXL enables the merging and unmerging of cells in a spreadsheet programmatically.
How to Merge and Unmerge Cells

- Download C# library to merge and unmerge cells
- Load an existing or create a brand new spreadsheet
- Utilize the
Merge
method to merge the desired range - Perform unmerge by specifying the merge region range address or index to
Unmerge
method - Export the modified spreadsheet
Get started with IronXL
Start using IronXL in your project today with a free trial.
Merge Cells Example
The Merge
method can be utilized to merge a range of cells. This process combines the cells without erasing any existing values or data, though only the value of the first cell in the merged region will be displayed. However, the values of the merged cells remain accessible in IronXL.
Please note
The code example below demonstrates how to merge a range of cells by specifying their addresses.
:path=/static-assets/excel/content-code-examples/how-to/csharp-excel-merge-cells-merge.cs
using IronXL;
// The main purpose of this code is to showcase how to use the IronXL library to merge cells in an Excel worksheet.
// Load the workbook from an Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Get the default worksheet of the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Define a range as the cells from B2 to B5
var range = workSheet["B2:B5"];
// Merge cells from B7 to E7 in the worksheet
workSheet.Merge("B7:E7");
// Merge the previously defined range (B2 to B5) using its address as a string
workSheet.Merge(range.RangeAddress);
// Save the workbook with merged cells as a new file
workBook.SaveAs("mergedCell.xlsx");
Imports IronXL
' The main purpose of this code is to showcase how to use the IronXL library to merge cells in an Excel worksheet.
' Load the workbook from an Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Get the default worksheet of the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Define a range as the cells from B2 to B5
Private range = workSheet("B2:B5")
' Merge cells from B7 to E7 in the worksheet
workSheet.Merge("B7:E7")
' Merge the previously defined range (B2 to B5) using its address as a string
workSheet.Merge(range.RangeAddress)
' Save the workbook with merged cells as a new file
workBook.SaveAs("mergedCell.xlsx")
Demonstration

Retrieve Merged Regions Example
Retrieving merged regions is a useful feature for identifying the displayed value in spreadsheet visualization software like Microsoft Excel. To obtain a list of merged regions, you can utilize the GetMergedRegions
method.
:path=/static-assets/excel/content-code-examples/how-to/csharp-excel-merge-cells-retrieve-merged-regions.cs
using IronXL;
using System.Collections.Generic;
using System;
// Create a new workbook and get the default worksheet.
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Apply merging to specific cell ranges on the worksheet.
// Merges the cells in the range B4 to C4.
workSheet["B4:C4"].Merge();
// Merges the cells in the range A1 to A4.
workSheet["A1:A4"].Merge();
// Merges the cells in the range A6 to D9.
workSheet["A6:D9"].Merge();
// Retrieve all the merged regions from the worksheet.
List<IronXL.Range> retrieveMergedRegions = workSheet.GetMergedRegions();
// Loop over each merged region and print the range address to the console.
foreach (IronXL.Range mergedRegion in retrieveMergedRegions)
{
Console.WriteLine(mergedRegion.RangeAddressAsString);
}
Imports IronXL
Imports System.Collections.Generic
Imports System
' Create a new workbook and get the default worksheet.
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Apply merging to specific cell ranges on the worksheet.
' Merges the cells in the range B4 to C4.
workSheet("B4:C4").Merge()
' Merges the cells in the range A1 to A4.
workSheet("A1:A4").Merge()
' Merges the cells in the range A6 to D9.
workSheet("A6:D9").Merge()
' Retrieve all the merged regions from the worksheet.
Dim retrieveMergedRegions As List(Of IronXL.Range) = workSheet.GetMergedRegions()
' Loop over each merged region and print the range address to the console.
For Each mergedRegion As IronXL.Range In retrieveMergedRegions
Console.WriteLine(mergedRegion.RangeAddressAsString)
Next mergedRegion
Unmerge Cells Example
Unmerging merged regions can be accomplished using two different approaches. The first and simplest method involves specifying the cell addresses, such as B3:B6
, to unmerge.
Alternatively, you can also unmerge cells based on the index of the merged region. The merged regions are listed in chronological order. To do this, you can retrieve the merged regions first and pass the desired index to the Unmerge
method.
Please note
:path=/static-assets/excel/content-code-examples/how-to/csharp-excel-merge-cells-unmerge.cs
using IronXL;
// Load the Excel workbook from the specified file named "mergedCell.xlsx".
WorkBook workBook = WorkBook.Load("mergedCell.xlsx");
// Access the default worksheet in the workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Unmerge the cells in the specified range "B7:E7" in the worksheet.
// This operation will separate cells that were previously merged in that range.
workSheet.Unmerge("B7:E7");
// Save the workbook with the changes to a new file named "unmergedCell.xlsx".
// The changes include unmerging of the specified cells.
workBook.SaveAs("unmergedCell.xlsx");
Imports IronXL
' Load the Excel workbook from the specified file named "mergedCell.xlsx".
Private workBook As WorkBook = WorkBook.Load("mergedCell.xlsx")
' Access the default worksheet in the workbook.
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Unmerge the cells in the specified range "B7:E7" in the worksheet.
' This operation will separate cells that were previously merged in that range.
workSheet.Unmerge("B7:E7")
' Save the workbook with the changes to a new file named "unmergedCell.xlsx".
' The changes include unmerging of the specified cells.
workBook.SaveAs("unmergedCell.xlsx")
Demonstration

Frequently Asked Questions
What is cell merging in Excel?
Cell merging refers to the process of combining two or more adjacent cells into a single larger cell. This feature is useful for creating titles or aligning data consistently.
How can I merge cells using IronXL in C#?
You can merge cells using IronXL by utilizing the `Merge` method on the specified range. The process involves loading the workbook, selecting the worksheet, specifying the range, and applying the `Merge` method.
Does merging cells in Excel erase data?
Merging cells does not erase existing data. Only the value in the first cell of the merged region is displayed, but other cell values remain accessible.
How can I unmerge cells using IronXL?
You can unmerge cells by specifying the cell addresses or by using the index of the merged region with the `Unmerge` method.
What is the `GetMergedRegions` method used for?
The `GetMergedRegions` method is used to retrieve a list of merged regions in a worksheet, which helps in identifying which cells are merged.
Can I use IronXL to merge cells without installing Excel?
Yes, IronXL allows you to merge and unmerge cells programmatically without requiring Microsoft Excel to be installed on your system.
Is it possible to merge cells within a filter range?
Merging cells within a filter range can cause conflicts in Excel, and may require using Excel repair to view the spreadsheet correctly.
What should I do to begin using IronXL for merging cells?
To start using IronXL, you need to download the C# library from NuGet, load or create a spreadsheet, and then you can use methods like `Merge` and `Unmerge` for cell operations.