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

如何设置“从小到大排名”和“从大到小排名”的显示格式选项

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

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

【源 Excel 文件】(101089332.xlsx)

【输出 Excel 文件】(101089333.xlsx)

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldDataDisplayFormat
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# directories
sourceDir = RunExamples.Get_SourceDirectory()
outputDir = RunExamples.Get_OutputDirectory()
# Load a template file
workbook = Workbook(sourceDir + "PivotTableSample.xlsx")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotIndex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotIndex]
# Accessing the data fields.
pivotFields = pivotTable.data_fields
# Accessing the first data field in the data fields.
pivotField = pivotFields[0]
# Setting data display format
pivotField.data_display_format = PivotFieldDataDisplayFormat.RANK_LARGEST_TO_SMALLEST
pivotTable.calculate_data()
# Saving the Excel file
workbook.save(outputDir + "PivotTableDataDisplayFormatRanking_out.xlsx")