如何在 Excel 中分组和取消分组行与列 | IronXL

How to Group and Ungroup Rows & Columns

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

In Excel, the grouping feature helps organize data by creating collapsible sections for rows or columns. This simplifies navigation and analysis of large datasets. Conversely, the ungrouping feature restores the original ungrouped state. These features enhance data management and allow focused examination of specific spreadsheet sections.

IronXL enables programmatically grouping and ungrouping without the need for Interop in C# .NET.

Quickstart: Group and Ungroup Rows & Columns Effortlessly

Start organizing your Excel data in seconds—just load a workbook, call GroupRows, UngroupRows, GroupColumns or UngroupColumn on its worksheet, and save. IronXL makes getting started with grouping and ungrouping rows and columns intuitive and fast.

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.

    IronXL.WorkBook.Load("data.xlsx").DefaultWorkSheet.GroupRows(0, 4).WorkBook.SaveAs("grouped.xlsx");
  3. Deploy to test on your live environment

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


Group & Ungroup Rows Example

请注意All the index positions mentioned below follow zero-based indexing. Grouping and ungrouping can only be applied to cells that contain values.

Group Rows

The GroupRows method takes the index positions of rows to apply grouping. You can use this method multiple times for the same or different groups of rows if needed.

:path=/static-assets/excel/content-code-examples/how-to/group-and-ungroup-rows-columns-group-row.cs
using IronXL;

// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Ungroup row 1-9
workSheet.GroupRows(0, 7);

workBook.SaveAs("groupRow.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Group Rows

Ungroup Rows

Use the UngroupRows method to ungroup rows previously grouped. This method can also be used to split a group into two parts by applying it to the middle of the group. However, the resulting sections will not form separate groups unless grouped again.

:path=/static-assets/excel/content-code-examples/how-to/group-and-ungroup-rows-columns-ungroup-row.cs
using IronXL;

// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Ungroup row 3-5
workSheet.UngroupRows(2, 4);

workBook.SaveAs("ungroupRow.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Group Rows
Ungroup Rows

Group & Ungroup Columns Example

Group Columns

Columns can be grouped similarly to rows. Use the GroupColumns method to group columns by specifying either the index number or the column character.

:path=/static-assets/excel/content-code-examples/how-to/group-and-ungroup-rows-columns-group-column.cs
using IronXL;

// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Apply grouping to column A-F
workSheet.GroupColumns(0, 5);

workBook.SaveAs("groupColumn.xlsx");
Imports IronXL

' Load existing spreadsheet
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Apply grouping to column A-F
workSheet.GroupColumns(0, 5)

workBook.SaveAs("groupColumn.xlsx")
$vbLabelText   $csharpLabel

Output

Group Columns

Ungroup Columns

Similar to the ungrouping of rows, you can use the UngroupColumns method to split groups of columns. Applying this method in the middle of a column group will split it into two parts.

:path=/static-assets/excel/content-code-examples/how-to/group-and-ungroup-rows-columns-ungroup-column.cs
using IronXL;

// Load existing spreadsheet
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Ungroup column C-D
workSheet.UngroupColumn("C", "D");

workBook.SaveAs("ungroupColumn.xlsx");
Imports IronXL

' Load existing spreadsheet
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Ungroup column C-D
workSheet.UngroupColumn("C", "D")

workBook.SaveAs("ungroupColumn.xlsx")
$vbLabelText   $csharpLabel

Output

Group Columns
Ungroup Columns

常见问题解答

我怎样才能使用 C# 在 Excel 中对行进行分组和取消分组?

您可以在 IronXL 中使用 `GroupRows` 方法,通过指定索引位置对行进行分组。要取消分组,请对先前分组的行使用 `UngroupRows` 方法。

使用 C# 库在 Excel 中对列进行分组的过程是什么?

在 IronXL 中,您可以使用 `GroupColumns` 方法对列进行分组,通过指定索引号或列字符。

我可以在不使用 Interop 的情况下用 C# 管理 Excel 文件吗?

是的,IronXL 允许您在 C# 中以编程方式管理 Excel 文件,而无需依赖 Interop,包括对行和列的分组和取消分组。

当您在 Excel 的分组区段中取消分组一个片段时会发生什么?

当您使用 IronXL 在分组区段中间取消分组一个片段时,它会分成两部分。这些片段不会自动形成独立的组,除非您再次对它们进行分组。

如何安装 IronXL 以使用 C# 处理 Excel 文件?

您可以通过下载 NuGet 包管理器中的 IronXL 来开始在 C# 中处理 Excel 文件。

可以使用 IronXL 从 Excel 导出哪些文件格式?

IronXL 使 Excel 文件可以导出为各种格式,为您管理和分发数据提供灵活性。

使用 C# 在 Excel 中分组单元格有什么前提条件吗?

在 IronXL 中,只有包含值的单元格才能进行分组和取消分组操作,并且您必须为所有位置使用以零为基的索引。

如何使用 C# 自动化 Excel 中的数据组织?

通过使用 IronXL,您可以通过对行和列进行分组和取消分组来自动化 Excel 中的数据组织,简化大数据集的处理。

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