How to Group and Ungroup Rows & Columns

by Chaknith Bin

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 note
All the index positions mentioned above 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. Multiple groupings of the same or different rows are possible by using the same method again.

: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
VB   C#

Output

Group Rows

Ungroup Rows

Use the UngroupRows method to ungroup rows. This method acts like a cutting tool. Apply it to the middle of a row group to split it into two. However, note that the two resulting groups of rows will not be considered as separate groups. For example, applying the ungroup method to rows 3-5 in a group of rows 0-8 will result in a group of 1-2 and 6-8.

: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
VB   C#

Output

Group Rows
Ungroup Rows

Group & Ungroup Columns Example

Group Columns

Columns can be grouped in a similar manner to rows. Use the GroupColumns method to group columns by specifying either the index number of the column or the character representing the column as a string. It is also possible to have multiple groups of columns.

: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")
VB   C#

Output

Group Columns

Ungroup Columns

Similar to ungrouping of rows, the UngroupColumn method acts as a cutting tool. Applying it to the middle of a column group will split it into two. For example, applying ungroup C-D to a column group of A-F will result in one group of A-B and E-F.

Additionally, please note that you can use UngroupColumn when specifying the character representing the column, and UngroupColumns when specifying the column index.

: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")
VB   C#

Output

Group Columns
Ungroup Columns
Chaknith related to Output

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.