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 note
Merging 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 note
The 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

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 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 in C#?

You can unmerge cells by specifying the cell addresses or by using the index of the merged region with the `Unmerge` method in IronXL.

What is the method used to retrieve merged regions?

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 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 a library 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.

Chaknith related to Demonstration
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.