How to Group and Ungroup Rows & Columns

Introduction

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.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Group & Ungroup Rows Example

Please noteAll 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

Frequently Asked Questions

How can I group and ungroup rows in Excel using C#?

You can use the `GroupRows` method in IronXL to group rows by specifying their index positions. To ungroup, use the `UngroupRows` method on the previously grouped rows.

What is the process to group columns in Excel with a C# library?

In IronXL, you can group columns using the `GroupColumns` method, either by specifying the index numbers or the column characters.

Can I manage Excel files in C# without using Interop?

Yes, IronXL allows you to manage Excel files programmatically in C# without relying on Interop, including grouping and ungrouping rows and columns.

What happens when you ungroup a segment in a grouped section in Excel?

When you ungroup a segment in the middle of a grouped section using IronXL, it splits into two parts. These segments will not automatically form separate groups unless you group them again.

How do I install IronXL to work with Excel files in C#?

You can install IronXL by downloading it from the NuGet package manager, which allows you to start working with Excel files in C#.

What file formats can be exported from Excel using IronXL?

IronXL enables the export of Excel files to various formats, providing flexibility in how you manage and distribute your data.

Are there prerequisites for grouping cells in Excel using C#?

In IronXL, grouping and ungrouping can only be applied to cells that contain values, and you must use zero-based indexing for all positions.

How can I automate data organization in Excel using C#?

By using IronXL, you can automate data organization in Excel by grouping and ungrouping rows and columns, which simplifies the handling of large datasets.

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.