Question posted 2015 · +4 upvotes
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
Accepted answer +8 upvotes
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();
}
}
Excel VBA objects referenced (4)
String.Format— PivotTable.HasAutoFormat property (Excel)pivottable-hasautoformat-property-excel-638f892d-7dc9-314c-efaf-ee724b9f86f5String.Format— Formatting and VBA codes for headers and footersWorkbook— Workbooks and WorksheetsWorkbook— Add a Table of Contents to a Workbook
Top excel Q&A (6)
- Shortcut to Apply a Formula to an Entire Column in Excel +335 (2011)
- How should I escape commas and speech marks in CSV files so they work in Excel? +136 (2012)
- Convert xlsx to csv in linux command line +96 (2012)
- How to create a link inside a cell using EPPlus +50 (2011)
- IF statement: how to leave cell blank if condition is false ("" does not work) +44 (2013)
- T-SQL: Export to new Excel file +44 (2012)
excel solutions on this site
.