I am trying to write code which produces excel report with pivot table. For accomplishing this task I am using ClosedXML library. The output looks like this:

The problem is that I have to get all groups of data collapsed by default, i.e. in the output I should see the following:

In other words, my output should contain collapsed rows and only summary should be displayed. How can I achieve this in code? Which method should I use?
pt.ShowRowStripes = true;
secondWorksheet.FirstRow().Hide();
secondWorksheet.TabActive = true;
secondWorksheet.CollapseRows(1);
secondWorksheet.Rows().Collapse();
pt.EnableShowDetails = false;
pt.ShowValuesRow = false;
secondWorksheet.PageSetup.ShowGridlines = true;
secondWorksheet.ShowGridLines = true;
workbook.PageOptions.ShowGridlines = true;
secondWorksheet.PivotTables.First().EnableShowDetails = false;
secondWorksheet.CollapseRows(1);should work and collapse all rows in the groups. Not sure if there are conflicts when working with pivot tables. See this answer for another example. - Raidri