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 note
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

Frequently Asked Questions

What is the purpose of grouping and ungrouping rows and columns in Excel?

Grouping rows and columns in Excel creates collapsible sections, helping to organize data, simplify navigation, and facilitate analysis of large datasets. Ungrouping restores the original state, enhancing data management and focused examination.

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

Using IronXL, you can programmatically group rows with the 'GroupRows' method by specifying the start and end index positions. To ungroup, use the 'UngroupRows' method on previously grouped rows.

Can a C# library be used without Interop to manage Excel files?

Yes, IronXL enables you to group and ungroup rows and columns in Excel files programmatically without the need for Interop in C# .NET.

How do you group columns in an Excel sheet using a C# library?

To group columns using IronXL, use the 'GroupColumns' method, specifying the start and end index numbers or column characters of the columns you wish to group.

What happens when you ungroup a segment in the middle of a grouped section?

Ungrouping a segment in the middle of a grouped section splits it into two parts. However, these sections will not be considered separate groups unless grouped again.

How do you install a C# library to start working with Excel files?

Download the IronXL C# library from NuGet by visiting the IronXL.Excel package page to start working with Excel files in C#.

What file formats can you export Excel files to using a C# library?

IronXL allows you to export Excel files to various file formats as needed, providing flexibility in how you manage and distribute your data.

Are there any specific prerequisites for cells to be grouped using a C# library?

Grouping and ungrouping can only be applied to cells that contain values, with all index positions following zero-based indexing.

Chaknith related to Output
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.