指定数据透视表项的绝对位置
Contents
[
Hide
]
有时,用户需要指定数据透视表项的绝对位置,Aspose.Cells API公开了一些新属性和方法来满足此用户需求。
- 添加了PivotItem.setPosition()属性,可用于指定所有数据透视项的位置索引,而不论父节点如何。添加了PivotItem.setPositionInSameParentNode()属性,可用于指定在同一父节点下的数据透视项的位置索引。
- 添加了 PivotItem.move(int count, boolean isSameParent) 方法以根据计数值向上或向下移动项,其中计数是要向上或向下移动PivotItem的位置数。如果计数值小于零,项将向上移动,而如果计数值大于零,PivotItem将向下移动,布尔类型的 isSameParent 参数指定是否在同一父节点中执行移动操作或不执行移动操作。
- 废弃了 PivotItem.move(int count) 方法,因此建议使用新添加的 PivotItem.move(int count, boolean isSameParent) 方法。
请注意,在使用 PivotItem.setPosition()、PivotItem.setPositionInSameParentNode() 属性和 PivotItem.move(int count, boolean isSameParent) 方法之前,需要调用 PivotTable.refreshData 和 PivotTable.calculateData 方法。
示例代码
以下示例代码创建一个数据透视表,然后指定相同父节点中数据透视表项的位置。
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(SpecifyAbsolutePositionOfPivotItem.class); | |
Workbook wb = new Workbook(dataDir + "source.xlsx"); | |
Worksheet wsPivot = wb.getWorksheets().add("pvtNew Hardware"); | |
Worksheet wsData = wb.getWorksheets().get("New Hardware - Yearly"); | |
// Get the pivottables collection for the pivot sheet | |
PivotTableCollection pivotTables = wsPivot.getPivotTables(); | |
// Add PivotTable to the worksheet | |
int index = pivotTables.add("='New Hardware - Yearly'!A1:D621", "A3", "HWCounts_PivotTable"); | |
// Get the PivotTable object | |
PivotTable pvtTable = pivotTables.get(index); | |
// Add vendor row field | |
pvtTable.addFieldToArea(PivotFieldType.ROW, "Vendor"); | |
// Add item row field | |
pvtTable.addFieldToArea(PivotFieldType.ROW, "Item"); | |
// Add data field | |
pvtTable.addFieldToArea(PivotFieldType.DATA, "2014"); | |
// Turn off the subtotals for the vendor row field | |
PivotField pivotField = pvtTable.getRowFields().get("Vendor"); | |
pivotField.setSubtotals(PivotFieldSubtotalType.NONE, true); | |
// Turn off grand total | |
pvtTable.setColumnGrand(false); | |
// Please call the PivotTable.RefreshData() and PivotTable.CalculateData(). Before using PivotItem.Position, | |
// PivotItem.PositionInSameParentNode and PivotItem.Move(int count, bool isSameParent). | |
pvtTable.refreshData(); | |
pvtTable.calculateData(); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("4H12").setPositionInSameParentNode(0); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("DIF400").setPositionInSameParentNode(3); | |
/* | |
* As a result of using PivotItem.PositionInSameParentNode,it will change the original sort sequence, // so when you use | |
* PivotItem.PositionInSameParentNode in another parent node,you need call the method named "CalculateData" again. | |
*/ | |
pvtTable.calculateData(); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("CA32").setPositionInSameParentNode(1); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("AAA3").setPositionInSameParentNode(2); | |
// Save file | |
wb.save(dataDir + "output.xlsx"); |