为数据透视表创建切片器
Contents
[
Hide
]
可能的使用场景
切片器用于快速过滤数据。它们可用于过滤表格或数据透视表中的数据。 Microsoft Excel 允许您通过选择表格或数据透视表然后单击插入 > 切片器Aspose.Cells for Python via Java 提供Worksheet.getSlicers().add()方法创建切片器。
为数据透视表创建切片器
以下代码片段加载示例 Excel 文件包含数据透视表。然后它根据第一个基本数据透视字段创建切片器。最后,它将工作簿保存在输出 XLSX格式。以下屏幕截图显示了 Aspose.Cells 在输出 Excel 文件中创建的切片器。
示例代码
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
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import Workbook, SaveFormat | |
# Load Source Excel file | |
workbook = Workbook("sampleCreateSlicerToPivotTable.xlsx") | |
# Access first worksheet | |
worksheet = workbook.getWorksheets().get(0) | |
# Access first pivot table inside the worksheet | |
pivottable = worksheet.getPivotTables().get(0) | |
# Add slicer relating to pivot table with first base field at cell B22 | |
idx = worksheet.getSlicers().add(pivottable, "B22", pivottable.getBaseFields().get(0)) | |
# Access the newly added slicer from slicer collection | |
slicer = worksheet.getSlicers().get(idx) | |
# Save the workbook in output XLSX format | |
workbook.save("outputCreateSlicerToPivotTable.xlsx", SaveFormat.XLSX) | |
jpype.shutdownJVM() |