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.
How to Group and Ungroup Rows & Columns
- Download the C# library to group and ungroup rows and columns
- Load an existing Excel file or create a new one
- Apply the group and ungroup operations on rows
- Apply the group and ungroup operations on columns
- Export the Excel file to various file formats as needed
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.
Install-Package IronXL.Excel
Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip
Manually install into your project
Download DLLGroup & Ungroup Rows Example
Please note
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
Output
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
Output
Before
After
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")
Output
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")
Output
Before
After