找回密码
 立即注册

QQ登录

只需一步,快速开始

wahaha

社区贡献组

44

主题

101

帖子

2723

积分

社区贡献组

积分
2723

活字格认证

wahaha
社区贡献组   /  发表于:2010-3-19 15:07  /   查看:7768  /  回复:0
年度报表示例
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用户可以在ActiveReports6.0的Sample中找到Annual Report,大家可以通过运行此用例,配合以下文字介绍,学习到如何使用子报表, 区域属性, 以及图表控件来快速的建立一个年度报表。以下按照Solution Explorer窗口里的文件逐个介绍。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. 年度报表(主报表)
以下介绍的是主报表的建立, 此年度报表分为三页, 缩影如下图。


a) 报表页眉区域(第一,二页)
在这次介绍的示例中, 我们的报表页眉区域分为两页。 这里通过使用PageBreak控件将ReportHeader分成两页, 并且将ReportHeader区域的NewPage属性设置为After. 另外, 这个报表会告诉用户可以使用Label的BackColor和ForeColor属性建立需要区域视觉分辨性强的报表。

这个报表的ReportHeader区域安插了SubReport控件, 在ReportStart事件里, 使用程序编译连接至ProductSalesByCategory报表。

建议在ReportStart事件中初始化报表, 而并非是在每个区域的Format事件。

CSharp:
private void AnnualReport_ReportStart(object sender, System.EventArgs eArgs)
        {
        //Set subreport control's Report property to a new report instance
        this.srptTop10.Report = new Top10();
              this.srptProductSales.Report = new ProductSalesByCategory();
        }

VB:
Private Sub AnnualReport_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ReportStart
        'Set subreport control's Report property to a new report instance
        Me.srptTop10.Report = New Top10()
        Me.srptProductSales.Report = New ProductSalesByCategory()
End Sub

ReportHeader区域右半部分黄色背景的, 使用的是Shape控件, 通过BackColor属性设置颜色。 左半边则使用了Picture控件。



b) Detail Section 明细区域(第三页)
明细区域包含两个SubReport控件,分别连接为Top10Customers和Top10Products报表。通常,在很多报表中,明细区域会运行很多次;但在此报表中,明细区域只有Label,而并无绑定的数据,因此只会运行一次。这样的话,两个子报表就可以在ReportStart事件中被初始化。



注意:请将ReportFooter区域的是Height属性设置为0. 这是因为,除了明细区域,页眉页脚区域在一页中是同时出现的,如果不想使用页脚,则需要将其高度设置为0,而并不能够单独删除页脚。

c) GetDBPath 获取数据源
对于数据库的连接,在论坛上知识库与精华区“ActiveReports 6.0产品使用指南 (2)快速绑定数据源 ”中有详细的介绍。这里的示例使用的数据库依然为NorthWind。

2. ProductSalesByCategory 报表
这可以是一个单独的ActiveReport,但在这个年度报表示例中被使用为SubReport,其放置的区域为ReportHeader区域(第二页报表的左下角)。
?
  • 这里需要注意的是,如果建立的报表是为了使用为子报表,则这个子报表如无特别需要则不用建立页眉页脚区域,可将其ReportHeader/Footer和PageHeader/Footer拿掉。这样会缩短报表运行的过程。
  • PrintWidth属性只有2.677英寸;这个宽度正好与主报表中SubReport控件匹配。
  • 这个报表在GroupHeader区域使用Labels来呈现数据名称,在明细区域逐条打印数据。
  • 在报表周围的灰色区域, 右击选择ViewCode,可以查看编译的程序,包括数据库的绑定, 各背景色彩的设定。

CSharp:
private void ProductSalesByCategory_DataInitialize(object sender, System.EventArgs eArgs)
{
      //Create dynamic datasource using sample database
      DataDynamics.ActiveReports.DataSources.OleDBDataSource _ds = new  DataDynamics.ActiveReports.DataSources.OleDBDataSource();
      _ds.ConnectionString = &quotrovider=Microsoft.Jet.OLEDB.4.0ersist Security Info=False;Data Source=" + GetDBPath.GetPath() + @"\nwind.mdb";
      _ds.SQL = "SELECT DISTINCTROW Categories.CategoryName, Sum([Order Details Extended].ExtendedPrice) AS ProductSales FROM Categories INNER JOIN (Products INNER JOIN (Orders INNER JOIN [Order Details Extended] ON Orders.OrderID = [Order Details Extended].OrderID) ON Products.ProductID = [Order Details Extended].ProductID) ON Categories.CategoryID = Products.CategoryID WHERE (((Orders.OrderDate) Between #1/1/95# And #12/31/95#)) GROUP BY Categories.CategoryName ORDER BY Categories.CategoryName";
      this.DataSource = _ds;
}
private void Detail_Format(object sender, System.EventArgs eArgs)
{
      // Check _iRow value to see if we need to highlight the row or not.
      if(this._iRow%2 == 0)
         this.Detail.BackColor = Color.LightYellow;
      else
         this.Detail.BackColor = Color.Transparent;
         this._iRow++;
}

VB:
Private Sub ProductSalesByCategory_DataInitialize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DataInitialize
      'Create dynamic datasource using sample database
      Dim _ds As New DataDynamics.ActiveReports.DataSources.OleDBDataSource()
      _ds.ConnectionString = &quotrovider=Microsoft.Jet.OLEDB.4.0ersist Security Info=False;Data Source=" + GetDBPath.GetPath() + "\nwind.mdb"
      _ds.SQL = "SELECT DISTINCTROW Categories.CategoryName, Sum([Order Details Extended].ExtendedPrice) AS ProductSales FROM Categories INNER JOIN (Products INNER JOIN (Orders INNER JOIN [Order Details Extended] ON Orders.OrderID = [Order Details Extended].OrderID) ON Products.ProductID = [Order Details Extended].ProductID) ON Categories.CategoryID = Products.CategoryID WHERE (((Orders.OrderDate) Between #1/1/95# And #12/31/95#)) GROUP BY Categories.CategoryName ORDER BY Categories.CategoryName"
      Me.DataSource = _ds
End Sub

Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles Detail.Format
      ' Check _iRow value to see if we need to highlight the row or not.
      If Me._iRow Mod 2 = 0 Then
         Me.Detail.BackColor = Color.LightYellow
      Else
         Me.Detail.BackColor = Color.Transparent
      End If
      Me._iRow = Me._iRow + 1
End Sub

3. StartupForm 起始表格
这个表格使用了ActiveReports的Viewer控件。 将Viewer的Dock属性设置为Fill,表格在运行时便会自动调整大小。 用户可以右击选择ViewCode来查看程序。

4. Top10Customers 报表和 Top10Products 报表
Top10Customers和Top10Products两个报表都各自只有两个区域, 即GroupHeader和Detail。 请将两个报表的PrintWidth属性设置为3.135 inches, 这样两个报表的大小正好合适于主报表。

这两个子报表的GroupHeader区域都被Chart控件填充着。 大家可以点击图表,查看一下这个图表属性的设置。 在属性窗口的下方, 点击蓝色的Data Source, 图表绑定的数据源窗口会弹出, 在Query框中, 可以查看到如何选择前10位的SQL语句。 (对于如何在ActiveReports中建立图表,我们会在下一个主题中详细介绍)。






在每个子报表的明细区域,设置两个TextBoxes和一个Label控件, 可以右击,选择ViewCode来查看如何设置数据源, 传递数据到图表上, 以及交替设置明细部分的背景色, 以及如何设置Label的Text属性。
~~~END~~~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部