How to Merge and Unmerge Cells in C# with IronXL
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.
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.
-
1Install IronXL with NuGet Package Manager
-
2Copy and run this code snippet.
var ws = WorkBook.Load("file.xlsx").DefaultWorkSheet; ws.Merge("B3:D3"); ws.SaveAs("merged.xlsx");C# -
3Deploy to test on your live environment
Start using IronXL in your project today with a free trial
Minimal Workflow (5 steps)

- Download C# library to merge and unmerge cells
- Load an existing or create a new spreadsheet
- Use the
Mergemethod to merge the desired range - Unmerge by specifying range address or index to
Unmergemethod - Export the modified spreadsheet
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.
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");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")What Happens to Cell Values When Merging?

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);
}Imports IronXL
Imports System.Collections.Generic
Imports System
Dim workBook As WorkBook = WorkBook.Create()
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Apply merge
workSheet.Merge("B4:C4")
workSheet.Merge("A1:A4")
workSheet.Merge("A6:D9")
' Retrieve merged regions
Dim retrieveMergedRegions As List(Of IronXL.Range) = workSheet.GetMergedRegions()
For Each mergedRegion As IronXL.Range In retrieveMergedRegions
Console.WriteLine(mergedRegion.RangeAddressAsString)
NextWhy 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.
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")What Are Common Issues When Unmerging Cells?

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