Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a graph from an excel worksheet using C#

I am writing code that writes some data to an Excel file using C# (using Office.InterOp.Excel). Now I have to select two columns out of the 10 in the worksheet and plot a graph between the two. This has to be done using only C#.

Any ideas? Most examples I found on the net are for fixed data values. What if the data values are not known before hand?

like image 963
user_2011 Avatar asked Jan 19 '23 13:01

user_2011


1 Answers

You can use ChartObjects class.

For example;

Microsoft.Office.Interop.Excel.Range chartRange ;  
Microsoft.Office.Interop.Excel.ChartObjects xlCharts = 
    (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Microsoft.Office.Interop.Excel.ChartObject myChart = 
    (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart;

chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

Source: http://csharp.net-informations.com/excel/csharp-excel-chart.htm

like image 81
Soner Gönül Avatar answered Jan 27 '23 23:01

Soner Gönül