插入切片器
Contents
[
Hide
]
可能的使用场景
切片器用于快速过滤数据。它既可以用于表格,还可以用于数据透视表中的数据过滤。Microsoft Excel 允许您通过选择表格或数据透视表,然后单击 “插入 > 切片器” 来创建切片器。Aspose.Cells for Python via .NET 也允许您使用该 Worksheet.slicers.add() 方法来创建切片器。
如何使用 Aspose.Cells for Python Excel 库在数据透视表中创建切片器
请参阅以下示例代码。它加载包含数据透视表的 示例 Excel 文件。然后基于第一个基本数据透视表字段创建切片器。最后,它以 输出 XLSX 和 输出 XLSB 格式保存工作簿。以下屏幕截图显示了由 Aspose.Cells 在输出 Excel 文件中创建的切片器。
示例代码
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
from aspose.cells import SaveFormat, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load sample Excel file containing pivot table. | |
wb = Workbook("sampleCreateSlicerToPivotTable.xlsx") | |
# Access first worksheet. | |
ws = wb.worksheets[0] | |
# Access first pivot table inside the worksheet. | |
pt = ws.pivot_tables[0] | |
# Add slicer relating to pivot table with first base field at cell B22. | |
idx = ws.slicers.add(pt, "B22", pt.base_fields[0]) | |
# Access the newly added slicer from slicer collection. | |
slicer = ws.slicers[idx] | |
# Save the workbook in output XLSX format. | |
wb.save("outputCreateSlicerToPivotTable.xlsx", SaveFormat.XLSX) | |
# Save the workbook in output XLSB format. | |
wb.save("outputCreateSlicerToPivotTable.xlsb", SaveFormat.XLSB) |
如何使用 Aspose.Cells for Python Excel 库创建 Excel 表格的切片器
请查看以下示例代码。它加载包含数据表的sample Excel file,然后基于第一列创建切片器。最后,将工作簿保存为output XLSX格式。
示例代码
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
from aspose.cells import SaveFormat, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load sample Excel file containing a table. | |
workbook = Workbook(sourceDir + "sampleCreateSlicerToExcelTable.xlsx") | |
# Access first worksheet. | |
worksheet = workbook.worksheets[0] | |
# Access first table inside the worksheet. | |
table = worksheet.list_objects[0] | |
# Add slicer | |
idx = worksheet.slicers.add(table, 0, "H5") | |
# Save the workbook in output XLSX format. | |
workbook.save(outputDir + "outputCreateSlicerToExcelTable.xlsx", SaveFormat.XLSX) |