我有一个装有一些数据的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;
如果我保留注释,则错误表明参数无效,并在该行显示:
Series s2 =(Series)xlChart.SeriesCollection(2);
如果删除注释,则该行会出现第二个异常:
SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();
如果我添加
1
作为参数,则图表无法正确显示。您对如何使其运作有任何建议吗? 最佳答案
哎呀,这些东西仍然给我做噩梦。 SeriesCollection周围有些怪异-但我不记得确切是什么。
尝试重新包含该行
// SeriesCollection seriesCollection =(SeriesCollection)xlChart.SeriesCollection();
并在任何地方引用seriesCollection对象。
也可能是SeriesCollection的索引是从零开始的,您可以尝试吗?
关于c# - 向Excel图表异常添加序列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23910573/