我使用chart类来生成excel的图表,使用如下代码
//-----------------chart向导,设置图表的基本信息-------------
/*生成图表的方法.MyRange1(数据源)、 XlChartType.xl3DColumn(图表样式)、1(套用样式格式的编号)
XlRowCol.xlColumns(分组是按行还是列)、1(第几列是分类也就是x轴)、1(从第几行开始取数,空出0行,可以作为图例)、true(有图列)
"KPI指标"(标题)、"姓名"(x轴标题)、"数量"(y轴标题)、"系列轴标题"(第二数字轴标题)*/
MyChart.ChartWizard
(MyRange1, Type.Missing, Type.Missing,
XlRowCol.xlColumns, 1, 1, true,
"KPI指标", "姓名", "数量", "系列轴标题");
//实例化chart对象的图形设置对象
Excel.ChartGroup grp = (Excel.ChartGroup)MyChart.ChartGroups(1);
//设置系列格式,也就是图表中字段的显示格式。SeriesCollection(1),括号中表示第几列。
Excel.Series s1 = (Excel.Series)grp.SeriesCollection(1);
//s.BarShape = XlBarShape.xlCylinder;//柱状设置成圆形
s1.HasDataLabels = true;//柱状头上的标签打开,显示数据
Excel.Series s2 = (Excel.Series)grp.SeriesCollection(2);
s2.HasDataLabels = true;//标签打开,显示数据
s2.ChartType = XlChartType.xlLine;//柱状设置为线型
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
s3.HasDataLabels = true;//标签打开,显示数据
s3.ChartType = XlChartType.xlLine;//柱状设置为线型
MyRange1有3列,图表是一个柱状和两个线性的图形。但是该程序执行到
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
报错
如果将
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
s3.HasDataLabels = true;//柱状头上的标签打开,显示数据
s3.ChartType = XlChartType.xlLine;//柱状设置为线型
注销,则程序可以输出一个柱状和一个个线性的图形。
每一个Excel.Series 代表一个系列。既然
Excel.Series s1 = (Excel.Series)grp.SeriesCollection(1);和
Excel.Series s2 = (Excel.Series)grp.SeriesCollection(2);
都可以执行。为什么加3
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
就会报错呢?另外说明一下,如果不用Excel.Series 设置系列的显示格式。那么图表可以正常显示3个柱状系列。问题出在声明第三个系列上面。关于这方面的资料,网上没有查到,MSDN里面也没有。我实在是想不出来啦。所以在这里请教各位大侠啦。
//-----------------chart向导,设置图表的基本信息-------------
/*生成图表的方法.MyRange1(数据源)、 XlChartType.xl3DColumn(图表样式)、1(套用样式格式的编号)
XlRowCol.xlColumns(分组是按行还是列)、1(第几列是分类也就是x轴)、1(从第几行开始取数,空出0行,可以作为图例)、true(有图列)
"KPI指标"(标题)、"姓名"(x轴标题)、"数量"(y轴标题)、"系列轴标题"(第二数字轴标题)*/
MyChart.ChartWizard
(MyRange1, Type.Missing, Type.Missing,
XlRowCol.xlColumns, 1, 1, true,
"KPI指标", "姓名", "数量", "系列轴标题");
//实例化chart对象的图形设置对象
Excel.ChartGroup grp = (Excel.ChartGroup)MyChart.ChartGroups(1);
//设置系列格式,也就是图表中字段的显示格式。SeriesCollection(1),括号中表示第几列。
Excel.Series s1 = (Excel.Series)grp.SeriesCollection(1);
//s.BarShape = XlBarShape.xlCylinder;//柱状设置成圆形
s1.HasDataLabels = true;//柱状头上的标签打开,显示数据
Excel.Series s2 = (Excel.Series)grp.SeriesCollection(2);
s2.HasDataLabels = true;//标签打开,显示数据
s2.ChartType = XlChartType.xlLine;//柱状设置为线型
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
s3.HasDataLabels = true;//标签打开,显示数据
s3.ChartType = XlChartType.xlLine;//柱状设置为线型
MyRange1有3列,图表是一个柱状和两个线性的图形。但是该程序执行到
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
报错
如果将
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
s3.HasDataLabels = true;//柱状头上的标签打开,显示数据
s3.ChartType = XlChartType.xlLine;//柱状设置为线型
注销,则程序可以输出一个柱状和一个个线性的图形。
每一个Excel.Series 代表一个系列。既然
Excel.Series s1 = (Excel.Series)grp.SeriesCollection(1);和
Excel.Series s2 = (Excel.Series)grp.SeriesCollection(2);
都可以执行。为什么加3
Excel.Series s3 = (Excel.Series)grp.SeriesCollection(3);
就会报错呢?另外说明一下,如果不用Excel.Series 设置系列的显示格式。那么图表可以正常显示3个柱状系列。问题出在声明第三个系列上面。关于这方面的资料,网上没有查到,MSDN里面也没有。我实在是想不出来啦。所以在这里请教各位大侠啦。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货