使用c#创建多个Excel图表对象 [英] Creating multiple Excel chart objects using c#

查看:204
本文介绍了使用c#创建多个Excel图表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时,试图从我的C#应用​​程序在Excel中创建几个图表。我试图创建多个图表对象。有没有更好的方法这样做?我确定行chartObject [col] =(Excel.Chart)oWB.Charts.Add(Missing.Value,Missing.Value,Missing.Value,Missing.Value);是我错了的地方。

I've spent hours now trying to create a few charts in Excel from my C# application. I'm trying to create more than one chart object. Is there a better way of doing this? I'm sure the line "chartObject[col] = (Excel.Chart)oWB.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);" is where I'm going wrong.

目前,当我调用此行时,有时会创建最后一个图表的副本,但有时它会工作。我完全不明白它的逻辑。

At the moment, when I call this line, it sometimes creates a a copy of the last chart, but sometimes it works. I can't understand the logic to it at all.

感谢

private void CreateCharts(Excel.Worksheet oWS, int numRows, int numCols)
    {
        Excel.Workbook oWB = (Excel.Workbook)oWS.Parent;
        Excel.Series oSeries;
        //Excel._Chart chartObject;
        Excel.Chart[] chartObject = new Excel.Chart[numCols];
        Excel.SeriesCollection[] oSeriesCollection = new Excel.SeriesCollection[numCols];
        int length = numRows + 2;
        string colname;

        //then you can assign as much as series you want,
        for (int col = 0; col < numCols; col++)
        {
            //create a new chart
            chartObject[col] = (Excel.Chart)oWB.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            chartObject[col].ChartType = Excel.XlChartType.xlLine;
            oSeriesCollection[col] = (Excel.SeriesCollection)chartObject[col].SeriesCollection();

            //add the actual occupancy
            colname = GetExcelColumnName(col * 3 + 1);
            oSeries = oSeriesCollection[col].NewSeries();
            oSeries.Values = oWS.Range[colname + "2", colname + length];

            //add the expected occupancy
            colname = GetExcelColumnName(col * 3 + 2);
            oSeries = oSeriesCollection[col].NewSeries();
            oSeries.Values = oWS.Range[colname + "2", colname + length];
        }
    }


推荐答案

问题我的是,在Excel中的图表对象实际上是一个图表表,而图表本身是一个 ChartObject 对象,并使用形状对象它在处理它。以下是链接另一个,说明它是一个abit,以及来自此MS链接请注意,有几种不同的方法可以完成:

The problem my be that the Chart object in Excel is really a chart sheet, while a chart itself is a ChartObject object, and you use the Shape object it's in to handle it. Here's a link, and another one that talke about it a abit, and some VBA code from this MS link that shows a little of it, note that there are a few different ways to get it done:

Sub AddChart_Excel()   
  Dim objShape As Shape   

  ' Create a chart and return a Shape object reference.   
  ' The Shape object reference contains the chart.   
  Set objShape = ActiveSheet.Shapes.AddChart(XlChartType.xlColumnStacked100)   

  ' Ensure the Shape object contains a chart. If so,   
  ' set the source data for the chart to the range A1:C3.  
  If objShape.HasChart Then  
    objShape.Chart.SetSourceData Source:=Range("'Sheet1'!$A$1:$C$3")  
  End If  
End Sub

这篇关于使用c#创建多个Excel图表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