创建到数据透视表的切片器
Contents
[
Hide
]
可能的使用场景
切片器可用于快速过滤数据。它可以用于表格或数据透视表中的数据过滤。Microsoft Excel允许您通过选择表格或数据透视表,然后点击插入 > 切片器来创建切片器。Aspose.Cells也允许您使用Worksheet.getSlicers().add()方法创建切片器。
为数据透视表创建切片器
请查看以下示例代码。它加载包含数据透视表的sample Excel file,然后基于第一个基本数据透视表字段创建切片器。最后,将工作簿保存为output XLSX和output XLSB格式。以下截图显示了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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Load sample Excel file containing pivot table. | |
Workbook wb = new Workbook(srcDir + "sampleCreateSlicerToPivotTable.xlsx"); | |
// Access first worksheet. | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Access first pivot table inside the worksheet. | |
PivotTable pt = ws.getPivotTables().get(0); | |
// Add slicer relating to pivot table with first base field at cell B22. | |
int idx = ws.getSlicers().add(pt, "B22", pt.getBaseFields().get(0)); | |
// Access the newly added slicer from slicer collection. | |
Slicer slicer = ws.getSlicers().get(idx); | |
// Save the workbook in output XLSX format. | |
wb.save(outDir + "outputCreateSlicerToPivotTable.xlsx", SaveFormat.XLSX); | |
// Save the workbook in output XLSB format. | |
wb.save(outDir + "outputCreateSlicerToPivotTable.xlsb", SaveFormat.XLSB); |