IRONSOFTWAREHOME

How to Merge and Unmerge Cells in C# with IronXL

Curtis Chau
Curtis Chau
Updated: August 2, 2026

IronXL enables C# developers to merge multiple Excel cells into one larger cell or unmerge them back to individual cells using simple methods like Merge("B3:D3") and Unmerge(), providing flexibility for formatting and data presentation without Excel Interop.

Quickstart: Merge a Range of Cells with a Single Call

In just a few lines, you can load a workbook, merge a specified cell range using IronXL's Merge method, and save the file. It's designed for developers to get started merging cells effortlessly without Excel Interop.

  1. 1Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. 2Copy and run this code snippet.

    var ws = WorkBook.Load("file.xlsx").DefaultWorkSheet; ws.Merge("B3:D3"); ws.SaveAs("merged.xlsx");
    C#
  3. 3Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

How Do I Merge Cells in Excel Using C#?

The Merge method can be used 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. This functionality is particularly useful when creating spreadsheets with formatted headers or when working with Excel templates.

Please note: Merging cells within the filter range can cause conflicts in the Excel file, requiring Excel repair to view the spreadsheet.

The code example below demonstrates how to merge a range of cells by specifying their addresses.

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");

What Happens to Cell Values When Merging?

Excel merge cells demonstration showing employee data before and after merging highlighted cells across columns

Why Does Merging Cells Sometimes Cause Excel Conflicts?

Merging cells refers to the process of combining two or more adjacent cells into a single larger cell. Unmerging cells 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. When working with cell formatting and styling cells, merged cells can sometimes interfere with other Excel features like sorting and filtering.

Excel conflicts typically occur when merged cells overlap with filter ranges or when attempting to sort data containing merged cells. These issues arise because Excel treats merged cells as a single unit, which can disrupt normal operations on individual cells within that range. Understanding these limitations helps when managing worksheets and planning your spreadsheet structure.

When Should I Use Cell Merging in Business Applications?

Cell merging is particularly useful for creating headers that span multiple columns, formatting reports for better visual presentation, and creating forms with larger input areas. It's commonly used in invoice templates, dashboards, and data entry forms. When exporting to Excel from business applications, merged cells help create professional-looking documents.

Common business scenarios include:

  • Report Headers: Creating titles that span the width of your data
  • Dashboard Design: Combining cells for summary statistics or KPIs
  • Form Creation: Merging cells for larger text input areas
  • Invoice Templates: Creating professional layouts with merged cells for company information

How Can I Retrieve All Merged Regions in a Worksheet?

Retrieving merged regions is useful for identifying the displayed value in spreadsheet visualization software like Microsoft Excel. To obtain a list of merged regions, use the GetMergedRegions method. This is especially helpful when loading existing spreadsheets to understand their structure.

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);
}

Why Would I Need to Get Merged Regions Programmatically?

Getting merged regions helps when you need to analyze spreadsheet structure, validate data integrity, or replicate formatting in other worksheets. It's essential for applications that process templates or perform automated spreadsheet modifications. This capability becomes crucial when editing Excel files programmatically or when building tools that need to preserve existing formatting.

Use cases for retrieving merged regions include:

  • Template Processing: Identifying merged regions in templates before populating data
  • Format Replication: Copying merge patterns from one worksheet to another
  • Data Validation: Ensuring data integrity when processing files with merged cells
  • Report Generation: Understanding existing merge patterns for dynamic report creation

What Order Are Merged Regions Returned In?

The merged regions are returned in chronological order based on when they were created. This ordering is important when using index-based unmerging operations. Understanding this order helps when implementing features like undo/redo functionality or when you need to select specific ranges for processing.

How Do I Unmerge Cells in Excel with IronXL?

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 unmerge cells based on the index of the merged region. The merged regions are listed in chronological order. To do this, retrieve the merged regions first and pass the desired index to the Unmerge method. This flexibility allows you to handle various scenarios when processing Excel files programmatically.

Please note: The cell address must correspond exactly to the merged region. You cannot unmerge portions of the merged region.
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");

What Are Common Issues When Unmerging Cells?

Excel spreadsheet comparison showing merged cells (left) versus unmerged cells (right) with employee data

Which Unmerge Method Should I Use?

Use address-based unmerging when you know the exact merged region coordinates. Use index-based unmerging when iterating through all merged regions programmatically or when the exact addresses may vary. Here's a comparison to help you decide:

Address-Based Unmerging:

  • Best for static templates with known merge patterns
  • Ideal when processing standardized reports
  • Simpler code when dealing with specific regions

Index-Based Unmerging:

  • Perfect for dynamic spreadsheets with varying merge patterns
  • Useful when processing user-uploaded files
  • Better for batch processing multiple merged regions

For more complex scenarios involving cell manipulation, explore the IronXL API Reference to discover additional methods and properties for handling merged cells efficiently.

Frequently Asked Questions

How do I merge cells in Excel using C# with IronXL?

To merge cells in Excel using C# with IronXL, utilize the Merge method. For example, to merge a range from B3 to D3, use `workSheet.Merge("B3:D3")`. This approach combines the cells without requiring Excel Interop, retaining the value of the first cell while remaining data is still accessible.

Can I merge and unmerge cells programmatically in C# without Excel Interop?

Yes, you can merge and unmerge cells programmatically in C# without Excel Interop using IronXL. It provides simple methods like `Merge` and `Unmerge` to accomplish this efficiently, allowing developers to manage Excel spreadsheets directly within their applications.

What happens to the values of the cells after merging them using IronXL?

When merging cells using IronXL, the value of the first cell in the merged range is displayed, while other values remain accessible in the program. This allows you to maintain data integrity while creating visually coherent headers or sections in your Excel files.

Why does merging cells sometimes cause issues in Excel?

Merging cells in Excel can cause issues, particularly with sorting and filtering, since merged cells are treated as a single entity. This can lead to conflicts when such cells overlap with other ranges. IronXL helps manage these challenges by providing direct access to merged cell properties.

When should cell merging be used in business applications?

Cell merging in business applications is ideal for formatting headers over multiple columns, creating visually appealing reports, and designing forms with larger input areas, such as invoices or dashboards. IronXL provides functionality to implement these features in exports and automated processing.

How can I retrieve all merged regions in a worksheet using IronXL?

To retrieve all merged regions in a worksheet using IronXL, use the `GetMergedRegions` method. This allows you to programmatically identify merged areas, which is useful for analyzing spreadsheet structure or ensuring data consistency across modifications.

What are some common use cases for retrieving merged regions with IronXL?

Common use cases include analyzing spreadsheet structure, validating data integrity, replicating formatting patterns across different sheets, and processing templates where merged regions need to be identified and managed programmatically.

How do I unmerge cells in Excel using IronXL?

Unmerge cells in Excel using IronXL by using the `Unmerge` method with either the cell address, like `B3:D3`, or by accessing the index of the merged region programmatically, using the `GetMergedRegions` method to identify the target merge.

What are the differences between address-based and index-based unmerging in IronXL?

Address-based unmerging is suitable for static templates where merge patterns are known, allowing you to specify exact coordinates. Index-based unmerging is more flexible for dynamic spreadsheets involving varying merge patterns and batch processing of multiple regions.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 2,237,574Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronXL"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronXL to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronXL.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required