Excel grouping

GemBox.Spreadsheet supports grouping of rows and columns.

In the following example grouping is set both on rows and columns, where rows are expanded and columns are collapsed.

Excel row and column grouping set with GemBox.Spreadsheet
Screenshot of Excel row and column grouping set with GemBox.Spreadsheet
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Grouping");

        worksheet.Cells[0].Value = "Cell grouping examples:";

        // Vertical grouping.
        worksheet.Cells[2, 0].Value = "GroupA Start";
        worksheet.Rows[2].OutlineLevel = 1;
        worksheet.Cells[3, 0].Value = "A";
        worksheet.Rows[3].OutlineLevel = 1;
        worksheet.Cells[4, 1].Value = "GroupB Start";
        worksheet.Rows[4].OutlineLevel = 2;
        worksheet.Cells[5, 1].Value = "B";
        worksheet.Rows[5].OutlineLevel = 2;
        worksheet.Cells[6, 1].Value = "GroupB End";
        worksheet.Rows[6].OutlineLevel = 2;
        worksheet.Cells[7, 0].Value = "GroupA End";
        worksheet.Rows[7].OutlineLevel = 1;
        // Put outline row buttons above groups.
        worksheet.ViewOptions.OutlineRowButtonsBelow = false;

        // Horizontal grouping (collapsed).
        worksheet.Cells["E2"].Value = "Gr.C Start";
        worksheet.Columns["E"].OutlineLevel = 1;
        worksheet.Columns["E"].Hidden = true;
        worksheet.Cells["F2"].Value = "C";
        worksheet.Columns["F"].OutlineLevel = 1;
        worksheet.Columns["F"].Hidden = true;
        worksheet.Cells["G2"].Value = "Gr.C End";
        worksheet.Columns["G"].OutlineLevel = 1;
        worksheet.Columns["G"].Hidden = true;
        worksheet.Columns["H"].Collapsed = true;

        workbook.Save("Grouping.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = New ExcelFile
        Dim worksheet = workbook.Worksheets.Add("Grouping")

        worksheet.Cells(0).Value = "Cell grouping examples:"

        ' Vertical grouping.
        worksheet.Cells(2, 0).Value = "GroupA Start"
        worksheet.Rows(2).OutlineLevel = 1
        worksheet.Cells(3, 0).Value = "A"
        worksheet.Rows(3).OutlineLevel = 1
        worksheet.Cells(4, 1).Value = "GroupB Start"
        worksheet.Rows(4).OutlineLevel = 2
        worksheet.Cells(5, 1).Value = "B"
        worksheet.Rows(5).OutlineLevel = 2
        worksheet.Cells(6, 1).Value = "GroupB End"
        worksheet.Rows(6).OutlineLevel = 2
        worksheet.Cells(7, 0).Value = "GroupA End"
        worksheet.Rows(7).OutlineLevel = 1
        ' Put outline row buttons above groups.
        worksheet.ViewOptions.OutlineRowButtonsBelow = False

        ' Horizontal grouping (collapsed).
        worksheet.Cells("E2").Value = "Gr.C Start"
        worksheet.Columns("E").OutlineLevel = 1
        worksheet.Columns("E").Hidden = True
        worksheet.Cells("F2").Value = "C"
        worksheet.Columns("F").OutlineLevel = 1
        worksheet.Columns("F").Hidden = True
        worksheet.Cells("G2").Value = "Gr.C End"
        worksheet.Columns("G").OutlineLevel = 1
        worksheet.Columns("G").Hidden = True
        worksheet.Columns("H").Collapsed = True

        workbook.Save("Grouping.%OutputFileType%")
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.