ActiveReport 报表事件调用之动态脚本剖析

发布时间:2014/03/26 00:03 发布者:roger.wang

返回博客中心

在C#下,有一个类库System.CodeDom.Compiler,非常强大。如,可用来动态生成dll,因此可以实现动态表达式、报表等函数,非常方便。AR的Section报表(区域报表)中有脚本,是通过System.CodeDom.Compiler实现的编译为dll,具体下面的实战步骤。

Step 1: 新建一个WindowsFormsApplication1的exe应用程序

Step 2: 新建SectionReport1.rpx区域报表,并在其Detail内拖入一个TextBox控件

AR8_Script

Step 3: 新建一个Model类,代码如下:

Code Snippet
  1. publicpartialclassModule1
  2. {
  3.     publicstring strCD;
  4.  
  5.     publicstring PrintReport()
  6.     {
  7.         strCD ="C#";
  8.  
  9.         return strCD;
  10.     }
  11. }

AR8_Script2

Step 4: 在SectionReport1.rpx的脚本中,添加如下代码:

WindowsFormsApplication1.Module1 mo = new WindowsFormsApplication1.Module1();

public void ActiveReport_DataInitialize()
{
    string s = mo.strCD;
    mo.PrintReport();
       
    ((TextBox) rpt.Sections["Detail"].Controls["TextBox1"]).Text = mo.strCD;    
}
 

雷区

  • 脚本,一定要注意大小写。如     ((TextBox) rpt.Sections["Detail"].Controls["TextBox1"]).Text = mo.strCD;
  • WindowsFormsApplication1.Module1 需要添加命名空间

AR8_Script3

Step 5: Form1添加Viewer,并在Load事件添加如下代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            string url = @"..\..\SectionReport1.rpx";

            SectionReport Report = new SectionReport();
            System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(url);
            Report.LoadLayout(xtr);
            //Report.AddScriptReference("System.Data.dll");
            Report.AddScriptReference("WindowsFormsApplication1.exe");            
            Report.Run();
            //Report.AddNamedItem()
            xtr.Close();
            viewer1.Document = Report.Document;
        }

 

结果如下:

AR8_Script4

 

说了半天,怎么没有看到Compiler的概念呢。

请看代码:

Code Snippet
  1. namespace Script
  2. {
  3.     using GrapeCity.ActiveReports;
  4.     using GrapeCity.ActiveReports.SectionReportModel;
  5.     using GrapeCity.ActiveReports.Data;
  6.     using GrapeCity.ActiveReports.Document.Section;
  7.     using System;
  8.     using System.Drawing;
  9.     using System.Diagnostics;
  10.     using System.Data;
  11.     using System.Collections;
  12.     using System.Collections.Generic;
  13.  
  14.     publicclassReportItems
  15.     {
  16.  
  17.         public GrapeCity.ActiveReports.SectionReport rpt;  //
  18.         public GrapeCity.ActiveReports.SectionReportModel.PageHeader PageHeader;
  19.         public GrapeCity.ActiveReports.SectionReportModel.Detail Detail;
  20.         public GrapeCity.ActiveReports.SectionReportModel.TextBox TextBox1;
  21.         public GrapeCity.ActiveReports.SectionReportModel.TextBox TextBox2;
  22.         public GrapeCity.ActiveReports.SectionReportModel.PageFooter PageFooter;
  23.     }
  24.  
  25.     //
  26.     publicclassReportScript : ReportItems
  27.     {
  28.         WindowsFormsApplication1.Module1 mo =new WindowsFormsApplication1.Module1();
  29.         publicvoid ActiveReport_DataInitialize()
  30.         {
  31.             string s = mo.strCD;
  32.             mo.PrintReport();
  33.  
  34.             ((TextBox)rpt.Sections["Detail"].Controls["TextBox1"]).Text = mo.strCD;
  35.         }
  36.     }
  37. }

 

在Report.Run();的时候,通过Compiler生成了如下的C#代码。区域报表,被封装为ReportItems类,报表脚本类集成自他ReportScript。

深入阅读:

1 帮助文档 ActiveReports 8 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Script > Script for Simple Reports

2 帮助文档 ActiveReports 8 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Script > Script for Subreports

3 ActiveReports 区域报表中的事件介绍

4 ActiveReports区域报表中运行时修改报表布局实现方法

5 ActiveReports区域报表中动态列报表实现方法

 

源码: C#、VS2013、AR8


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网