How to Group and Ungroup Rows and Columns in Excel | IronXL

How to Group & Ungroup Rows & Columns in C#

IronXL provides simple methods to programmatically group and ungroup rows and columns in Excel spreadsheets using C#, enabling collapsible sections without Interop dependencies for better data organization.

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 programmatic grouping and ungrouping without the need for Interop in C# .NET. Whether building financial reports, managing inventory data, or organizing employee records, IronXL's grouping functionality provides the flexibility to create hierarchical data structures that users can expand or collapse as needed.

Quickstart: Group and Ungroup Rows & Columns Effortlessly

Start organizing your Excel data in seconds—just load a workbook, call GroupRows, UngroupRows, GroupColumns or UngroupColumns on its worksheet, and save. IronXL makes getting started with grouping and ungrouping rows and columns intuitive and fast. For complex spreadsheets, check out our guide on managing worksheets for additional organizational techniques.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    IronXL.WorkBook.Load("data.xlsx").DefaultWorkSheet.GroupRows(0, 4).WorkBook.SaveAs("grouped.xlsx");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


How Do I Group & Ungroup Rows in Excel?

Please noteAll the index positions mentioned below follow zero-based indexing. Grouping and ungrouping can only be applied to cells that contain values.

Grouping rows in Excel is particularly useful when dealing with hierarchical data structures, such as organizational charts, financial statements with subcategories, or project timelines with phases. IronXL makes this process straightforward through its intuitive API. For more advanced spreadsheet operations, explore our comprehensive IronXL documentation.

How Do I Create Row Groups Using GroupRows?

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. This is especially helpful when organizing data into logical sections that users can expand or collapse for better readability.

When working with employee data, financial records, or inventory lists, row grouping allows you to create collapsible sections that improve spreadsheet navigation. The method accepts two parameters: the starting row index and ending row index (both inclusive).

: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

Excel sheet with rows 1-9 selected for grouping, showing employee data with ID, name, and job title columns

For more complex scenarios, you can create nested groups or multiple separate groups within the same worksheet. Learn more about selecting ranges to work with specific data sections effectively.

How Do I Remove Row Groups Using UngroupRows?

Use the UngroupRows method to ungroup rows previously grouped. This method can also 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.

The ungrouping functionality is essential when you need to reorganize your data structure or when preparing spreadsheets for different audiences who may not need the grouping hierarchy. This method provides flexibility in managing your Excel data organization dynamically.

: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

Excel spreadsheet with rows 2-9 selected (highlighted in red) showing employee data before grouping
Excel spreadsheet showing grouped rows 2-9 with employee data and grouping controls highlighted

How Do I Group & Ungroup Columns in Excel?

Column grouping is particularly valuable when working with wide spreadsheets containing multiple data categories. For instance, when managing financial data, you might group monthly columns by quarters, or when handling employee data, group personal information columns separately from performance metrics. For additional data manipulation techniques, see our guide on adding rows and columns.

How Do I Create Column Groups Using GroupColumns?

Columns can be grouped similarly to rows. Use the GroupColumns method to group columns by specifying either the index number or the column character. This flexibility allows you to work with column references in the format most convenient for your application.

The ability to group columns programmatically is invaluable when generating reports that need to show summary data with the option to drill down into details. This is commonly used in financial reporting, inventory management, and data analysis applications.

: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

Excel spreadsheet with employee data showing column C (Job Title) highlighted by red arrow for grouping demonstration

How Do I Remove Column Groups Using UngroupColumns?

Similar to ungrouping 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. This feature is particularly useful when restructuring reports or adapting spreadsheets for different viewing preferences.

When working with complex spreadsheets, ungrouping allows you to flatten the hierarchy temporarily for operations like sorting or applying formulas across all columns.

: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

Excel spreadsheet with employee data and red arrow pointing to Job Title column for grouping demonstration
Excel spreadsheet with grouped columns showing employee data and column grouping controls highlighted with red arrows

Advanced Grouping Techniques

For more sophisticated Excel automation scenarios, you can combine grouping with other IronXL features. Here's an example that demonstrates creating multiple nested groups:

using IronXL;

