Articles in this section
Category / Section

How to create Pivot Table in Excel document using C#, VB.NET?

5 mins read

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 tablerefreshing a pivot tablepivot table filterspivot table settingspivot 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.


Conclusion

I hope you enjoyed learning about how to create Pivot Table in C#, VB.NET.

You can refer to our .NET Excel library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Excel library example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or  feedbackportal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied