调用office的Excel制作统计图,并保存成图片

这篇具有很好参考价值的文章主要介绍了调用office的Excel制作统计图,并保存成图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

public class CMSChart
    {
        private Chart chart;

        public CMSChart(Chart chart)
        {
            this.chart = chart;
        }
        

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="fullPicName"></param>
        public void SaveAs(string fullPicName)
        {
            ChartImageFormat format = ChartImageFormat.Jpeg;
            string ext = fullPicName.Substring(fullPicName.LastIndexOf('.') + 1);
            switch (ext.ToLower())
            {
                case "bmp": format = ChartImageFormat.Bmp; break;
                case "jpg": format = ChartImageFormat.Jpeg; break;
                case "png": format = ChartImageFormat.Png; break;
                case "tif": format = ChartImageFormat.Tiff; break;
                case "emf": format = ChartImageFormat.Emf; break;
            }
            chart.SaveImage(fullPicName, format);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt">数据表,不能为空</param>
        /// <param name="xField">x轴数据字段,只有1个</param>
        /// <param name="yFields">y轴数据字段,多个</param>
        /// <param name="yTypes"></param>
        /// <param name="yTitles"></param>
        /// <param name="colors"></param>
        /// <param name="xTitle"></param>
        /// <param name="yTitle"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool ShowChart(DataTable dt, string xField, string[] yFields, string[] yTypes, string[] yTitles, Color[] colors, string xTitle, string yTitle, string title)
        {
            bool result = false;
            try
            {
                chart.Titles.Clear();
                Title t = new Title(title, Docking.Top);
                chart.Titles.Add(title);
                chart.ChartAreas.Clear();
                chart.Series.Clear();
                ChartArea area = new ChartArea();

                //绘图区域高/宽相对于图表的高/宽的百分比
                area.Position.Height = 52;
                area.Position.Width = 96;
                //绘图区域起始位置坐标相对于图表高/宽的百分比
                area.Position.X = 2;
                area.Position.Y = 16;

                area.Name = "area" + yTitles;
                area.AxisX.Title = xTitle;
                area.AxisX.TitleAlignment = StringAlignment.Far;
                int Interval = dt.Rows.Count / 12;
                if (Interval < 1)
                    Interval = 1;
                area.AxisX.Interval = Interval;
                area.AxisY.Title = yTitle;
                area.AxisX.MajorGrid.Enabled = false;
                area.AxisY.MajorGrid.Enabled = false;                
                chart.ChartAreas.Add(area);
                double min = double.MaxValue, max = double.MinValue;
                for (int i = 0; i < yFields.Length; i++)
                {                    
                    Series series = new Series();
                    series.Color = colors[i];
                    series.Name = yTitles[i];                    
                    switch (yTypes[i])
                    {
                        case "line": series.ChartType = SeriesChartType.Line;
                            series.BorderWidth = 2;
                            break;
                        case "column": series.ChartType = SeriesChartType.Column; break;
                    }
                    foreach (DataRow row in dt.Rows)
                    {
                        if (!Convert.IsDBNull(row[yFields[i]]))
                        {
                            int x = Convert.ToInt32(row[xField]);
                            double y = Convert.ToDouble(row[yFields[i]]);
                            DataPoint p = new DataPoint(x, y);
                            series.Points.Add(p);
                            if (y > max)
                                max = y;
                            if (y < min)
                                min = y;
                        }
                    }
                    if (min > 0)
                    {
                        double cha = max - min;
                        if (cha < min * 0.3)
                            area.AxisY.Minimum = Math.Round(min * 0.7, 0);
                    }
                    else if (max < 0)
                    { }
                    series.ChartArea = area.Name;
                    series.LabelToolTip = string.Format("年:#VALX\r\n{0}:#VAL", yTitles[i]);
                    series.ToolTip = string.Format("年:#VALX\r\n{0}:#VAL", yTitles[i]);                   
                    chart.Series.Add(series);                    
                }
            }
            catch (Exception ex)
            {
                ErrorManager.AddErrorToLog(ex);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="xField"></param>
        /// <param name="yFields"></param>
        /// <param name="yTypes"></param>
        /// <param name="yTitles"></param>
        /// <param name="colors"></param>
        /// <param name="xTitle"></param>
        /// <param name="yTitle"></param>
        /// <param name="title"></param>
        /// <param name="fullPicName"></param>
        /// <returns></returns>
        public bool CreatePic(DataTable dt, string xField, string[] yFields, string[] yTypes, string[] yTitles, Color[] colors, string xTitle, string yTitle, string title, string fullPicName)
        {
            bool result = false;
            try
            {
                ShowChart(dt, xField, yFields, yTypes, yTitles, colors, xTitle, yTitle, title);
                SaveAs(fullPicName);
            }
            catch (Exception ex)
            {
                ErrorManager.AddErrorToLog(ex);
                result = false;
            }
            return result;
        }
    }

excel格式设置:
调用office的Excel制作统计图,并保存成图片,excel文章来源地址https://www.toymoban.com/news/detail-523193.html

到了这里,关于调用office的Excel制作统计图,并保存成图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包