在开发一些管理信息系统中开发一些统计报表是必须的功能之一。开发报表的用到的组件也很多。可以用微软自带的owc组件,这是微软的office报表组件,可以和.net实现无缝的连接。也可以采用如ZedGraph等开源的.net组件来开发,还可以使用水晶报表和SQL Server自带的Report Service来开发报表。手段很多,各有所长。下面我介绍下我以前项目中使用的owc开发报表的一个示例,代码如下,代码中已经加入了必要的注视就不再一一讲解了。

private void DrawMonthlyErrorTimeGraph(DataTable dtData)

        {

            string strGroup = Request.QueryString["Group"].ToString();

            string stryear = Request.QueryString["Year"].ToString();

            string strmonth = Request.QueryString["Month"].ToString();

            lblTitle.Text = stryear+"年"+strmonth+"月"+strGroup+"月xxxxx报表";

            // 在此处放置用户代码以初始化页面

            //创建ChartSpace对象来放置图表

            ChartSpace objCSpace = new ChartSpaceClass();

            //在ChartSpace对象中添加图表,Add方法返回chart对象

            ChChart objChart = objCSpace.Charts.Add(0);

            //指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到

            objChart.Type = ChartChartTypeEnum.chChartTypeLineStackedMarkers;

            //指定图表是否需要图例

            objChart.HasLegend = true;

            //给定标题

            objChart.HasTitle = true;

            objChart.Title.Caption =strGroup+"xxxx透视";

            //给定x,y轴的图示说明

            objChart.Axes[0].HasTitle = true;

            objChart.Axes[0].Title.Caption = "X : 日期";

            objChart.Axes[1].HasTitle = true;

            objChart.Axes[1].Title.Caption = "Y : 故障时间";

            //计算数据

            /*categories 和 values 可以用tab分割的字符串来表示*/

            string strSeriesName_ErrorTime = "故障时间";

            int idays = GetMonthDays(stryear, strmonth);

            StringBuilder sbdays = new StringBuilder();

            for (int i = 1; i <= idays; i++)

            {

                sbdays.Append(i.ToString());

                sbdays.Append('\t');

            }

            string strCategory = sbdays.ToString();

            StringBuilder strb_ErrorTime = new StringBuilder();

           

           

            for (int j = 1; j <= idays; j++)

            {

                if (dtData.Rows[0][j.ToString()].ToString() == "")

                {

                    strb_ErrorTimeTime.Append("0");

                }

                else

                {

                    strb_ErrorTimeTime.Append(dtData.Rows[0][j.ToString()].ToString());

                }

                strb_ErrorTime.Append('\t');

            }

            string strValue_Error = strb_ErrorTime.ToString();

            //添加一个series

            objChart.SeriesCollection.Add(0);

            //给定series的名字

            objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames,

            +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName_StopLine);

            //给定分类

            objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,

            +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strCategory);

            //给定值

            objChart.SeriesCollection[0].SetData

              (ChartDimensionsEnum.chDimValues,

              (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue_Error );

            //创建GIF文件的相对路径.

            string FileName = "/" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()

           + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".gif";

            //输出成GIF文件.

            objCSpace.ExportPicture(Server.MapPath("../TempImage") + FileName, "GIF", 650, 300);

            //把图片添加到placeholder.

            string strImageTag = "<IMG SRC='" + "../TempImage" + FileName + "'/>";

            PlaceChartStopLineTime.Controls.Add(new LiteralControl(strImageTag));

        }