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

:path=/static-assets/excel/content-code-examples/how-to/csharp-excel-merge-cells-merge.cs
using IronXL;

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

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

// Merge cells B7 to E7
workSheet.Merge("B7:E7");

// Merge selected range
workSheet.Merge(range.RangeAddressAsString);

workBook.SaveAs("mergedCell.xlsx");
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
workSheet.Merge("B7:E7")

' Merge selected range
workSheet.Merge(range.RangeAddressAsString)

workBook.SaveAs("mergedCell.xlsx")
VB   C#

Demonstration

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.

:path=/static-assets/excel/content-code-examples/how-to/csharp-excel-merge-cells-unmerge.cs
using IronXL;

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

// Unmerge the merged region of B7 to E7
workSheet.Unmerge("B7:E7");

workBook.SaveAs("unmergedCell.xlsx");
Imports IronXL

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

' Unmerge the merged region of B7 to E7
workSheet.Unmerge("B7:E7")

workBook.SaveAs("unmergedCell.xlsx")
VB   C#

Demonstration

Unmerge Cells Demonstration