指定枢轴项的绝对位置
Contents
[
Hide
]
有时,用户需要指定枢轴项的绝对位置,Aspose.Cells API 公开了一些新属性和实现用户需求的方法。
- 添加枢轴项.位置可用于指定所有 PivotItems 中的位置索引的属性,而不考虑父节点。添加PivotItem.PositionInSameParentNode可用于指定同一父节点下的 PivotItems 中的位置索引的属性。
- 添加PivotItem.Move(int count, bool isSameParent)方法,以便根据计数值向上或向下移动项目,其中计数是将 PivotItem 向上或向下移动的位置数。如果计数值小于零,item将向上移动,如果计数值大于零,则PivotItem将向下移动,布尔类型isSameParent参数指定是否必须在同一父节点进行移动操作或不。
- 废弃的*PivotItem.Move(整数计数)*方法因此建议使用新添加的方法PivotItem.Move(int count, bool isSameParent)反而。
以下示例代码创建一个数据透视表,然后它指定同一父节点中的数据透视项位置。您可以下载源Excel和输出Excel文件供您参考。如果您打开输出的 Excel 文件,您将看到枢轴项“4H12”在父项“K11”中的第 0 个位置,“DIF400”在第 3 个位置。同样,CA32 在位置 1,AAA3 在位置 2
This file contains hidden or 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook wb = new Workbook(dataDir + "source.xlsx"); | |
Worksheet wsPivot = wb.Worksheets.Add("pvtNew Hardware"); | |
Worksheet wsData = wb.Worksheets["New Hardware - Yearly"]; | |
// Get the pivottables collection for the pivot sheet | |
PivotTableCollection pivotTables = wsPivot.PivotTables; | |
// Add PivotTable to the worksheet | |
int index = pivotTables.Add("='New Hardware - Yearly'!A1:D621", "A3", "HWCounts_PivotTable"); | |
// Get the PivotTable object | |
PivotTable pvtTable = pivotTables[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.RowFields["Vendor"]; | |
pivotField.SetSubtotals(PivotFieldSubtotalType.None, true); | |
// Turn off grand total | |
pvtTable.ColumnGrand = 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.RowFields["Item"].PivotItems["4H12"].PositionInSameParentNode = 0; | |
pvtTable.RowFields["Item"].PivotItems["DIF400"].PositionInSameParentNode = 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.RowFields["Item"].PivotItems["CA32"].PositionInSameParentNode = 1; | |
pvtTable.RowFields["Item"].PivotItems["AAA3"].PositionInSameParentNode = 2; | |
// Save file | |
wb.Save(dataDir + "output_out.xlsx"); |
请注意,使用前需要调用 PivotTable.RefreshData 和 PivotTable.CalculateData 方法枢轴项.位置, PivotItem.PositionInSameParentNode属性和PivotItem.Move(int count, bool isSameParent)方法。