How to export excel chart from C#

Shown below is C# program which depicts the process of creating an Excel chart and exporting it to a popular picture file formats such as JPG, BMP, GIF, and others. This code fragment involves using the Microsoft Excel 12.0 Object Library and depicts the necessary stages ensuring the fields containing the chart data are converted into a picture file format.

In the previous section, we discussed the procedure for importing the Microsoft Excel 12.0 Object Library into our C# project, thereby facilitating seamless integration of Excel-related functionalities. By utilizing the presented C# source code, developers can successfully export charts from Excel files to picture files, offering enhanced flexibility and compatibility for further use and distribution

Create Excel file from CSharp

In preparation for creating a chart, it is essential to populate the Excel sheet with the required data. To accomplish this, we employ a C# program to fill the Excel sheet with the necessary information. Once the data has been successfully populated in the Excel sheet, it will assume the appearance depicted in the accompanying picture.


After populating the data in the Excel file, the subsequent step involves creating a chart object in C# and configuring it with the essential information, including positions, size, data range, chart type, and more. Once the chart object is configured, you can utilize the appropriate command to export the chart as a picture file. This process allows for the seamless conversion of the chart into a visual representation that can be saved and utilized independently in various formats, such as BMP, JPG, GIF, and others. By implementing these steps, you can effectively create and export a visually appealing chart from the Excel file, enhancing its accessibility and enabling its utilization in a wider range of applications.

chartPage.Export(@"C:\excel_chart_export.bmp","BMP",null );

The following picture shows the BMP Chart file export from Excel.

csharp-excel-chart-export 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; //export chart as picture file chartPage.Export(@"C:\excel_chart_export.bmp","BMP",misValue ); xlWorkBook.SaveAs("", 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-Excel.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(); } } } }