How to Create Excel Pie Chart in C#

In this topic, we will explain easy-to-use steps on how to create Excel Pie Chart in C# .NET application. The steps below exhibit how to create dynamic Pie chart in C# windows application programmatically.

Commonly, a Pie chart denotes categorical data whereas each pie slice can show specific category. In MS Excel, you may utilize rich set of chart tools. So, you can make Excel Pie chart in C# project on the fly. Next, you may save to Excel XLSX format. You can simply open the output XLSX file into some Excel viewer to display the graph.

Steps to Create Excel Pie Chart in C#

  1. Download Aspose.Cells for .NET package from NuGet Gallery
  2. Import relevant Aspose.Cells and Aspose.Cells.Charts namespaces with using directive
  3. Instantiate License object and set the license file using SetLicense method
  4. Create a Workbook object to create a new Excel workbook
  5. Get the first worksheet in the workbook and add data to the worksheet
  6. Add new Pie chart using ChartType.Pie enum member and set its data series with other attributes
  7. Save the output Excel file that contains the Pie chart in it

In below example, we will be creating a Pie chart dynamically based on quarterly sales data. The source data for the chart will also be pasted via code. Moreover, we set chart’s title and specify data labels properties to show values and percentages on the pie slices. We can use ChartType enumeration to select or set other similar types to draw charts.

Sample code to Create Excel Pie Chart in C#

The above code snippet inserts some data into the first worksheet cells and then creates an Excel Pie graph in C# based on that data source. Finally, it saves an Excel XLSX file which will contain the Pie chart. Moreover, you can convert Excel chart to JPG in C# without Interop as well.

 English