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();//顯示給圖像

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章