Create a chart in Excel using C #

I'm having trouble creating charts in Excel using C #. I 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.ChartObject)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 quite sure what to do next. Here is what I want to do:

1) It is assumed that these are several rows of data, but they are not next to each other (for example, A2: Y2; A4: Y4; A6: Y6;). How to add each of them to the chart?

2) A1: Y1 has all the meanings for my legend, how would I add this to the legend?

3) How can I change it so that each chart is created in a new tab?

Thanks!

+5
source share
1 answer

Just found out the answer to question 1:

chartRange = xlWorkSheet.get_Range("B137:Y137, B139:Y139, B141:Y141", Missing.Value);

Now find out the answer to question 3:

chartPage.Location(Excel.XlChartLocation.xlLocationAsNewSheet, "Chart1");

, 2 , , # 2, A B.

+4

All Articles