刷新和计算包含计算项的数据透视表

刷新和计算包含计算项的数据透视表

以下示例代码加载包含三个计算项(如"add"、“div”、“div2”)的数据透视表的源Excel文件。我们首先将单元格D2的值更改为20,然后使用Aspose.Cells API刷新和计算数据透视表,并以PDF格式保存工作簿。在输出PDF中的结果显示Aspose.Cells成功刷新和计算了包含计算项的数据透视表。您可以通过在单元格D2中手动输入值20,然后通过Alt+F5快捷键或单击数据透视表刷新按钮来验证它。

// 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);