# How to export excel chart from C#

The following C# program shows how to Export a Chart from Excel file to a Picture file like , BMP , JPG , GIF etc .

The following C# source code using Microsoft Excel 12.0 Object Library .In the previous section we saw how to import Microsoft Excel 12.0 Object Library in the C# project .

Before we crating a Chart , we have to fill data in Excel sheet. Here we are filling the data in excel sheet through CSharp program. After fill the data in your Excel sheet its look like the following picture. After fill the data in excel file, We have to create a chart object in CSharp and configure the Chart object with necessary data like positions , size , data range , chart type etc.. and use the command for export chart as picture file .

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

The following picture shows the BMP Chart file export from Excel. 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("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-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(); } } } }
{"ft_lang_label":"__label__en","ft_lang_prob":0.59519124,"math_prob":0.7758428,"size":3341,"snap":"2023-14-2023-23","text_gpt3_token_len":924,"char_repetition_ratio":0.21756068,"word_repetition_ratio":0.01,"special_character_ratio":0.27476802,"punctuation_ratio":0.2961832,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97581553,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,5,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-04T08:35:50Z\",\"WARC-Record-ID\":\"<urn:uuid:6515b4c7-8301-42a3-9d62-920beb2a90c4>\",\"Content-Length\":\"18762\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f8de4c91-f9e7-482c-9baf-9ea9e53503fb>\",\"WARC-Concurrent-To\":\"<urn:uuid:78f94dcb-efe1-4341-b98d-f65fdea6375f>\",\"WARC-IP-Address\":\"107.180.46.197\",\"WARC-Target-URI\":\"http://csharp.net-informations.com/excel/csharp-excel-chart-export.htm\",\"WARC-Payload-Digest\":\"sha1:PFYI623BG7ECWCF5ZAIX7SFJ73DYCUVG\",\"WARC-Block-Digest\":\"sha1:DLZDU2LLLLVLHT4X7ZWDV7UFCFNHVK66\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224649518.12_warc_CC-MAIN-20230604061300-20230604091300-00459.warc.gz\"}"}