activeReport_开发

来源:互联网 发布:阿里云幕布的申请 编辑:程序博客网 时间:2024/05/21 01:46

ActiveReport .net下的一个出色的报表开发程序,虽然和水晶报表相比,名气不那么大,甚至有很多人不知道它的存在,但是并不妨碍它在.net报表开发中的出色表现,本文将一步一步地介绍如何使用它和VS2008开发.net报表。

 

1.         安装:

你可 以从Data Dynamics的网站上下载最新的ActiveReport for .net 3.0 ,你可以免费使用,但是在生成的报表最下边 有水印,不过不影响报表的整体美观。下载后直接安装就可以了。你也可以找破解包,安装完后会看到开始-程序菜单里的DataDynamics的目录,里面包括了,帮助,例子程序等。

2.         第一个Web表程序。

在安 装完成后,启动VS2008,会看到启动界面上有ActiveReport的图标,进入后新建一个工程,并且给工程添加新文件,在文件类型中就可以看到ActiveReport文件的图标。给报表文件命名为ActiveReport1,确定后会弹出一个提示:你正在添加一个特殊格式的文件,要在网站中使用此类型,需将其放到 app_code文件夹中,是否将其放入App_Code中,点击确定,则在App_Code看到有一个ActiveReport1.cs文件,这个就是报表文件了。打开文件,就是报表的CS文 件,在此注明,大家不要被此文件给愚弄了,说实话我在此迷惑不解了好久,一直在找它的设计文件,但是一直无果,所以一直费劲的在敲代码来布局,但在一个偶然的机会,才发现右键,视图设计器,就可以打开设计页面,当时我气的差点吐血,我可是重装了N次呀,这才是众里寻它千百度,蓦然回首那人却在灯火阑珊处,而前人没有一个人提到,为了大家少走弯路,所以在此强调一下。下面我们接着言归正传,在设计页面可以看到三部分,PageHeader(表头),Detail(明细),PageFooter(表尾)。打开工具箱,给Detail部分随便添加一个Label

下面 就要显示这个报表了,在工程中添加一个web窗体,在窗体上写上以

<object id="arv" codebase="arview2.cab#version=-1,-1,-1,-1" height="100%" width="100%" classid="clsid:8569D715-FF88-44BA-8D1D-AD3E59543DDE" viewastext>
    <param name="_ExtentX" value="26141">
    <param name="_ExtentY" value="11959">
   </object>

在窗体上还要写以下 代码:此处是一个再次load事件,应该是内部调用使用的。

<script language="vbscript">

<!--
   sub arv_ControlLoaded()
       arv.DataPath = "ActiveXViewer.aspx?ReturnReport=1"
       
   end sub
   -->
   </script>

   我 们在.cs文件中写入

string sReturnReport = this.Page.Request.QueryString["ReturnReport"];

  if ( (sReturnReport != null) && (sReturnReport.Trim().Length != 0) )
  {
      this.Page.Response.Buffer = true;
   ActiveReport3 rpt = null;

   try
   {

               //这两句话是重点
               rpt = new ProductReport(myArray);
               rpt.Run(false);     
   }
   catch (DataDynamics.ActiveReports.ReportException eRunReport)
   {
    this.Trace.Warn("Report failed to run:/n" + eRunReport.ToString());
   }

     MemoryStream outStream = new MemoryStream();
      rpt.Document.Save(outStream, DataDynamics.ActiveReports.Document.RdfFormat.AR20);

    outStream.Seek(0, SeekOrigin.Begin);

    byte[] bytes = new byte[outStream.Length];
     outStream.Read(bytes, 0, (int)outStream.Length);

     this.Page.Response.ClearContent();
   this.Page.Response.ClearHeaders();

     this.Page.Response.BinaryWrite(bytes);
      this.Page.Response.End();
  }

然后运行,就可以 看到在Viewer控件中显示报表了。

3.       还要注意的是,在web方式下,需要 在web.config文件中添加下面这段:

<httpHandlers>

<addverb="*"path="*.rpx"type="DataDynamics.ActiveReports.Web.Handlers.RpxHandler, ActiveReports.Web, Version=4.2.1.1238, Culture=neutral, PublicKeyToken=cc4967777c49a3ff"/>

<addverb="*"path="*.ActiveReport"type="DataDynamics.ActiveReports.Web.Handlers.CompiledReportHandler, ActiveReports.Web, Version=4.2.1.1238, Culture=neutral, PublicKeyToken=cc4967777c49a3ff"/>

<addverb="*"path="*.ArCacheItem"type="DataDynamics.ActiveReports.Web.Handlers.WebCacheAccessHandler, ActiveReports.Web, Version=4.2.1.1238, Culture=neutral, PublicKeyToken=cc4967777c49a3ff"/>

</httpHandlers>

上一篇主要是说了activereport的配置和一个简单的示例,这次我来详细说一下,如何生成一个动态的报表。

