如何在 Excel 中合并和取消合并单元格

How to Merge and Unmerge Cells

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

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

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.

请注意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.

请注意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

常见问题解答

如何在C#中不使用Excel Interop合并单元格?

通过利用IronXL的Merge方法,您可以在C#中不使用Excel Interop合并单元格。这涉及加载工作簿,选择工作表,指定范围,并应用Merge方法来合并单元格。

如何在C#应用程序中取消合并单元格?

要在C#应用程序中使用IronXL取消合并单元格,您可以使用Unmerge方法。指定已合并区域的范围地址或索引以逆转合并过程。

开始使用IronXL进行Excel操作的流程是什么?

要开始使用IronXL进行Excel操作,先从NuGet下载库,加载或创建电子表格,并利用方法如MergeUnmerge对单元格进行编程操作。

我可以检索电子表格中合并区域的列表吗?

是的,您可以使用IronXL的GetMergedRegions方法检索电子表格中合并区域的列表。这对于识别您的工作表中哪些单元格已合并很有帮助。

是否可以在不安装Microsoft Excel的情况下管理Excel电子表格?

是的,IronXL允许您以编程方式管理Excel电子表格,包括合并和取消合并单元格,而无需在系统上安装Microsoft Excel。

在过滤范围内合并单元格时,我该注意什么?

在过滤范围内合并单元格可能会导致Excel文件中的冲突,这可能需要使用Excel修复工具来正确查看电子表格。

Merge方法会擦除单元格数据吗?

不会,Merge方法不会擦除数据。它合并单元格同时仅显示第一个单元格的值,但所有单元格的值仍可访问。

如何在IronXL中可视化合并和取消合并过程?

IronXL提供代码示例和演示,显示如何执行合并和取消合并过程。这些示例帮助您可视化如何编程操作Excel文件。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布