OWC报表

对OWC进行了封装,以方便在页面调用,可以很简单的生成稳中有各种折线图,柱状图及饼图等

工程下载

using System;
using System.Data;
using System.Text;
using Microsoft.Office.Interop.Owc11;//添加Office组件引用
namespace Aoner
{
    /// <summary>
    /// 统计图的封装类。
    /// </summary>
    public class Statement
    {
        #region 属性
        private string imagepath;
        private string title;
        private string xAxis;
        private string yAxis;
        private string seriesname;
        private int width;
        private int height;
        private DataTable datasource;
        private string strCategory;
        private string strValue;
        /// <summary>
        /// 图片存放路径
        /// </summary>
        public string ImagePath
        {
            set { imagepath = value; }
            get { return imagepath; }
        }
        public string Title
        {
            set { title = value; }
            get { return title; }
        }
        public string XAxis
        {
            set { xAxis = value; }
            get { return xAxis; }
        }
        public string YAxis
        {
            set { yAxis = value; }
            get { return yAxis; }
        }
        public string SeriesName
        {
            set { seriesname = value; }
            get { return seriesname; }
        }
        public int Width
        {
            set { width = value; }
            get { return width; }
        }
        public int Height
        {
            set { height = value; }
            get { return height; }
        }
        public DataTable DataSource
        {
            set
            {
                datasource = value;
                strCategory = GetColumnsStr(datasource);
                strValue = GetValueStr(datasource);
            }
            get { return datasource; }
        }
        private string GetColumnsStr(DataTable dt)
        {
            StringBuilder strList = new StringBuilder();
            foreach (DataRow r in dt.Rows)
            {
                strList.Append(r[0].ToString() + '/t');
            }
            return strList.ToString();
        }
        private string GetValueStr(DataTable dt)
        {
            StringBuilder strList = new StringBuilder();
            foreach (DataRow r in dt.Rows)
            {
                strList.Append(r[1].ToString() + '/t');
            }
            return strList.ToString();
        }
        #endregion

        public Statement() { }
        public Statement(string imagePath, string title, string seriesName)
        {
            this.imagepath = imagePath;
            this.title = title;
            this.seriesname = seriesName;
        }
        /// <summary>
        /// 饼图
        /// </summary>
        /// <returns></returns>
        public string CreatePie()
        {
            ChartSpace objCSpace = new ChartSpaceClass(); 
            ChChart objChart = objCSpace.Charts.Add(0);
            objChart.Type = ChartChartTypeEnum.chChartTypePie;
            objChart.HasLegend = true;
            objChart.HasTitle = true;
            objChart.Title.Caption = title;
            ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
            ThisChSeries.SetData(ChartDimensionsEnum.chDimSeriesNames,
            ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName);
            ThisChSeries.SetData(ChartDimensionsEnum.chDimCategories,
            ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
            ThisChSeries.SetData(ChartDimensionsEnum.chDimValues,
            ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
            ChDataLabels dl = objChart.SeriesCollection[0].DataLabelsCollection.Add();
            dl.HasValue = true;
            dl.HasPercentage = true;
            string filename = DateTime.Now.Ticks.ToString() + ".gif";
            string strAbsolutePath = imagepath + "//" + filename;
            objCSpace.ExportPicture(strAbsolutePath, "GIF", width, height);//输出成GIF文件.
            return filename;
        }
        public string CreateBar(string type)
        {
            //创建ChartSpace对象来放置图表
            ChartSpace laySpace = new ChartSpaceClass();
            //在ChartSpace对象中添加图表
            ChChart InsertChart = laySpace.Charts.Add(0);
            switch(type)
            {
                case "Line":
                    InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
                    break;
                case "Area":
                    InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
                    break;
                case "Bar":
                    InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
                    break;
                case "Column":
                    InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图
                    break;
                default:
                    InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
                    break;
            }
            //指定图表是否需要图例标注
            InsertChart.HasLegend = true;
            InsertChart.HasTitle = true;//为图表添加标题
            InsertChart.Title.Caption = title;//标题名称
            //为x,y轴添加图示说明
            InsertChart.Axes[0].HasTitle = true;
            InsertChart.Axes[0].Title.Caption = xAxis;//月份
            InsertChart.Axes[1].HasTitle = true;
            //InsertChart.Axes[1].Scaling.SplitMinimum = 400;//设定临界值s
            InsertChart.Axes[1].Title.Caption = yAxis;
            //添加一个series系列
            InsertChart.SeriesCollection.Add(0);
            //给定series系列的名字
            InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), seriesname);
            //给定分类
            InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
            //给定值
            InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
            //输出文件.
            ChDataLabels dl = InsertChart.SeriesCollection[0].DataLabelsCollection.Add();
            dl.HasValue = true;
            string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".gif";
            string strAbsolutePath = imagepath + "//" + filename;
            laySpace.ExportPicture(strAbsolutePath, "GIF", width, height);
            return filename;
           
        }
    }
}

页面调用示例:

OWCChart11 chart = new OWCChart11();
        chart.Title = "啥球子破玩意";
        chart.SeriesName = "图例";
        string filepath = Server.MapPath(".");
        chart.PhaysicalImagePath = filepath;
        chart.PicHight = 320;
        chart.PicWidth = 500;
        chart.DataSource = ds.Tables[0];//这是你的数据源
        this.Image1.ImageUrl = filepath + "//" + chart.CreatePie();//显示给图像

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值