Easily Create an Excel Pivot Table in Just 3 Steps Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (178).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (223)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (225)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (936)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (162)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (156)Chart  (138)Chart of the week  (53)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (67)Development  (650)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (6)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (12)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (607)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Create an Excel Pivot Table in Just 3 Steps Using C#

Easily Create an Excel Pivot Table in Just 3 Steps Using C#

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 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#.

Enjoy a smooth experience with Syncfusion’s Excel Library! Get started with a few lines of code and without Microsoft or interop dependencies.

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 before proceeding.

  1. First, create a .NET Core console application in Visual Studio.Create a .NET Core Console Application
  2. Install the latest Syncfusion.XlsIO.Net.Core NuGet package in your app.Install Syncfusion.XlsIO.Net.Core NuGet
  3. Finally, add the following code to create a pivot table in a new worksheet(PivotSheet) in the existing Excel document.
    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

Input Excel document
Input Excel document
Creating a pivot table in an Excel document using Syncfusion .NET Excel Library and C#
Creating a pivot table in an Excel document using Syncfusion .NET Excel Library and C#

References

For more details, refer to creating pivot tables in Excel using C# documentation and GitHub demo.

Witness the possibilities in demos showcasing the robust features of Syncfusion’s C# Excel Library.

Conclusion

Thanks for reading! This blog explored creating a pivot table in an Excel document using C# and the Syncfusion Excel Library(XlsIO). The Excel Library also allows you to export Excel data to imagesdata tablesCSVTSVHTMLcollections of objectsODSJSON, and other file formats.

Take a moment to peruse the import data documentation, where you’ll discover additional importing options and features such as data tablescollection objectsgrid viewdata columns, and HTML, 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 page. If you are not a Syncfusion customer, try our 30-day free trial to check out our available features.

For questions, you can contact us through our support forumsupport portal, or feedback portal. We are always happy to assist you!

Don't settle for ordinary spreadsheet solutions. Switch to Syncfusion and upgrade the way you handle Excel files in your apps!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed
Syncfusion Ad