// Create hierarchical grouping for financial data
WorkBook workBook = WorkBook.Load("financial_report.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Create main category groups
workSheet.GroupRows(1, 5);   // Revenue section
workSheet.GroupRows(7, 11);  // Expenses section
workSheet.GroupRows(13, 17); // Summary section

// Create sub-groups within expenses
workSheet.GroupRows(8, 9);   // Operating expenses
workSheet.GroupRows(10, 11); // Administrative expenses

// Group quarterly columns
workSheet.GroupColumns(1, 3);   // Q1 (Jan-Mar)
workSheet.GroupColumns(4, 6);   // Q2 (Apr-Jun)
workSheet.GroupColumns(7, 9);   // Q3 (Jul-Sep)
workSheet.GroupColumns(10, 12); // Q4 (Oct-Dec)

workBook.SaveAs("hierarchical_financial_report.xlsx");
using IronXL;

// Create hierarchical grouping for financial data
WorkBook workBook = WorkBook.Load("financial_report.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Create main category groups
workSheet.GroupRows(1, 5);   // Revenue section
workSheet.GroupRows(7, 11);  // Expenses section
workSheet.GroupRows(13, 17); // Summary section

// Create sub-groups within expenses
workSheet.GroupRows(8, 9);   // Operating expenses
workSheet.GroupRows(10, 11); // Administrative expenses

// Group quarterly columns
workSheet.GroupColumns(1, 3);   // Q1 (Jan-Mar)
workSheet.GroupColumns(4, 6);   // Q2 (Apr-Jun)
workSheet.GroupColumns(7, 9);   // Q3 (Jul-Sep)
workSheet.GroupColumns(10, 12); // Q4 (Oct-Dec)

workBook.SaveAs("hierarchical_financial_report.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This approach creates a well-organized financial report where users can collapse or expand sections as needed. For additional formatting options, explore our guide on conditional formatting to highlight important data within grouped sections.

Best Practices and Performance Considerations

When working with grouping and ungrouping operations, consider these best practices:

  1. Plan Your Structure: Design your grouping hierarchy before implementation to avoid excessive regrouping operations
  2. Performance: Group operations are lightweight, but when working with large datasets, batch your operations together
  3. User Experience: Consider the end-user perspective when creating groups - logical groupings improve data comprehension
  4. Combine with Other Features: Leverage IronXL's autosize functionality to ensure grouped content displays properly

For complex enterprise applications, you might also want to implement password protection on workbooks containing sensitive grouped data, or export to different formats while maintaining the grouping structure.

Summary

IronXL's grouping and ungrouping functionality provides developers with powerful tools for organizing Excel data programmatically. Whether building reporting systems, data analysis tools, or automated Excel generators, these features enable you to create professional, user-friendly spreadsheets that enhance data readability and navigation. The ability to programmatically control data organization without Excel Interop dependencies makes IronXL an essential tool for modern .NET applications working with spreadsheet data.

Frequently Asked Questions

How do I group rows in Excel using C#?

You can group rows in Excel using IronXL's GroupRows method. Simply load your workbook, call GroupRows on the worksheet with the index positions of rows you want to group, and save the file. For example: IronXL.WorkBook.Load("data.xlsx").DefaultWorkSheet.GroupRows(0, 4).WorkBook.SaveAs("grouped.xlsx");

Can I group and ungroup columns in Excel programmatically?

Yes, IronXL provides both GroupColumns and UngroupColumns methods to programmatically group and ungroup columns in Excel spreadsheets using C#. These methods work similarly to row grouping and allow you to create collapsible column sections without needing Interop dependencies.

What types of data organization benefit from row and column grouping?

IronXL's grouping functionality is particularly useful for organizing hierarchical data structures such as financial reports with subcategories, organizational charts, project timelines with phases, employee records, and inventory data. It helps create collapsible sections that simplify navigation and analysis of large datasets.

Do I need Microsoft Office Interop to group rows and columns in C#?

No, IronXL enables programmatic grouping and ungrouping of rows and columns without requiring Microsoft Office Interop. This makes it a more lightweight and deployment-friendly solution for C# .NET applications.

Can I apply grouping to multiple sets of rows or columns?

Yes, with IronXL you can use the GroupRows and GroupColumns methods multiple times for the same or different groups of rows and columns. This flexibility allows you to create complex hierarchical data structures with multiple collapsible sections as needed.

What file formats are supported when saving grouped Excel data?

IronXL allows you to export Excel files with grouped rows and columns to various file formats. After applying grouping operations, you can save your workbook using the SaveAs method to maintain the grouping structure in the output file.

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.
Ready to Get Started?
Nuget Downloads 1,765,830 | Version: 2025.12 just released