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

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

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

from aspose.cells import SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load source excel file containing a pivot table having calculated items
wb = Workbook(dataDir + "sample.xlsx")
# Access first worksheet
sheet = wb.worksheets[0]
# Change the value of cell D2
sheet.cells.get("D2").put_value(20)
# Refresh and calculate all the pivot tables inside this sheet
for pt in sheet.pivot_tables:
pt.refresh_data()
pt.calculate_data()
# Save the workbook in output pdf
wb.save(dataDir + "RefreshAndCalculateItems_out.pdf", SaveFormat.PDF)