刷新和计算包含计算项的数据透视表
Contents
[
Hide
]
Aspose.Cells现在支持刷新和计算具有计算项的数据透视表。请像往常一样使用PivotTable.refreshData()和PivotTable.caclulateData()执行此函数。
刷新和计算包含计算项的数据透视表
以下示例代码加载了源Excel文件,其中包含一个具有“add”、“div”、“div2”等三个计算项的数据透视表。我们首先将单元格 D2 的值更改为 20,然后使用Aspose.Cells API刷新和计算数据透视表,并将工作簿保存为PDF格式。输出PDF中的结果显示Aspose.Cells已成功刷新和计算具有计算项的数据透视表。您可以使用Microsoft Excel手动将值20放入单元格D2,然后通过Alt+F5快捷键或点击数据透视表刷新按钮来验证。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(RefreshCalculatePivotTablehavingCalculatedItems.class); | |
// Load source excel file containing a pivot table having calculated | |
// items | |
Workbook wb = new Workbook(dataDir + "sample.xlsx"); | |
// Access first worksheet | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Change the value of cell D2 | |
sheet.getCells().get("D2").putValue(20); | |
// Refresh and calculate all the pivot tables inside this sheet | |
for (int i = 0; i < sheet.getPivotTables().getCount(); i++) { | |
PivotTable pt = sheet.getPivotTables().get(i); | |
pt.refreshData(); | |
pt.calculateData(); | |
} | |
// Save the workbook in output pdf | |
wb.save(dataDir + "output.pdf", SaveFormat.PDF); |