How to Merge and Unmerge Cells in Excel

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.

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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    var ws = WorkBook.Load("file.xlsx").DefaultWorkSheet; ws.Merge("B3:D3"); ws.SaveAs("merged.xlsx");
  3. Deploy 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 noteMerging 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.

: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

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.

: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

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 noteThe cell address must correspond exactly to the merged region. You cannot 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

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

You can merge cells in Excel using C# with IronXL's Merge method. Simply load your workbook, call the Merge method with the cell range (e.g., ws.Merge("B3:D3")), and save the file. This combines multiple cells into one larger cell without requiring Excel Interop.

What happens to cell values when merging cells?

When using IronXL to merge cells, the process combines the cells without erasing any existing values or data. Only the value of the first cell in the merged region will be displayed in the merged cell, but IronXL still allows you to access all the original values programmatically.

Why does merging cells sometimes cause Excel conflicts?

Excel conflicts typically occur when merged cells overlap with filter ranges or when attempting to sort data containing merged cells. IronXL helps identify these issues - merging cells within filter ranges can require Excel repair to view the spreadsheet properly.

Can I unmerge cells that have been previously merged?

Yes, IronXL provides an Unmerge method that allows you to split merged cells back into their original individual cells. You can unmerge by specifying either the range address or index, providing flexibility for data analysis and formatting adjustments.

Do I need Microsoft Excel installed to merge cells programmatically?

No, IronXL allows you to merge and unmerge Excel cells in C# without requiring Microsoft Excel or Excel Interop to be installed. The library works independently, making it ideal for server environments and automated processes.

Is merging cells useful for creating formatted headers?

Yes, IronXL's merge functionality is particularly useful when creating spreadsheets with formatted headers or when working with Excel templates. It allows you to create professional-looking layouts by combining cells for titles and section headers.

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.
Ready to Get Started?
Nuget Downloads 1,753,501 | Version: 2025.12 just released