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

可能的使用场景

切片器用于快速筛选数据。可以在表格或数据透视表中使用。Microsoft Excel 允许通过选择表格或数据透视表,然后点击 插入 > 切片器 来创建切片器。Aspose.Cells 也支持使用 Worksheet.getSlicers().add() 方法创建切片器。

为数据透视表创建切片器

请查看以下示例代码。它加载包含数据透视表的sample Excel file,然后基于第一个基本数据透视表字段创建切片器。最后,将工作簿保存为output XLSXoutput XLSB格式。以下截图显示了Aspose.Cells在输出Excel文件中创建的切片器。

todo:image_alt_text

示例代码

// 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);