Excel charts are used to visualize the data in spreadsheets. MS Excel supports a variety of charts such as line, bar, pie, doughnut, pyramid, bubble, etc. In this article, you will learn how to create charts in Excel files using C#.
- C# API to Create Charts in Excel
- Create Charts in Excel using C#
- Create a Line Chart in Excel using C#
- Generate a Pyramid Chart in Excel using C#
- Get a Free API License
C# API to Create Charts in Excel
In order to work with Excel charts, we’ll use Aspose.Cells for .NET. It is a powerful API that lets you implement Excel automation in .NET applications. Furthermore, it allows you create a variety of charts seamlessly. To use the API, you can either download the DLL or install it using NuGet.
Install-Package Aspose.Cells
Supported Excel Chart Types
Aspose.Cells for .NET provides a complete set of standard chart types. The list includes, but not limited to:
Chart Types | Description |
---|---|
Column | Represents Clustered Column Chart |
ColumnStacked | Represents Stacked Column Chart |
Column100PercentStacked | Represents 100% Stacked Column Chart |
Column3DClustered | Represents 3D Clustered Column Chart |
Column3DStacked | Represents 3D Stacked Column Chart |
Column3D100PercentStacked | Represents 3D 100% Stacked Column Chart |
Column3D | Represents 3D Column Chart |
Bar | Represents Clustered Bar Chart |
BarStacked | Represents Stacked Bar Chart |
Bar100PercentStacked | Represents 100% Stacked Bar Chart |
Bar3DClustered | Represents 3D Clustered Bar Chart |
Bar3DStacked | Represents 3D Stacked Bar Chart |
Bar3D100PercentStacked | Represents 3D 100% Stacked Bar Chart |
Line | Represents Line Chart |
LineStacked | Represents Stacked Line Chart |
Line100PercentStacked | Represents 100% Stacked Line Chart |
LineWithDataMarkers | Represents Line Chart with data markers |
LineStackedWithDataMarkers | Represents Stacked Line Chart with data markers |
Line100PercentStackedWithDataMarkers | Represents 100% Stacked Line Chart with data markers |
Line3D | Represents 3D Line Chart |
Pie | Represents Pie Chart |
Pie3D | Represents 3D Pie Chart |
PiePie | Represents Pie of Pie Chart |
PieExploded | Represents Exploded Pie Chart |
Pie3DExploded | Represents 3D Exploded Pie Chart |
PieBar | Represents Bar of Pie Chart |
Scatter | Represents Scatter Chart |
ScatterConnectedByCurvesWithDataMarker | Represents Scatter Chart connected by curves, with data markers |
For a complete list of supported Excel charts, visit this article.
Create Charts in Excel using C#
The following are the steps to create a chart in Excel using C#.
- First, create a new Excel workbook or load an existing one using Workbook class.
- Access the desired worksheet into a Worksheet object.
- Insert data into the worksheet (if worksheet is empty).
- Create a chart in the worksheet using Worksheet.Charts.Add(ChartType type, int upperLeftRow, int upperLeftColumn, int lowerRightRow, int lowerRightColumn) method.
- Access the chart by its index into a Chart object.
- Set data source for the chart using Chart.SetChartDataRange(“A1:C4”, true) method.
- Finally, save the workbook using Workbook.Save(string) method.
The following code sample shows how to create an Excel chart using C#.
Create a Line Chart in Excel using C#
In order to insert a line chart, you only need to specify the ChartType.Line type in Worksheet.Charts.Add() method. The rest of the steps will be the same as mentioned in the previous section.
- First, create an Excel workbook using the Workbook class.
- Access the desired worksheet into a Worksheet object.
- Insert data into the worksheet.
- Create the line chart using Worksheet.Charts.Add() method.
- Access the chart by its index into a Chart object.
- Set data source for the chart using Chart.SetChartDataRange(“A1:C4”, true) method.
- Finally, ave the workbook using Workbook.Save(string) method.
The following code sample shows how to create a line chart in Excel using C#.
Create a Pyramid Chart in Excel using C#
For adding a pyramid chart, simply pass ChartType.Pyramid type while adding the chart to the worksheet. The following are the steps to add a pyramid chart in Excel using C#.
- First, create an Excel workbook using the Workbook class.
- Access the desired worksheet into a Worksheet object.
- Insert data into the worksheet.
- Create the pyramid chart using Worksheet.Charts.Add() method.
- Access the chart by its index into a Chart object.
- Set data source for the chart using Chart.SetChartDataRange(“A1:C4”, true) method.
- Finally, save the workbook using Workbook.Save(string) method.
The following code sample shows how to insert a pyramid chart in an Excel worksheet using C#.
To learn more about working with Excel charts, read this documentation article.
Get a Free API License
You can try Aspose.Cells for .NET without evaluation limitations by requesting a temporary license.
Conclusion
In this article, you have seen how to create charts in Excel worksheets using C#. Particularly, you have learned how to create column, line, and pyramid charts in Excel. Furthermore, you can also create other types of charts seamlessly using Aspose.Cells for .NET. For more details, visit the documentation of the API. In case you would have any queries, contact us via our forum.