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.

Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


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 noteMerging cells within the filter range can cause conflicts in the Excel file, requiring the execution of Excel repair to view the spreadsheet.

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;

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")
$vbLabelText   $csharpLabel

Demonstration

Merge Cells 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;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Apply merge
workSheet.Merge("B4:C4");
workSheet.Merge("A1:A4");
workSheet.Merge("A6:D9");

// Retrieve merged regions
List<IronXL.Range> retrieveMergedRegions = workSheet.GetMergedRegions();

foreach (IronXL.Range mergedRegion in retrieveMergedRegions)
{
    Console.WriteLine(mergedRegion.RangeAddressAsString);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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 noteThe cell address must correspond exactly to the merged region. It is not possible to unmerge portions of the merged region.

: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")
$vbLabelText   $csharpLabel

Demonstration

Unmerge Cells Demonstration

Frequently Asked Questions

How can I merge cells in C# without using Excel Interop?

You can merge cells in C# without using Excel Interop by utilizing IronXL's Merge method. This involves loading a workbook, selecting the worksheet, specifying the range, and applying the Merge method to combine the cells.

How can I unmerge cells in a C# application?

To unmerge cells in a C# application using IronXL, you can use the Unmerge method. Specify the range address or the index of the merged region to reverse the merging process.

What is the process to begin using IronXL for Excel operations?

To start using IronXL for Excel operations, download the library from NuGet, load or create a spreadsheet, and utilize methods like Merge and Unmerge to perform cell operations programmatically.

Can I retrieve a list of merged regions in a spreadsheet?

Yes, you can retrieve a list of merged regions in a spreadsheet using IronXL's GetMergedRegions method. This is helpful for identifying which cells are merged in your worksheet.

Is it possible to manage Excel spreadsheets without installing Microsoft Excel?

Yes, IronXL allows you to manage Excel spreadsheets, including merging and unmerging cells, programmatically without needing Microsoft Excel installed on your system.

What should I consider when merging cells within a filter range?

Merging cells within a filter range can lead to conflicts in Excel files, which may require using Excel repair tools to correctly view the spreadsheet.

Does the Merge method erase data from cells?

No, the Merge method does not erase data. It combines cells while displaying only the first cell's value, but all cell values remain accessible.

How can I visualize the merging and unmerging process in IronXL?

IronXL provides code examples and demonstrations that show how to execute merging and unmerging processes. These examples help visualize how to manipulate Excel files programmatically.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.