I'll be adding a blog on how to achieved this @ www.bluechilli.com but until then I'll copy here what I'm posting:
ClosedXML and Excel - How to add a pivot table
Pivot tables can be 90% done using ClosedXML by using a excel template as a base.
Excel template steps
The missing 10% is any new columns are not shown by default, the user will need to pick the columns.
Done!
ASP.Net C# Example:
using (var wb = new XLWorkbook(HostingEnvironment.MapPath("~/App_Data/My_Template.xlsx")))
{
....
wb.Worksheets.TryGetWorksheet("NWData", out wsNWData);
var nwTable = wsNWData.Table("NWTable");
nwTable.InsertRowsBelow(exportData.Networth.Count - 1, true);
if (exportData.Dates.Count > 2) nwTable.InsertColumnsAfter(exportData.Dates.Count - 2, true); //Handle non-default columns
Then using wsNWData.Cell(x,y) set any new column headers and add data rows
ClosedXML and Excel - How to add a pivot table
Pivot tables can be 90% done using ClosedXML by using a excel template as a base.
Excel template steps
- In excel setup a named table with appropriate header columns and data.
- Create a pivot table as desired using that named table as the source.
- Ensure in pivot options data saved is unchecked and refresh on file load is checked
-
Blank table data leaving 1 blank row in the named table range.
The missing 10% is any new columns are not shown by default, the user will need to pick the columns.
Done!
ASP.Net C# Example:
using (var wb = new XLWorkbook(HostingEnvironment.MapPath("~/App_Data/My_Template.xlsx")))
{
....
region Networth
IXLWorksheet wsNWData;wb.Worksheets.TryGetWorksheet("NWData", out wsNWData);
var nwTable = wsNWData.Table("NWTable");
nwTable.InsertRowsBelow(exportData.Networth.Count - 1, true);
if (exportData.Dates.Count > 2) nwTable.InsertColumnsAfter(exportData.Dates.Count - 2, true); //Handle non-default columns
Then using wsNWData.Cell(x,y) set any new column headers and add data rows