EPPlus is a handy library on CodePlex that makes it easier to work with the Open Office XML format for Excel, especially without relying on the installation of Excel on a server environment.
I have been using it to write out site audits of SharePoint sites to a spreadsheet, then using some of the formatting capabilities to group, highlight, and improve viewability of the data.
One capability that I could not find was the ability to programmatically set the displayed group level for an entire worksheet (the equivalent of pressing the “1″ grouping button that is displayed when content is grouped) to collapsed, which is easily done in VBA as follows:
ActiveSheet.Outline.ShowLevels RowLevels:=1
which will collapse all groupings to the top-level of grouping (which would correspond to ExcelRow.OutlineLevel = 0 in EPPlus). That is the default display behavior I wanted, as some of my worksheets have up to five levels of groupings and a couple thousand rows.
But I could not find such a feature in the worksheet object, so I had to use some brute force to format my worksheets the way I want them to appear when the document opens.
Below is a code snippet using EPPlus that I run to format my columns in a consistent manner, then collapse all of the rows whose OutlineLevel is > 0. OutlineLevel is the property that manages the indenture of the Excel grouping feature. Note the usage of the Dimension.End properties, which make it trivial to find the extents of your data.
Update 11-7-2013: you should check Dimension for nullity before proceeding, as a blank worksheet will have null Dimension
EPPlus certainly reduces the discomfort associated with using Open Office XML, and offers a very rich set of features to generate complex documents rather easily. There have been no recent updates to the project, hopefully it will continue to be maintained.
Filed under: C#, General, Miscellaneous Tagged: Collapse, EPPlus, Excel, Formatting, Open Office XML
