How to group rows/columns in EPPlus

calendar_today Asked Sep 24, 2015
thumb_up 8 upvotes
history Updated April 14, 2026

Direct Answer

Looks like you want to do Row and Columns outlines that are collapsed. This should demonstrate how to do that: [TestMethod] public void Row_Col_Grouping_Test() {…. This is a 56-line Excel VBA snippet, ranked #254th of 303 by community upvote score, from 2015.


The Problem (Q-score 4, ranked #254th of 303 in the Excel VBA archive)

The scenario as originally posted in 2015

Is there a way to achieve this in EPPlus?
Only thing I could find on the internet is grouping specific data
for example:

AAA     --->    AAA     5 occurrences
AAA             BBB     2 occurences
BBB
BBB    
AAA
AAA
AAA

but not visually like in the screenshots

Why this Range / Worksheet targeting trips people up

The question centers on reaching a specific cell, range, or workbook object. In Excel VBA, this is the #1 source of failures after activation events: every property (.Value, .Formula, .Address) behaves differently depending on whether the parent Workbook is explicit or implicit.


The Verified Solution — niche answer (below median) (+8)

56-line Excel VBA pattern (copy-ready)

Looks like you want to do Row and Columns outlines that are collapsed. This should demonstrate how to do that:

[TestMethod]
public void Row_Col_Grouping_Test()
{
    //http://stackoverflow.com/questions/32760210/how-to-group-rows-columns-in-epplus

    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]
    {
        new DataColumn("Header", typeof (string)), new DataColumn("Col1", typeof (int)), new DataColumn("Col2", typeof (int)), new DataColumn("Col3", typeof (object))
    });

    for (var i = 0; i < 10; i++)
    {
        var row = datatable.NewRow();
        row[0] = String.Format("Header {0}", i); row[1] = i; row[2] = i*10; row[3] = Path.GetRandomFileName(); datatable.Rows.Add(row);
    }

    //Create a test file
    var fi = new FileInfo(@"c:tempgrouping.xlsx");
    if (fi.Exists)
        fi.Delete();

    using (var pck = new ExcelPackage(fi))
    {
        var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
        worksheet.Cells.LoadFromDataTable(datatable, true);

        worksheet.Cells["B12"].Formula = "SUM(B2:B11)";
        worksheet.Cells["C12"].Formula = "SUM(C2:C11)";

        //Row Group 1
        for (var i = 2; i <= 6; i++)
        {
            worksheet.Row(i).OutlineLevel = 1;
            worksheet.Row(i).Collapsed = true;
        }

        //Row Group 2
        for (var i = 6; i <= 10; i++)
        {
            worksheet.Row(i).OutlineLevel = 2;
            worksheet.Row(i).Collapsed = true;
        }

        //Column Group
        for (var i = 2; i <= 4; i++)
        {
            worksheet.Column(i).OutlineLevel = 1;
            worksheet.Column(i).Collapsed = true;
        }

        pck.Save();
    }
}


When to Use It — classic (2013–2016)

Ranked #254th in its category — specialized fit

This pattern sits in the 98% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.

What changed between 2015 and 2026

The answer is 11 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +8 vs the Excel VBA archive median ~4; this entry is niche. The score plus 4 supporting upvotes on the question itself (+4) means the asker and 7 subsequent voters all validated the approach.

Does the 56-line snippet run as-is in Office 2026?
expand_more

Yes. The 56-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

Published around 2015 — what’s changed since?
expand_more

Published 2015, which is 11 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #253?
expand_more

The pattern one rank above is “Mysterious use of LOOKUP”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 4, Answer-score 8, original post 2015, ranked #254th of 303 in the Excel VBA archive. Last regenerated April 14, 2026.