报表分为三块,pageHeader,pageFooter,还有detail,

pageHeader是设置列名和titledetail是设计表的内容包括统计数据,pageFooter设置页脚。

第一步,我们新建一个报表文件,一个是设计页面,一个是源代码的页面,我们可以在设计页面进行布局,不过我是针对我的项目而言,事先不知道要显示多少列,所以我直接写的代码。

首先:我重载了构造函数,加了两个参数,因为我要获得我要显示多少列,还有数据查询语句的条件,具体代码如下:

public AssetInfoReport(string filterSql,string columnName)
 {
  //
  // Required for Windows Form Designer support
  //
        this.filterSql = filterSql;

       //这是我把要显示的列放在一个字符串中了,其实仁者见仁,有好多种方法可以使用,可以根据具体情况做改动。
        this.columnNameArr = columnName.Split(';');
  InitializeComponent();
 }

然后我们就可以在 InitializeComponent这个方法中进行我的设计了。

第二步 参数设计

//设置打印的方向,Landscape为横向,Portrait为纵向

this.PageSettings.Orientation = DataDynamics.ActiveReports.Document.PageOrientation.Landscape;

//设置打印的纸张大小,我设的为A4.

        this.PageSettings.PaperHeight = 11.7F;
        this.PageSettings.PaperWidth = 8.267716F;
        this.PageSettings.Margins.Top = 1;
        this.PageSettings.Margins.Bottom = 1;
        this.PageSettings.Margins.Left = 1;
        this.PageSettings.Margins.Right = 1;
//
设置实际打印的宽度
,        
        
        this.PrintWidth = this.PageSettings.PaperHeight - this.PageSettings.Margins.Left - this.PageSettings.Margins.Right;

 第三步,报表设计

if (columnNameArr != null)
        {
            float _top = 0.7F;
            float _left = 0F;
            float _width = this.PrintWidth / columnNameArr.Length;
            for (int i = 0; i < columnNameArr.Length; i++)
            {
               
                string labelID = "lbl" + columnNameArr[i].Split(',')[0];
                string textBoxID = "txt" + columnNameArr[i].Split(',')[0];
                string lblValue = columnNameArr[i].Split(',')[1];
                string textBoxValue = columnNameArr[i].Split(',')[0];
                //
初始化
Label background-color: #C7E0FA;overflow-y:hidden
                Label lbl = new DataDynamics.ActiveReports.Label();
                lbl.Name = labelID;
                lbl.Value = lblValue;
                lbl.Text = lblValue;
                lbl.Border.BottomColor = System.Drawing.Color.Black;
                lbl.Border.BottomStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                lbl.Border.LeftColor = System.Drawing.Color.Black;
                lbl.Border.LeftStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                if (i == columnNameArr.Length - 1)
                {
                    lbl.Border.RightColor = System.Drawing.Color.Black;
                    lbl.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                }
                else
                {
                    lbl.Border.RightColor = System.Drawing.Color.Black;
                    lbl.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.None;
                   
                }
                lbl.Border.TopColor = System.Drawing.Color.Black;
                lbl.Border.TopStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                lbl.Style = "text-align: center; font-size: 10pt; vertical-align: middle;font-weight:bold";
                lbl.Height = 0.3F;
                lbl.Left = _left;
                lbl.Top = _top;
                lbl.Width = _width;
                //
Label添加到报表头中

                this.pageHeader.Controls.Add(lbl);
                //
初始化TextBox
                TextBox txt = new DataDynamics.ActiveReports.TextBox();
                txt.Name = textBoxID;
                txt.DataField = textBoxValue;

                txt.Alignment = DataDynamics.ActiveReports.TextAlignment.Center;
                txt.Border.BottomStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                txt.Border.LeftStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                if (i == columnNameArr.Length - 1)
                {
                    txt.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                    //txt.Border.TopStyle = DataDynamics.ActiveReports.BorderLineStyle.Solid;
                }
                else
                {
                    txt.Border.RightStyle = DataDynamics.ActiveReports.BorderLineStyle.None;
                    txt.Border.TopStyle = DataDynamics.ActiveReports.BorderLineStyle.None;
                }
                txt.CanGrow = false;
                txt.DistinctField = null;
                txt.Left = _left;
                txt.Style = "font-size: 10pt;text-align: center;";
                txt.VerticalAlignment = DataDynamics.ActiveReports.VerticalTextAlignment.Middle;
                txt.Width = _width;
                txt.Size = new System.Drawing.SizeF(_width, 0.3F);
                //
TextBox添加到报表体中

                this.detail.Controls.Add(txt);
                _left += lbl.Width;
            }
        }

当然还有pageHeader,pageFooter,detail的高度等参数的设置,这里就不再多说.

第四步 数据源绑定

this.DataSource = GetAssetInfo(filterSql);

然后再新建一个页面,在新页面中调用此报表,具体参见上一篇文章.这样一个动态报表打印就完成了.

 

原创粉丝点击