如何在Excel中使用C#在图表中添加多个系列

如何在Excel中使用C#在图表中添加多个系列

本文介绍了如何在Excel中使用C#在图表中添加多个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加如下图所示的图表。

I would like to add a chart like the following picture.

这个图表有3个系列(黑色,红色,蓝色)。

This chart has 3 series (Black, Red, Blue).

以下是在图表上创建一个系列的代码块...

The following is a block of code that creates "one" series on a chart...

Excel._Workbook oWorkbook = (Excel._Workbook)oSheet.Parent;
Excel._Chart oChart = (Excel._Chart)oWorkbook.Charts.Add(oSheet, Type.Missing, Type.Missing, Type.Missing);

// Y axis data
Excel.Range oRange = oSheet.get_Range(yRange, Type.Missing);

// Creates a chart
oChart.ChartWizard(oRange, chartType, 2, Excel.XlRowCol.xlColumns, Type.Missing, Type.Missing, false, title, xAxisTitle, yAxisTitle, Type.Missing);

// Sets X axis category
Excel.Series oSeries = (Excel.Series)oChart.SeriesCollection(1);
oSeries.XValues = oSheet.get_Range(xRange, Type.Missing);
oChart.Name = chartName;

MSDN API不够有用,我几乎找不到任何有关这个问题的教程或示例。 (或者我可能不是很好地搜索它们)

如果有人给我一个解决方案,将不胜感激。

MSDN API is not helpful enough and I can hardly find any tutorial or example on this problem. (Or maybe I am not that good searching them)
It would be appreciated if someone gives me a solution.

推荐答案

我可以用非常简单的解决方案解决这个问题。

如果我设置yRange(oRange)右边,ChartWizard方法会自动创建图形。

具有A2:A100,A2:A100,C2:C100的范围将在一个图表上生成两行(系列),并且如果数据范围包括标题(或系列标签),ChartWizard系列名称。

I could solve this issue with very simple solution.
If I set the yRange (oRange) right, "ChartWizard" method automatically creates the graphs.
So instead of range having "A2:A100", "A2:A100,C2:C100" will generates two lines (series) on one chart and also if the data range includes the heading (or series label), the "ChartWizard" will automatically put the series name in the legend.

这篇关于如何在Excel中使用C#在图表中添加多个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:09