)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Create Pivot Table in C#, VB.NET

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: January 22, 2020

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can create a Pivot Table in Excel document.

What is a Pivot Table?

Pivot Table is a program tool that allows you to reorganize and summarize the data in a spreadsheet or database table to obtain a desired report. It is used to build a list of unique values and a good way to quickly see all the values that appear in the field.

Syncfusion Essential XlsIO represents the Pivot Table in Excel worksheet through IPivotTable interface.

This sample explains the creation of a Pivot Table using the following data.

Data for creating pivot table

Data for creating Pivot Table

Steps to add Pivot Table in Excel, programmatically:

  1. Create a new C# console application project.

Create a new C# console application project

Create a new C# console application project

  1. Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package to the project

Install NuGet package to the project

  1. Include the following namespace in Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

  1. Include the following code snippet in main method of Program.cs file to add Pivot Table in Excel document.

C#

//Create a new ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate a new Excel application object
    IApplication application = excelEngine.Excel;
 
    //Set the default appliction version
    application.DefaultVersion = ExcelVersion.Excel2016;
 
    //Load an existing Excel workbook into IWorkbook
    IWorkbook workbook = application.Workbooks.Open("../../PivotData.xlsx");
 
    //Get the first worksheet in the workbook into IWorksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Get the second worksheet in the workbook into IWorksheet
    IWorksheet pivotSheet = workbook.Worksheets[1];
 
    //Create pivot cache with the data range
    IPivotCache pivotCache = workbook.PivotCaches.Add(worksheet.Range["A1:H50"]);
 
    //Create PivotTable with the cache at the specified range
    IPivotTable pivotTable = pivotSheet.PivotTables.Add("PivotTable", pivotSheet.Range["A1"], pivotCache);
 
    //Add pivot table fields
    pivotTable.Fields[2].Axis = PivotAxisTypes.Row;
    pivotTable.Fields[6].Axis = PivotAxisTypes.Row;
    pivotTable.Fields[3].Axis = PivotAxisTypes.Column;
    pivotTable.Fields[4].Axis = PivotAxisTypes.Page;
 
    //Add data field
    IPivotField pivotField = pivotTable.Fields[5];
    pivotTable.DataFields.Add(pivotField, "Sum", PivotSubtotalTypes.Sum);
 
    //Create a PivotFilter object to apply filter to page fields
    IPivotFilter pivotFilter = pivotTable.Fields[4].PivotFilters.Add();
 
    //Assign pivot filter value as "Binder"
    pivotFilter.Value1 = "Binder";
 
    //Apply built in style for pivot table
    pivotTable.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium2;
 
    //Activate the pivot sheet
    pivotSheet.Activate();
 
    //Save the Excel document
    workbook.SaveAs("PivotTable.xlsx");
}

 

VB.NET

'Create a new ExcelEngine
Using excelEngine As ExcelEngine = New ExcelEngine()
 
    'Instantiate the Excel application object
    Dim application As IApplication = excelEngine.Excel
 
    'Set the default appliction version
    application.DefaultVersion = ExcelVersion.Excel2016
 
    'Load an existing Excel workbook into IWorkbook
    Dim workbook As IWorkbook = application.Workbooks.Open("../../PivotData.xlsx")
 
    'Get the first worksheet in the workbook into IWorksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Get the second worksheet in the workbook into IWorksheet
    Dim pivotSheet As IWorksheet = workbook.Worksheets(1)
 
    'Create pivot cache with the data range
    Dim pivotCache As IPivotCache = workbook.PivotCaches.Add(worksheet.Range("A1:H50"))
 
    'Create PivotTable with the cache at the specified range
    Dim pivotTable As IPivotTable = pivotSheet.PivotTables.Add("PivotTable", pivotSheet.Range("A1"), pivotCache)
 
    'Add pivot table fields
    pivotTable.Fields(2).Axis = PivotAxisTypes.Row
    pivotTable.Fields(6).Axis = PivotAxisTypes.Row
    pivotTable.Fields(3).Axis = PivotAxisTypes.Column
    pivotTable.Fields(4).Axis = PivotAxisTypes.Page
 
    'Add data field
    Dim pivotField As IPivotField = pivotTable.Fields(5)
    pivotTable.DataFields.Add(pivotField, "Sum", PivotSubtotalTypes.Sum)
 
    'Create a PivotFilter object to apply filter to page fields
    Dim pivotFilter As IPivotFilter = pivotTable.Fields(4).PivotFilters.Add()
 
    'Assign pivot filter value as "Binder"
    pivotFilter.Value1 = "Binder"
 
    'Apply built in style for pivot table
    pivotTable.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium2
 
    'Activate the Pivot Sheet
    pivotSheet.Activate()
 
    'Save the Excel document
    workbook.SaveAs("PivotTable.xlsx")
End Using

 

A complete working sample to create a Pivot Table in Excel document can be downloaded from Create-PivotTable.zip.

By executing the program, you will get the output Excel file as follows.

Create a Pivot Table

Output Excel document

Take a moment to peruse the documentation where you will find other options like editing and formatting a pivot table, refreshing a pivot table, pivot table filters, pivot table settings, pivot table sorting and more with code examples.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

Sort Pivot Table fields using Syncfusion Excel library

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon