C#操作Excel做Chart并输出成图


/// <summary>

        /// 创建Chart,并设置相关属性
        /// 最后按照固定路径输出成gif图
        /// </summary>
        /// <param name="saveDocPath">保存图片路径</param>
        public void CreateChart(string saveDocPath)
        {
            Excel.Series oSeries;
            Excel.Chart oChart;
            oWB = (Excel.Workbook)sheet.Parent;
            oChart = (Excel.Chart)oWB.Charts.Add(Missing.Value, Missing.Value,

                Missing.Value, Missing.Value);

//应用的数据"C39:C53"

            oChart.SetSourceData((Excel.Range)sheet.get_Range("C39:C53", Missing.Value), Missing.Value);         
            oChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置整个图表的边框线条          
            oChart.ChartArea.Border.Color = ColorTranslator.ToOle(Color.Black);// 设置整个图表的边框颜色
            oChart.ChartArea.Interior.Color = ColorTranslator.ToOle(Color.White);// 设置整个图表的内部颜色
            oChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.LightCyan); //设置绘图区的背景
            oChart.PlotArea.Width = 630;
            oChart.PlotArea.Height = 420;
            oChart.PlotArea.Top = 60;
            oChart.PlotArea.Left = 15;
            //oChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.FromName("#b2d235"));//一片黑,貌似不能用
            //oChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.FromArgb(64, 224, 208)); 
            oChart.HasLegend = false;//没有图例
            oChart.HasTitle = true;//设置标题
            oChart.ChartTitle.Text = DateTime.Now.Year.ToString()+"年"+DateTime.Now.Month.ToString()+"月"+DateTime.Now.Day.ToString()+"日北京市**区降雨量柱状图";
            oChart.ChartTitle.Font.Name = "黑体";
            //oChart.ChartTitle.Font.Bold = true;
            oChart.ChartTitle.Font.Size = 23;
            //oChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlDouble;//标题的线框样式
            //oChart.ChartTitle.Shadow = true;
            
            Use the ChartWizard to create a new chart from the selected data.
            //Rng = sheet.get_Range("C39:C53", Missing.Value);
            //oChart.ChartWizard(oResizeRng, Excel.XlChartType.xl3DColumn, Missing.Value,
            //    Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, Missing.Value,
            //    Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //oChart.Walls.Border.Color = ColorTranslator.ToOle(Color.Green);//walls只是在3D时使用
            //oChart.Walls.Interior.Color = ColorTranslator.ToOle(Color.Blue);
            //oChart.WallsAndGridlines2D = true;

            //设置圆柱样式
            oSeries = (Excel.Series)oChart.SeriesCollection(1);
            oSeries.XValues = sheet.get_Range ("B39", "B53");//x轴街道名称
            oSeries.Border.Color = ColorTranslator.ToOle(Color.Black);//圆柱的边框
            oSeries.Border.Weight = Excel.XlBorderWeight.xlThin;//圆柱边框线宽
            oSeries.Interior.Color = ColorTranslator.ToOle(Color.Lime);//圆柱的内部颜色
            oSeries.HasDataLabels = true;//圆柱有数据标签
            
            //oChart.Location(Excel.XlChartLocation.xlLocationAsObject, sheet.Name);

            //Excel.ChartGroup grp = (Excel.ChartGroup)oChart.ChartGroups(1);
            //grp.GapWidth = 20;//柱形不同的颜色
            //grp.VaryByCategories = true;

            //设置Y轴的显示
            Excel.Axis yAxis = (Excel.Axis)oWB.ActiveChart.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
            yAxis.MajorGridlines.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            yAxis.MajorGridlines.Border.Color = ColorTranslator.ToOle(Color.Aqua);//gridLine横向线条的颜色
            yAxis.MajorGridlines.Border.Weight = Excel.XlBorderWeight.xlThin;
            yAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
            yAxis.Border.Weight = Excel.XlBorderWeight.xlThin;
            yAxis.HasTitle = true;//y轴有标题
            yAxis.AxisTitle.Text = "单位:mm";
            yAxis.AxisTitle.Font.Name = "宋体";
            yAxis.AxisTitle.Font.Size = 17;
            //yAxis.AxisTitle.Font.Bold = true;
            yAxis.AxisTitle.Top = 35;
            yAxis.AxisTitle.Orientation = 0;//角度
            //yAxis.MinimumScale = 1;
            //yAxis.MaximumScale = 200;
            yAxis.TickLabels.Font.Name = "宋体";//y轴数字样式
            yAxis.TickLabels.Font.Size = 14;
            yAxis.TickLabels.Font.Bold = true;


            //设置X轴的显示
            Excel.Axis xAxis = (Excel.Axis)oWB.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
            xAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//x轴颜色
            xAxis.Border.Weight = Excel.XlBorderWeight.xlThin;
            //yAxis.TickLabelSpacing = 30;
            //xAxis.TickLabels.Orientation = Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;//Y轴显示的方向,是水平
            xAxis.TickLabels.Orientation = (Excel.XlTickLabelOrientation)45;
            xAxis.TickLabels.Font.Size = 15;
            xAxis.TickLabels.Font.Name = "楷体_GB2312";
            xAxis.TickLabels.Font.Bold =false;
            


            //chart另存图片
            string chartName = "chart" + DateTime.Now.ToString("yyyyMMdd");
       oChart.Export(saveDocPath+"\\"+chartName+".gif", "GIF", false);

        }
  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值