How to Merge and Unmerge Cells

IronXL enable merging and unmerging of Cells in spreadsheet programmatically.

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Merge Cells Example

The method Merge can be used to merge a range of cells. The merging process will not erase any value or data from the cells, however only the first column & row Cell's value of that merged region will be shown. The value of those merged cells is still accessible in IronXL.

Please note

  • Merging cells located inside filter range will conflict the Excel file and require to run Excel repair to view the Spreadsheet.

The code example below shows how to easily merge range of Cells by specifying the Cells address.

using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

var range = workSheet["B2:B5"];

// Merge cells B7 to E7

// Merge selected range

Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

Private range = workSheet("B2:B5")

' Merge cells B7 to E7

' Merge selected range

VB   C#


Merge Cells Demonstration

Unmerge Cells Example

There are two possible way to unmerge any merged regions. The most obvious one is to specify the cells address such as "B3:B6".

Please note

  • Cells address has to be of the exact merged region
  • It is not possible to unmerge potions of the merged region.

It is also possible to perform unmerging based on merged region indexed. The list of merged regions is ordered chronologically. It is currently not possible to access the list of merged regions via IronXL methods.

using IronXL;

WorkBook workBook = WorkBook.Load("mergedCell.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Unmerge the merged region of B7 to E7

Imports IronXL

Private workBook As WorkBook = WorkBook.Load("mergedCell.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Unmerge the merged region of B7 to E7

VB   C#


Unmerge Cells Demonstration