Learn how to How to add, update, and edit slicers in Excel using C#
Created
November 6, 2025 13:35
-
-
Save aspose-com-gists/fe923aef491af5b447331d60eb45192c to your computer and use it in GitHub Desktop.
How to add, update, and edit slicers in Excel using C#
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
| // Load sample Excel file containing a table. | |
| Workbook workbook = new Workbook("SampleTable.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("outputCreateSlicerToExcelTable.xlsx", SaveFormat.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
| // Load sample Excel file containing slicer. | |
| Workbook wb = new Workbook("InputSlicer.xlsx"); | |
| // Access first worksheet. | |
| Worksheet ws = wb.Worksheets[0]; | |
| // Access the first slicer inside the slicer collection. | |
| Slicer slicer = ws.Slicers[0]; | |
| // Set the number of columns of the slicer. | |
| slicer.NumberOfColumns = 2; | |
| // Set the type of slicer style. | |
| slicer.StyleType = SlicerStyleType.SlicerStyleLight6; | |
| // Save the workbook in output XLSX format. | |
| wb.Save("outputFormattingSlicer.xlsx", SaveFormat.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
| // Load sample Excel file containing pivot table. | |
| Workbook wb = new Workbook("SamplePivotTable.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, "B12", 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); |
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
| // Load sample Excel file containing slicer. | |
| Workbook wb = new Workbook("InputSlicer.xlsx"); | |
| // Access first worksheet. | |
| Worksheet ws = wb.Worksheets[0]; | |
| // Access the first slicer inside the slicer collection. | |
| Slicer slicer = ws.Slicers[0]; | |
| // Access the slicer items. | |
| SlicerCacheItemCollection scItems = slicer.SlicerCache.SlicerCacheItems; | |
| // Loop through slicer items and update selection. | |
| foreach (SlicerCacheItem item in scItems) | |
| { | |
| if (item.Value == "Pink" || item.Value == "Green") | |
| { | |
| item.Selected = false; | |
| } | |
| } | |
| // Refresh slicer to apply changes. | |
| slicer.Refresh(); | |
| // Save the updated workbook. | |
| wb.Save("outputUpdatingSlicer.xlsx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment