在数据透视表中处理数据字段的数据显示格式

“从最小到最大的排名”和“从最大到最小的排名”显示格式选项

Aspose.Cells提供了设置数据透视表字段显示格式选项的功能。为此,API提供了PivotField.DataDisplayFormat属性。要按最大到最小排名,您可以将PivotField.DataDisplayFormat属性设置为PivotFieldDataDisplayFormat.RANK_LARGEST_TO_SMALLEST。以下代码片段演示了设置显示格式选项。

可从此处下载示例源文件和输出文件以测试示例代码:

源Excel文件

输出Excel文件

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// directories
String sourceDir = Utils.Get_SourceDirectory();
String outputDir = Utils.Get_OutputDirectory();
// Load a template file
Workbook workbook = new Workbook(sourceDir + "PivotTableSample.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
int pivotIndex = 0;
// Get the pivot tables in the sheet
PivotTable pivotTable = sheet.getPivotTables().get(pivotIndex);
// Accessing the data fields.
PivotFieldCollection pivotFields = pivotTable.getDataFields();
// Accessing the first data field in the data fields.
PivotField pivotField = pivotFields.get(0);
// Setting data display format
pivotField.setDataDisplayFormat(PivotFieldDataDisplayFormat.RANK_LARGEST_TO_SMALLEST);
pivotTable.calculateData();
// Saving the Excel file
workbook.save(outputDir + "PivotTableDataDisplayFormatRanking_out.xlsx");