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.
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 blending cells effortlessly without Excel Interop.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
var ws = WorkBook.Load("file.xlsx").DefaultWorkSheet; ws.Merge("B3:D3"); ws.SaveAs("merged.xlsx");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download C# library to merge and unmerge cells
- Load an existing or create a brand new spreadsheet
- Utilize the
Mergemethod to merge the desired range - Perform unmerge by specifying the merge region range address or index to
Unmergemethod - Export the modified spreadsheet
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.
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")
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
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.
: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")
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.
