【问题标题】:Adding series to Excel chart exception将系列添加到 Excel 图表异常
【发布时间】:2014-07-17 14:45:30
【问题描述】:

我有一个包含一些数据的 excel 文件。我正在尝试打开第二张工作表并创建一个图表。问题是Series 给了我一个System.Runtime.InteropServices.COMException was caught 或者如果我取消注释注释行一个No overload for method 'SeriesCollection' takes '0' arguments。这是我的代码:

Microsoft.Office.Interop.Excel.ChartObjects chartObjs = (Microsoft.Office.Interop.Excel.ChartObjects)ws.ChartObjects(Type.Missing);
            Microsoft.Office.Interop.Excel.ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
            Microsoft.Office.Interop.Excel.Chart xlChart = chartObj.Chart;

            Range rg1 = ws.get_Range("A1", "D" + rowcount);
            rg1.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;

            xlChart.SetSourceData(rg1, Microsoft.Office.Interop.Excel.XlRowCol.xlColumns);
            xlChart.ChartType = XlChartType.xlLine;
            xlChart.Legend.Position = XlLegendPosition.xlLegendPositionBottom;

            Axis axis = (Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);

            axis.MaximumScaleIsAuto = false;
            axis.MaximumScale = 3;

            Axis Xaxis = (Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
            Xaxis.TickLabels.Orientation = XlTickLabelOrientation.xlTickLabelOrientationDownward;

            //SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();

            Series s1 = (Series)xlChart.SeriesCollection(1);
            s1.Name = "Serie1";
            s1.MarkerStyle = XlMarkerStyle.xlMarkerStyleCircle;

            //seriesCollection.NewSeries();

            Series s2 = (Series)xlChart.SeriesCollection(2);
            s2.Name = "Serie2";
            s2.MarkerStyle = XlMarkerStyle.xlMarkerStyleNone;

            //seriesCollection.NewSeries();

            Series s3 = (Series)xlChart.SeriesCollection(3);
            s3.Name = "Serie3";
            s3.MarkerStyle = XlMarkerStyle.xlMarkerStyleNone;

如果我保留 cmets,错误会显示无效参数并显示在该行: 系列 s2 = (系列)xlChart.SeriesCollection(2); 如果我删除 cmets,我会在该行得到第二个异常:

SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();

如果我将1 添加为参数,则图表无法正确显示。您对如何使其工作有任何建议吗?

【问题讨论】:

    标签: c# excel series


    【解决方案1】:

    啊,那些东西仍然让我做噩梦。 SeriesCollection 周围有些奇怪——但我不记得它到底是什么。

    尝试重新包含该行 //SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection(); 并在各处引用 seriesCollection 对象。 另外,SeriesCollection 的索引可能是从零开始的,你可以试试吗?

    【讨论】:

    • 我尝试将0作为参数添加,但仍然出现相同的异常:System.Runtime.InteropServices.COMException,因此图表无法正常显示。
    • 我看到有人在这个话题的答案中遇到了同样的问题:stackoverflow.com/questions/5573972/excel-charts-c?rq=1 很遗憾,还没有解决。
    • Comexception 的内容是什么?有什么线索吗?此外,尝试在 seriesCollection 上使用数组索引 []
    • 它只是说Invalid Parameter。你能举一个数组索引的例子[]
    • 我发现了问题。在声明SeriesCollection 时,我不得不将Type.Missing 作为参数。我已将您的解决方案标记为答案。感谢您的帮助!
    【解决方案2】:

    默认情况下,当您创建新图表时,它没有任何系列,因此您无法使用 chartPage.SeriesCollection(1) 选择它。您需要先创建一个系列。

    要添加新系列,您需要使用以下内容:

    SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();
    
    Series s1 = seriesCollection.NewSeries();
    s1.Name = "Serie1";
    s1.MarkerStyle = XlMarkerStyle.xlMarkerStyleCircle;
    
    Series s2 = seriesCollection.NewSeries();
    s2.Name = "Serie2";
    s2.MarkerStyle = XlMarkerStyle.xlMarkerStyleNone;
    

    您可能还需要将值添加到系列而不是图表中,例如:

    Series ser = sc.NewSeries();
    ser.XValues = _excelWorksheet.Range[YourRange];
    ser.Values = _excelWorksheet.Range[YourRange];
    

    【讨论】:

      猜你喜欢
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 2015-06-03
      • 2021-06-05
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多