Create Pivot Chart in Excel in C#

Starting from version 9.8.5, Spire.XLS supports creating pivot chart based on pivot table. This article is going to demonstrate how we can use Spire.XLS to implement this feature.

The input.xlsx Excel file:

Create Pivot Chart in Excel in C#

Sample Code

using Spire.Xls;
using Spire.Xls.Core;

namespace CreatePivotChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx");

            //get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //get the first pivot table in the worksheet
            IPivotTable pivotTable = sheet.PivotTables[0];

            //create a clustered column chart based on the pivot table
            Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered, pivotTable);
            //set chart position
            chart.TopRow = 19;
            chart.BottomRow = 38;
            //set chart title
            chart.ChartTitle = "Pivot Chart";

            //save the resultant file 
            workbook.SaveToFile("CreatPivotChart.xlsx", ExcelVersion.Version2013);
        }
    }
}

Screenshot of the created pivot chart:

Create Pivot Chart in Excel in C#