Group & Ungroup Rows & Columns

Apply group and ungroup of rows & columns with intuitive APIs without using Office Interop with IronXL library.

Group & Ungroup Rows

The GroupRows method groups rows by taking index positions. You can apply multiple groupings using this method.

To ungroup rows, use the UngroupRows method. This method acts like a cutting tool. When applied to the middle of a row group, it splits the group into two. However, the new groups remain connected as a single logical group. For example, ungrouping rows 2-4 within a group spanning 0-9 will result in groups 0-1 and 5-9.

// Import the IronXL library to work with Excel files
using IronXL;
using IronXL.Styles;

class ExcelGroupingExample
{
    static void Main()
    {
        // Load or create a new Excel workbook
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Access the first sheet in the workbook
        WorkSheet sheet = workbook.CreateWorkSheet("Sheet1");

        // Group rows from index 0 to 9
        sheet.GroupRows(0, 9);

        // Ungroup rows from index 2 to 4
        sheet.UngroupRows(2, 4);

        // Save changes to the workbook
        workbook.SaveAs("GroupedRows.xlsx");
    }
}
// Import the IronXL library to work with Excel files
using IronXL;
using IronXL.Styles;

class ExcelGroupingExample
{
    static void Main()
    {
        // Load or create a new Excel workbook
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Access the first sheet in the workbook
        WorkSheet sheet = workbook.CreateWorkSheet("Sheet1");

        // Group rows from index 0 to 9
        sheet.GroupRows(0, 9);

        // Ungroup rows from index 2 to 4
        sheet.UngroupRows(2, 4);

        // Save changes to the workbook
        workbook.SaveAs("GroupedRows.xlsx");
    }
}
' Import the IronXL library to work with Excel files
Imports IronXL
Imports IronXL.Styles

Friend Class ExcelGroupingExample
	Shared Sub Main()
		' Load or create a new Excel workbook
		Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

		' Access the first sheet in the workbook
		Dim sheet As WorkSheet = workbook.CreateWorkSheet("Sheet1")

		' Group rows from index 0 to 9
		sheet.GroupRows(0, 9)

		' Ungroup rows from index 2 to 4
		sheet.UngroupRows(2, 4)

		' Save changes to the workbook
		workbook.SaveAs("GroupedRows.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Group & Ungroup Columns

Columns can be grouped similarly to rows. Use the GroupColumns method to group columns by index or by specifying column names as strings. Create multiple column groups if needed.

Ungroup columns with the UngroupColumns method, which also acts as a cutting tool. Applying it at the center of a column group splits it into two. For instance, ungrouping columns C-D within a group A-F results in groups A-B and E-F.

// Import the IronXL library to manage Excel workbooks
using IronXL;
using IronXL.Styles;

class ExcelGroupingExample
{
    static void Main()
    {
        // Load or create a new Excel workbook
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Access the first sheet in the workbook
        WorkSheet sheet = workbook.CreateWorkSheet("Sheet1");

        // Group columns from index 0 to 5 (or from column 'A' to 'F')
        sheet.GroupColumns(0, 5); // or sheet.GroupColumns("A", "F");

        // Ungroup columns from index 2 to 3 (or columns 'C' to 'D')
        sheet.UngroupColumns(2, 3); // or sheet.UngroupColumns("C", "D");

        // Save changes to the workbook
        workbook.SaveAs("GroupedColumns.xlsx");
    }
}
// Import the IronXL library to manage Excel workbooks
using IronXL;
using IronXL.Styles;

class ExcelGroupingExample
{
    static void Main()
    {
        // Load or create a new Excel workbook
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Access the first sheet in the workbook
        WorkSheet sheet = workbook.CreateWorkSheet("Sheet1");

        // Group columns from index 0 to 5 (or from column 'A' to 'F')
        sheet.GroupColumns(0, 5); // or sheet.GroupColumns("A", "F");

        // Ungroup columns from index 2 to 3 (or columns 'C' to 'D')
        sheet.UngroupColumns(2, 3); // or sheet.UngroupColumns("C", "D");

        // Save changes to the workbook
        workbook.SaveAs("GroupedColumns.xlsx");
    }
}
' Import the IronXL library to manage Excel workbooks
Imports IronXL
Imports IronXL.Styles

Friend Class ExcelGroupingExample
	Shared Sub Main()
		' Load or create a new Excel workbook
		Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

		' Access the first sheet in the workbook
		Dim sheet As WorkSheet = workbook.CreateWorkSheet("Sheet1")

		' Group columns from index 0 to 5 (or from column 'A' to 'F')
		sheet.GroupColumns(0, 5) ' or sheet.GroupColumns("A", "F");

		' Ungroup columns from index 2 to 3 (or columns 'C' to 'D')
		sheet.UngroupColumns(2, 3) ' or sheet.UngroupColumns("C", "D");

		' Save changes to the workbook
		workbook.SaveAs("GroupedColumns.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Please note that all index positions mentioned above follow zero-based indexing. Grouping can only be applied as far as the cells contain values.