C#让Excel中数据自动生成图表

如上图,数据已经存在,如何用C#代码另右边的坐标图自动生成,附上代码,有例子也行... 如上图,数据已经存在,如何用C#代码另右边的坐标图自动生成,附上代码,有例子也行 展开
 我来答
静心闻无弦
2011-08-29 · 超过16用户采纳过TA的回答
知道答主
回答量:50
采纳率:0%
帮助的人:46万
展开全部
以前用过,保存了一个函数如下:
private void CreateChart(Excel._Workbook m_Book,Excel._Worksheet m_Sheet,int num)
{
Excel.Range oResizeRange;
Excel.Series oSeries;

m_Book.Charts.Add(Missing.Value, Missing.Value, 1, Missing.Value);
m_Book.ActiveChart.ChartType = Excel.XlChartType.xlLine;//设置图形

//设置数据取值范围
m_Book.ActiveChart.SetSourceData(m_Sheet.get_Range("A2", "C" + num.ToString()), Excel.XlRowCol.xlColumns);
//m_Book.ActiveChart.Location(Excel.XlChartLocation.xlLocationAutomatic, title);
//以下是给图表放在指定位置
m_Book.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, m_Sheet.Name);
oResizeRange = (Excel.Range)m_Sheet.Rows.get_Item(10, Missing.Value);
m_Sheet.Shapes.Item("Chart 1").Top = (float)(double)oResizeRange.Top; //调图表的位置上边距
oResizeRange = (Excel.Range)m_Sheet.Columns.get_Item(6, Missing.Value); //调图表的位置左边距
// m_Sheet.Shapes.Item("Chart 1").Left = (float)(double)oResizeRange.Left;
m_Sheet.Shapes.Item("Chart 1").Width = 400; //调图表的宽度
m_Sheet.Shapes.Item("Chart 1").Height = 250; //调图表的高度

m_Book.ActiveChart.PlotArea.Interior.ColorIndex = 19; //设置绘图区的背景色
m_Book.ActiveChart.PlotArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置绘图区边框线条
m_Book.ActiveChart.PlotArea.Width = 400; //设置绘图区宽度
//m_Book.ActiveChart.ChartArea.Interior.ColorIndex = 10; //设置整个图表的背影颜色
//m_Book.ActiveChart.ChartArea.Border.ColorIndex = 8;// 设置整个图表的边框颜色
m_Book.ActiveChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置边框线条
m_Book.ActiveChart.HasDataTable = false;

//设置Legend图例的位置和格式
m_Book.ActiveChart.Legend.Top = 20.00; //具体设置图例的上边距
m_Book.ActiveChart.Legend.Left = 60.00;//具体设置图例的左边距
m_Book.ActiveChart.Legend.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone;
m_Book.ActiveChart.Legend.Width = 150;
m_Book.ActiveChart.Legend.Font.Size = 9.5;
//m_Book.ActiveChart.Legend.Font.Bold = true;
m_Book.ActiveChart.Legend.Font.Name = "宋体";
//m_Book.ActiveChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;//设置图例的位置
m_Book.ActiveChart.Legend.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置图例边框线条

//设置X轴的显示
Excel.Axis xAxis = (Excel.Axis)m_Book.ActiveChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
xAxis.MajorGridlines.Border.LineStyle = Excel.XlLineStyle.xlDot;
xAxis.MajorGridlines.Border.ColorIndex = 1;//gridLine横向线条的颜色
xAxis.HasTitle = false;
xAxis.MinimumScale = 1500;
xAxis.MaximumScale = 6000;
xAxis.TickLabels.Font.Name = "宋体";
xAxis.TickLabels.Font.Size = 9;

//设置Y轴的显示
Excel.Axis yAxis = (Excel.Axis)m_Book.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
yAxis.TickLabelSpacing = 30;
yAxis.TickLabels.NumberFormat = "M月D日";
yAxis.TickLabels.Orientation = Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;//Y轴显示的方向,是水平还是垂直等
yAxis.TickLabels.Font.Size = 8;
yAxis.TickLabels.Font.Name = "宋体";

//m_Book.ActiveChart.Floor.Interior.ColorIndex = 8;
/***以下是设置标题*****
m_Book.ActiveChart.HasTitle=true;
m_Book.ActiveChart.ChartTitle.Text = "净值指数";
m_Book.ActiveChart.ChartTitle.Shadow = true;
m_Book.ActiveChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlContinuous;
*/

oSeries = (Excel.Series)m_Book.ActiveChart.SeriesCollection(1);
oSeries.Border.ColorIndex = 45;
oSeries.Border.Weight = Excel.XlBorderWeight.xlThick;
oSeries = (Excel.Series)m_Book.ActiveChart.SeriesCollection(2);
oSeries.Border.ColorIndex = 9;
oSeries.Border.Weight = Excel.XlBorderWeight.xlThick;

}

自己改吧改吧
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式