插入切片器
Contents
[
Hide
]
可能的使用场景
切片器用于快速过滤数据。它可用于在表格或数据透视表中过滤数据。Microsoft Excel允许您通过选择表格或数据透视表,然后单击插入 > 切片器 来创建切片器。Aspose.Cells也允许您使用Worksheet.Slicers.Add()方法创建切片器。
为数据透视表创建切片器
请参阅以下示例代码。它加载包含数据透视表的 示例 Excel 文件。然后基于第一个基本数据透视表字段创建切片器。最后,它以 输出 XLSX 和 输出 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-.NET | |
// Load sample Excel file containing pivot table. | |
Workbook wb = new Workbook("sampleCreateSlicerToPivotTable.xlsx"); | |
// Access first worksheet. | |
Worksheet ws = wb.Worksheets[0]; | |
// Access first pivot table inside the worksheet. | |
Aspose.Cells.Pivot.PivotTable pt = ws.PivotTables[0]; | |
// Add slicer relating to pivot table with first base field at cell B22. | |
int idx = ws.Slicers.Add(pt, "B22", pt.BaseFields[0]); | |
// Access the newly added slicer from slicer collection. | |
Aspose.Cells.Slicers.Slicer 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); |
为Excel表创建切片器
请查看以下示例代码。它加载包含数据表的sample Excel file,然后基于第一列创建切片器。最后,将工作簿保存为output XLSX格式。
示例代码
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-.NET | |
// Load sample Excel file containing a table. | |
Workbook workbook = new Workbook(sourceDir + "sampleCreateSlicerToExcelTable.xlsx"); | |
// Access first worksheet. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access first table inside the worksheet. | |
ListObject table = worksheet.ListObjects[0]; | |
// Add slicer | |
int idx = worksheet.Slicers.Add(table, 0, "H5"); | |
// Save the workbook in output XLSX format. | |
workbook.Save(outputDir + "outputCreateSlicerToExcelTable.xlsx", SaveFormat.Xlsx); |