如何使用c#计算excel中图表中系列的总数?

时间:2016-08-26 07:01:01

标签: c# excel charts

我在工作表中创建了一个图表,现​​在我想使用以下代码设置系列的格式:

for (int i = 1; i <= ***number of series***; i++)
{
    set_Series_Format(Excelbook, Chart_ScatterSmooth.SeriesCollection(i));
}

我必须得到图表中系列总数的计数,但到目前为止我还没有任何解决方案。

2 个答案:

答案 0 :(得分:0)

我发现添加Series后,我可以查询SeriesCollection.Count属性:

 Excel.SeriesCollection  sc = myChart.Chart.SeriesCollection();
 int SeriesCount = sc.Count;

奇怪的是,在我之后添加0Series之前,它会报告2。因此,'集合'可能只是一个只有默认Series的集合。 SeriesCollection() and its docs {{3}}  建议这样的事情,但我真的不确定:

  

获取单个系列(T:Microsoft.Office.Interop.Excel.Series   对象)或所有系列的集合(a   T:Microsoft.Office.Interop.Excel.SeriesCollection集合中的   图表或图表组

答案 1 :(得分:0)

问题很老,但有人可能会喜欢答案。

// Get the chart object
Excel.ChartObject xlChartObject = xlSheet.ChartObjects("Chart 1");
// Get the chart from the chart object
Excel.Chart xlChart = xlChartObject.Chart;
// Get the full series collection from the chart
Excel.FullSeriesCollection fullSeriesCollection = xlChart.FullSeriesCollection();

// Returns the count of series from the chart
int SeriesCount = fullSeriesCollection.Count;

fullSeriesCollection.Count 将在图表 xlChart 中返回该系列。 可以在这里FullSeriesCollection Interface

找到该文档。

FullSeriesCollection()的优点之一是它包含一个枚举器,使您可以遍历该系列:

foreach (Excel.Series xlSeries in xlChart.FullSeriesCollection())
{
    xlSeries.MarkerStyle = Excel.XlMarkerStyle.xlMarkerStyleCircle;
}

希望这会有所帮助。