使用C#在Excel中创建图表

时间:2022-09-28 17:18:35

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:

我在使用C#在Excel中创建图表时遇到了一些麻烦。我已经设法获得了使用以下代码的任何类型的图表:

            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 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?

1)应该有几行数据,但它们并不相邻(例如A2:Y2; A4:Y4; A6:Y6;)。如何将其中的每一个添加到图表中?

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

2)A1:Y1具有我的图例的所有值,我将如何将其添加到图例中?

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

3)如何更改它以便在新选项卡上创建每个图表?

Thanks!

谢谢!

1 个解决方案

#1


4  

Just figured out answer to question 1:

刚刚想出问题1的答案:

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

Now figured out answer to question 3:

现在想出问题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.

最后问题2的答案是简单地添加行名作为选择的一部分,因此#2而不是A而不是B.

#1


4  

Just figured out answer to question 1:

刚刚想出问题1的答案:

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

Now figured out answer to question 3:

现在想出问题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.

最后问题2的答案是简单地添加行名作为选择的一部分,因此#2而不是A而不是B.