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?
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
精彩评论