创建到数据透视表的切片器

可能的使用场景

分层切片器用于快速过滤数据。它们可用于在表或数据透视表中过滤数据。Microsoft Excel允许您通过选择表或数据透视表然后单击插入 > 分层切片器 来创建一个分层切片器。Aspose.Cells for Python via Java提供了Worksheet.getSlicers().add()方法来创建分层切片器。

为数据透视表创建切片器

以下代码段加载包含数据透视表的样本Excel文件,然后根据第一个基本数据透视字段创建分层切片器。最后,将工作簿保存为输出XLSX格式。以下屏幕截图显示了Aspose.Cells在输出Excel文件中创建的分层切片器。

todo:image_alt_text

示例代码

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()