刷新并计算具有计算项的数据透视表

刷新并计算具有计算项的数据透视表

下面的示例代码加载源文件其中包含一个数据透视表,其中包含三个计算项,例如“add”、“div”、“div2”。我们首先将单元格 D2 的值更改为 20,然后使用 Aspose.Cells API 刷新和计算数据透视表,并将工作簿保存为 PDF 格式。结果在输出 PDF显示Aspose.Cells刷新计算数据透视表计算项成功。您可以使用 Microsoft Excel 验证它,方法是手动将值 20 放入单元格 D2,然后通过 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);