Create a chart in Excel using C#
I'm having some trouble creating charts in Excel with C#. I've managed to get a chart of any kind working with the following code:
Excel.Range chartRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.Char开发者_JS百科tObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A2", "Y2");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
Unfortunately, I'm not really sure what to do next. Here is what I want to do:
1) There are supposed to be several rows of data, but they are not next to each other (E.g. A2:Y2; A4:Y4; A6:Y6;). How do I add each of these to the chart?
2) A1:Y1 has all of the values for my legend, how would I add this in to the legend?
3) How can I change it so that each chart is created on a new tab?
Thanks!
Just figured out answer to question 1:
chartRange = xlWorkSheet.get_Range("B137:Y137, B139:Y139, B141:Y141", Missing.Value);
Now figured out answer to question 3:
chartPage.Location(Excel.XlChartLocation.xlLocationAsNewSheet, "Chart1");
And finally the answer to Question 2 was to simply add the row names as part of the selection, so as #2 but A instead of B.
精彩评论