Easily Create an Excel Pivot Table in Just 3 Steps Using C#
Tags: excel, csharp, documentprocessing, fileformats
Published: 2024-05-10 04:18:41

---
title: Easily Create an Excel Pivot Table in Just 3 Steps Using C#
published: true
date: 2024-05-07 13:11:08 UTC
tags: excel, csharp, documentprocessing, fileformats
canonical_url: https://www.syncfusion.com/blogs/post/create-pivot-table-in-excel-csharp
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8cjj1mt23ilkztwb9bb.png
---
**TL;DR:** Syncfusion Excel Library is the perfect tool for all kinds of Excel creation, reading, editing, and viewing functionalities. Let’s learn how to create pivot tables in an Excel document using this robust library with C#.

A pivot table is an extraordinary feature in Excel that allows users to summarize and analyze large datasets quickly. It allows the users to create dynamic pivot views by grouping only required fields in the Excel data.

The [Syncfusion Excel Library](https://www.syncfusion.com/document-processing/excel-framework/net "Syncfusion Excel Library") is also known as Essential XlsIO. It facilitates the smooth creation, reading, and editing of Excel documents using C#. It supports the creation of Excel documents from scratch, modification of existing Excel documents, data import and export, Excel formulas, conditional formats, data validations, charts, sparklines, tables, pivot tables, pivot charts, template markers, and much more.

In this blog, we’ll explore the steps to create a pivot table using Syncfusion Excel Library in C#.

## Creating a pivot table in Excel using C#

Follow these steps to create a pivot table using the Syncfusion Excel Library and C#:

**Note:** Please refer to the [.NET Excel Library’s getting started documentation](https://help.syncfusion.com/file-formats/xlsio/create-excel-file-csharp-vbnet "Create Excel File in C# and VB.NET") before proceeding.

1.First, create a .[NET Core console application](https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio?pivots=dotnet-8-0 "Create a .NET console application using Visual Studio") in [Visual Studio](https://visualstudio.microsoft.com/vs/ "Visual Studio"). ![Create a .NET Core Console Application](https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Create-a-.NET-Core-Console-Application.png)
2.Install the latest [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core "Syncfusion.XlsIO.Net.Core NuGet package") NuGet package in your app. ![Install Syncfusion.XlsIO.Net.Core NuGet](https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Install-Syncfusion.XlsIO_.Net_.Core-NuGet.png)
3.Finally, add the following code to create a pivot table in a new worksheet(PivotSheet) in the existing Excel document.
```csharp
using Syncfusion.XlsIO; 

namespace PivotTable

{

    class Program

    {

        public static void Main()

        {

            using (ExcelEngine excelEngine = new ExcelEngine())

            {

                IApplication application = excelEngine.Excel;

                FileStream fileStream = new FileStream("../../../Data/SalesReport.xlsx", FileMode.Open, FileAccess.Read);

                IWorkbook workbook = application.Workbooks.Open(fileStream);

                IWorksheet worksheet = workbook.Worksheets[0]; 

                IWorksheet pivotSheet = workbook.Worksheets.Create("PivotSheet"); 

                //Create a Pivot cache with the given data range.

                IPivotCache cache = workbook.PivotCaches.Add(worksheet["A1:H50"]); 

                //Create "PivotTable1" with the cache at the specified range.

                IPivotTable pivotTable = pivotSheet.PivotTables.Add("PivotTable1", pivotSheet["A1"], cache); 

                //Add Pivot table row fields.             

                pivotTable.Fields[3].Axis = PivotAxisTypes.Row;

                pivotTable.Fields[4].Axis = PivotAxisTypes.Row; 

                //Add Pivot table column fields.

                ivotable.Fields[2].Axis = PivotAxisTypes.Column; 

                //Add data fields.

                IPivotField field = pivotTable.Fields[5];

                pivotTable.DataFields.Add(field, "Units", PivotSubtotalTypes.Sum); 

                field = pivotTable.Fields[6];

                pivotTable.DataFields.Add(field, "Unit Cost", PivotSubtotalTypes.Sum); 

                //Pivot table style.

                pivotTable.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium14;           

                string fileName = "PivotTable.xlsx";

                //Saving the workbook as a stream.

                FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);

                workbook.SaveAs(stream);

                stream.Dispose();

            }

        }

    }

}
```

Refer to the following images
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Input-Excel-document.png" alt="Input Excel document" style="width:100%">

  <figcaption>Input Excel document</figcaption>

</figure>
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/05/Creating-a-pivot-table-in-an-Excel-document-using-Syncfusion-.NET-Excel-Library-and-C.png" alt="Creating a pivot table in an Excel document using Syncfusion .NET Excel Library and C#" style="width:100%">

  <figcaption>Creating a pivot table in an Excel document using Syncfusion .NET Excel Library and C#</figcaption>

</figure>

## References

For more details, refer to [creating pivot tables in Excel using C# documentation](https://help.syncfusion.com/file-formats/xlsio/working-with-pivot-tables "Working with Pivot Tables in Excel using C#") and [GitHub demo](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Pivot%20Table/PivotTable "Create Pivot Table in Excel using C# GitHub demo").

## Conclusion

Thanks for reading! This blog explored creating a pivot table in an Excel document using C# and the [Syncfusion Excel Library](https://www.syncfusion.com/excel-framework/net "Syncfusion Excel Library")**(XlsIO)**. The Excel Library also allows you to export Excel data to [images](https://help.syncfusion.com/file-formats/xlsio/worksheet-to-image-conversion "Worksheet to Image conversion"), [data tables](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-data-table "Exporting from Worksheet to Data Table"), [CSV](https://www.syncfusion.com/kb/9092/convert-excel-file-to-csv-in-c-vb-net "Convert Excel file to CSV"), [TSV](https://www.syncfusion.com/kb/8556/does-xlsio-support-tsv-files "Excel to TSV conversion"), [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet#save-worksheet-as-html "Save Worksheet as HTML"), [collections of objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-clr-objects "Working with Excel Data"), [ODS](https://help.syncfusion.com/file-formats/xlsio/excel-to-ods-conversion "Excel to ODS Conversion"), [JSON](https://help.syncfusion.com/file-formats/xlsio/excel-to-json-conversion "Excel to JSON Conversion"), and other file formats.

Take a moment to peruse the [import data](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-data-to-worksheets "Importing Data to Worksheets") documentation, where you’ll discover additional importing options and features such as [data tables](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datatable "Import Data from DataTable"), [collection objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-collection-objects "Import Data from Collection Objects"), [grid view](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-dataview "Import Data from DataView"), [data columns](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datacolumn "Importing Data from DataColumn"), and [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-html-table-to-excel-worksheet "Importing HTML Table to Excel Worksheet"), all accompanied by code samples.

Feel free to try out these features and share your feedback in the comments section of this blog post!

For existing customers, the new version of Essential Studio is available for download from the [License and Downloads](https://www.syncfusion.com/account/downloads "Essential Studio License and Downloads page") page. If you are not a Syncfusion customer, try our 30-day [free trial](https://www.syncfusion.com/downloads "Get the free 30-day evaluation of Essential Studio products") to check out our available features.

For questions, you can contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/ "Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [Create Excel table in just 3 steps using C#](https://www.syncfusion.com/blogs/post/create-excel-table-in-3-steps-csharp "Blog: Create Excel table in just 3 steps using C#")
- [Print Excel Documents in Just 4 Steps Using C#](https://www.syncfusion.com/blogs/post/print-excel-in-csharp.aspx "Blog: Print Excel Documents in Just 4 Steps Using C#")
- [Converting XLS to XLSX Format in Just 3 Steps Using C#](https://www.syncfusion.com/blogs/post/convert-xls-to-xlsx-csharp.aspx "Blog: Converting XLS to XLSX Format in Just 3 Steps Using C#")
- [Merge Multiple Excel Files into One in Just 3 Steps Using C#](https://www.syncfusion.com/blogs/post/merge-multiple-excel-csharp.aspx "Blog: Merge Multiple Excel Files into One in Just 3 Steps Using C#")
- [6 Easy Ways to Export Data to Excel in C#](https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx "Blog: 6 Easy Ways to Export Data to Excel in C#")
- [Seamlessly Import and Export CSV Data in Excel Using C#](https://www.syncfusion.com/blogs/post/import-export-csv-data-in-excel-csharp.aspx "Blog: Seamlessly Import and Export CSV Data in Excel Using C#")
- [Easy Steps to Export HTML Tables to Excel in C#](https://www.syncfusion.com/blogs/post/easy-steps-to-export-html-tables-to-an-excel-worksheet-in-c.aspx "Blog: Easy Steps to Export HTML Tables to Excel in C#")