在C#下,有一个类库System.CodeDom.Compiler,非常强大。如,可用来动态生成dll,因此可以实现动态表达式、报表等函数,非常方便。AR的Section报表(区域报表)中有脚本,是通过System.CodeDom.Compiler实现的编译为dll,具体下面的实战步骤。
Step 1: 新建一个WindowsFormsApplication1的exe应用程序
Step 2: 新建SectionReport1.rpx区域报表,并在其Detail内拖入一个TextBox控件
Step 3: 新建一个Model类,代码如下:
- publicpartialclassModule1
- {
- publicstring strCD;
- publicstring PrintReport()
- {
- strCD ="C#";
- return strCD;
- }
- }
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 需要添加命名空间
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; }
结果如下:
说了半天,怎么没有看到Compiler的概念呢。
请看代码:
- namespace Script
- {
- using GrapeCity.ActiveReports;
- using GrapeCity.ActiveReports.SectionReportModel;
- using GrapeCity.ActiveReports.Data;
- using GrapeCity.ActiveReports.Document.Section;
- using System;
- using System.Drawing;
- using System.Diagnostics;
- using System.Data;
- using System.Collections;
- using System.Collections.Generic;
- publicclassReportItems
- {
- public GrapeCity.ActiveReports.SectionReport rpt; //
- public GrapeCity.ActiveReports.SectionReportModel.PageHeader PageHeader;
- public GrapeCity.ActiveReports.SectionReportModel.Detail Detail;
- public GrapeCity.ActiveReports.SectionReportModel.TextBox TextBox1;
- public GrapeCity.ActiveReports.SectionReportModel.TextBox TextBox2;
- public GrapeCity.ActiveReports.SectionReportModel.PageFooter PageFooter;
- }
- //
- publicclassReportScript : ReportItems
- {
- WindowsFormsApplication1.Module1 mo =new WindowsFormsApplication1.Module1();
- publicvoid ActiveReport_DataInitialize()
- {
- string s = mo.strCD;
- mo.PrintReport();
- ((TextBox)rpt.Sections["Detail"].Controls["TextBox1"]).Text = mo.strCD;
- }
- }
- }
在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
4 ActiveReports区域报表中运行时修改报表布局实现方法
源码: C#、VS2013、AR8