How to create Excel Chart from C#

In C#, it is possible to dynamically generate charts in Excel by using the capabilities of the Microsoft Excel 12.0 Object Library. The forthcoming section will provide a comprehensive demonstration of how to create a chart in Excel using C# source code.

The C# source code snippet presented below relies on the Microsoft Excel 12.0 Object Library, which we imported in the previous section of this guide, ensuring seamless integration within our C# project. By implementing this code, we can utilizes the power of Excel's charting features to create visually appealing and data-driven charts directly from our C# application.

Create Excel file from CSharp

Before creating a chart in Excel, it is necessary to populate the Excel sheet with data. In the following section, we will demonstrate how to accomplish this by programmatically filling the Excel sheet using a C# program. Once the data is successfully filled in the Excel sheet, it will resemble the visual representation depicted in the accompanying picture.

csharp-excel-chart-data

Once the data has been populated in the Excel sheet, the subsequent step involves creating a chart object in C# and customizing it with essential information such as positions, size, data range, chart type, and more. This can be accomplished by utilizing the Excel Interop library and the Microsoft Excel 12.0 Object Library within your C# project. By following the provided example, you can effectively generate a chart object and seamlessly tailor its properties to meet your specific requirements, enabling the visual representation of the data to be accurately presented in the desired manner.

The following picture shows the excel file after created a chart.

csharp-excel-create-chart Full Source C#
using System; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //add data xlWorkSheet.Cells[1, 1] = ""; xlWorkSheet.Cells[1, 2] = "Student1"; xlWorkSheet.Cells[1, 3] = "Student2"; xlWorkSheet.Cells[1, 4] = "Student3"; xlWorkSheet.Cells[2, 1] = "Term1"; xlWorkSheet.Cells[2, 2] = "80"; xlWorkSheet.Cells[2, 3] = "65"; xlWorkSheet.Cells[2, 4] = "45"; xlWorkSheet.Cells[3, 1] = "Term2"; xlWorkSheet.Cells[3, 2] = "78"; xlWorkSheet.Cells[3, 3] = "72"; xlWorkSheet.Cells[3, 4] = "60"; xlWorkSheet.Cells[4, 1] = "Term3"; xlWorkSheet.Cells[4, 2] = "82"; xlWorkSheet.Cells[4, 3] = "80"; xlWorkSheet.Cells[4, 4] = "65"; xlWorkSheet.Cells[5, 1] = "Term4"; xlWorkSheet.Cells[5, 2] = "75"; xlWorkSheet.Cells[5, 3] = "82"; xlWorkSheet.Cells[5, 4] = "68"; Excel.Range chartRange ; Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250); Excel.Chart chartPage = myChart.Chart; chartRange = xlWorkSheet.get_Range("A1", "d5"); chartPage.SetSourceData(chartRange, misValue); chartPage.ChartType = Excel.XlChartType.xlColumnClustered; xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls"); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } } }