This quick tutorial guides you on how to insert a slicer in Excel using C#. It provides the reference to the environment settings, step-wise details to write the application, and a runnable sample code to add slicer in Excel using C#. In this tutorial, we will add a slicer for a table however you will also learn the options to add a slicer to different objects in a worksheet and save it as an XLSX, XLS, or any other MS Excel-supported format.
Steps to Insert a Slicer in Excel using C#
- Set the environment to use Aspose.Cells for .NET to add a slicer
- Load the source Excel file into the Workbook object having a table in it
- Get access to the target worksheet where the slicer is to be inserted
- Get a reference to the desired table in the selected worksheet
- Add the slicer by providing the target column and slicer destination cell
- Save the resultant Excel file with the slicer on the disk
These simple steps describe the procedure to add slicers in Excel using C# by providing configuration details and then a step-by-step approach to the write the program. First, you have to load the source Excel file and get access to the target worksheet and target table for adding the slicer. Once the reference to the target table is obtained, a new slicer is added to the slicers collection in the worksheet by providing the table name, target column for the cell, and new slicer destination cell.
Code to Insert Slicers in Excel using C#
This code demonstrates the process to insert a slicer using C# in an Excel file. It uses Worksheet.Slicers.Add() method to add the slicer for a table by providing the required parameters. You can also add a slicer for a pivot table by using the overload of the same method by providing the target pivot table, the destination cell where the slicer is to be added, and the base pivot field.
This article has taught us to insert slicers using C#. If you want to learn the process of filtering data in a table, refer to the article on how to filter data in Excel table using C#.