C#/VB.NET: Create a Pie Chart in Excel

A pie chart is a circular chart for visually representation of data. It divides a circular statistical graph into sectors or slices and each sector represents a specific portion of the total percentage. In this article, you will learn how to programmatically create a pie chart in Excel using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Create a Pie Chart in Excel

The detailed steps are as follows:

  • Create a Workbook instance.
  • Get a specified worksheet using Workbook.Worksheets[sheetIndex] property.
  • Add some data to specified cells and set the cell styles and borders.
  • Add a pie chart to the worksheet using Worksheet.Charts.Add(ExcelChartType.Pie) method.
  • Set data range for the chart using Chart.DataRange property.
  • Set the position and title of the chart.
  • Get a specified series in the chart and set category labels and values for the series using ChartSerie.CategoryLabels and ChartSerie.Values properties.
  • Show data labels for data points by setting the ChartSerie.DataPoints.DefaultDataPoint.DataLabels.HasValue property as true.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Charts;
using System.Drawing;

namespace CreatePieChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Set sheet name
            sheet.Name = "Chart data";

            //Add data to specified cells
            sheet.Range["A1"].Value = "Year";
            sheet.Range["A2"].Value = "2002";
            sheet.Range["A3"].Value = "2003";
            sheet.Range["A4"].Value = "2004";
            sheet.Range["A5"].Value = "2005";

            sheet.Range["B1"].Value = "Sales";
            sheet.Range["B2"].NumberValue = 4000;
            sheet.Range["B3"].NumberValue = 6000;
            sheet.Range["B4"].NumberValue = 7000;
            sheet.Range["B5"].NumberValue = 8500;

            //Set cell styles
            sheet.Range["A1:B1"].Style.Font.IsBold = true;
            sheet.Range["A1:B1"].Style.KnownColor = ExcelColors.Black;
            sheet.Range["A1:B1"].Style.Font.Color = Color.White;
            sheet.Range["A1:B5"].Style.HorizontalAlignment = HorizontalAlignType.Center;
            sheet.Range["A1:B5"].Style.VerticalAlignment = VerticalAlignType.Center;

            //Set number format
            sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";

            //Set cell borders
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;

            //Add a pie chart to the worksheet
            Chart chart = sheet.Charts.Add(ExcelChartType.Pie);

            //Set data range for the chart
            chart.DataRange = sheet.Range["B2:B5"];
            chart.SeriesDataFromRange = false;

            //Set position of the chart
            chart.LeftColumn = 1;
            chart.TopRow = 7;
            chart.RightColumn = 9;
            chart.BottomRow = 28;

            //Set and format chart title
            chart.ChartTitle = "Sales by Year";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 14;

            // Get a specified series in the chart
            ChartSerie cs = chart.Series[0];

            //Set category labels for the series
            cs.CategoryLabels = sheet.Range["A2:A5"];

            //Set values for the series
            cs.Values = sheet.Range["B2:B5"];

            // Show data labels for data points
            cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;

            //Save the result file
            workbook.SaveToFile("PieChart.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Create a Pie Chart in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.