I have a C# MVC project that allows users to export a datagrid to Excel. When users opt to export the displayed data, I need to also include another dataset on a separate worksheet and build a pivot table on a third worksheet. I have all of these steps completed except for the following:
- Add report filters as shown in PivotTable Field List > Report Filter section in Excel (screenshot attached)
- Change and format text in PivotTable column headers as needed
Can anybody provide example code on how to accomplish these tasks? Code snippet is below...
NOTES:
Using version 5.1.4.0 of Aspose.Cells.dll with .NET 4.0 framework
CODE SNIPPET:
#region Add pivot table
//Getting the pivottables collection in the sheet
Aspose.Cells.Pivot.PivotTableCollection pivotTables = pivotTableWorksheet.PivotTables;
//Adding a PivotTable to the worksheet
int index = pivotTables.Add("=Resource Capacity Data!A1:H" + Convert.ToString(resourceCapacityData.Count), "A1", "PivotTable1");
//Accessing the instance of the newly added PivotTable
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];
PivotFieldCollection flds = pivotTable.PageFields;
//Showing the grand totals
pivotTable.RowGrand = true;
pivotTable.ColumnGrand = true;
//Setting the PivotTable report is automatically formatted
pivotTable.IsAutoFormat = true;
//Setting the PivotTable autoformat type.
pivotTable.AutoFormatType = Aspose.Cells.Pivot.PivotTableAutoFormatType.Report6;
//Add the first field to the page/filter area.
//pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Page, 0);
//the above doesn't work to put a filter/page into the pivot table...
//Add the second field to the row area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 1);
//Add the third field to the row area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 2);
//Add the fifth field to the row area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 4);
//Add the sixth field to the column area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, 5);
//Add the eighth field to the data area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 7);
pivotTable.RefreshDataOnOpeningFile = true;
#endregion