Refresh and Calculate Pivot Table having Calculated Items

Refresh and Calculate Pivot Table having Calculated Items

The following sample code loads the source excel file which contains a pivot table having three calculated items such as “add”, “div”, “div2”. We first change the value of cell D2 to 20 and then refresh and calculate pivot table using Aspose.Cells APIs and save the workbook in PDF format. The results in the output PDF shows that Aspose.Cells refreshed and calculated the pivot table having calculated items successfully. You can verify it using Microsoft Excel by manually putting the value 20 in cell D2 and then refreshing the pivot table via Alt+F5 shortcut key or clicking the pivot table Refresh button.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Load source excel file containing a pivot table having calculated items
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access first worksheet
Worksheet sheet = wb.Worksheets[0];
// Change the value of cell D2
sheet.Cells["D2"].PutValue(20);
// Refresh and calculate all the pivot tables inside this sheet
foreach (PivotTable pt in sheet.PivotTables)
{
pt.RefreshData();
pt.CalculateData();
}
// Save the workbook in output pdf
wb.Save(dataDir + "RefreshAndCalculateItems_out.pdf", SaveFormat.Pdf);