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
// Importing the IronXL library to work with Excel files
using IronXL;

// Load an existing spreadsheet from a file
// Ensure the file path "sample.xlsx" is correct and accessible
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Access the default worksheet in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Group rows 1-9 in the worksheet. It's important to note that IronXL uses zero-based indexing.
// Therefore, row 1 is index 0 and row 9 is index 8 in zero-based indexing.
// GroupRows method will group the rows from index 0 to index 8 (which corresponds to rows 1-9).
workSheet.GroupRows(0, 8);

// Save the modified workbook to a new file
workBook.SaveAs("groupRow.xlsx");

// After running this code, the row grouping is saved in "groupRow.xlsx" file.
$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;

// This code demonstrates how to ungroup rows in a spreadsheet using the IronXL library.
// IronXL is an Excel Library to work with Excel files in .NET (C#).

// Load an existing spreadsheet from a file named "sample.xlsx".
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Get the default worksheet from the loaded workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Ungroup rows 3 to 5.
// Note that row indices are zero-based in IronXL, so rows 3-5 correspond to indices 2-4.
workSheet.UngroupRows(2, 4);

// Save the modified workbook to a new file named "ungroupRow.xlsx".
workBook.SaveAs("ungroupRow.xlsx");
$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; // Reference the IronXL library for Excel file manipulation

// Load an existing spreadsheet from file named "sample.xlsx"
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Access the default worksheet within the loaded workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Apply grouping to columns A (index 0) through F (index 5)
// This action visually groups the columns in the Excel sheet
workSheet.GroupColumns(0, 5);

// Save the modified workbook as a new file named "groupColumn.xlsx"
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;

// This code demonstrates how to load, modify, and save an Excel workbook using IronXL.

// Load an existing spreadsheet from a file called "sample.xlsx".
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Access the default worksheet in the workbook.
// IronXL uses the notion of a DefaultWorkSheet, which is typically the first sheet in the workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Ungroup columns C through D.
// This removes any grouping that may have been applied to these columns.
workSheet.UngroupColumn("C", "D");

// Save the modified workbook to a new file named "ungroupColumn.xlsx".
// This ensures the original file remains unchanged and the modifications are saved in a new file.
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 C#?

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 IronXL 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 IronXL?

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 IronXL 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 IronXL?

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 in IronXL?

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.