← 返回所有博客文章

ActiveReports 7的扩展API可以让Visual Basic和C#开发人员完全控制报表处理引擎,从而以满足业务的需求。

  • Data Retrieval
  • Data Transformation
  • Layout
  • Rendering

这些API使开发人员可以从头开始创建自己的报表,并拥有报表创建的完整控制权--在运行时创建报表。

运行时创建报表的步骤:

  • 创建报表实例
  • 动态添加报表控件和报表区域
  • 修改报表区域和报表控件布局、大小、颜色等
  • 绑定报表数据源

    Results2

    完整源码如下:

    Code Snippet
    1. privatevoid Form1_Load(object sender, EventArgs e)
    2.         {
    3.             rpt =newSectionReport();
    4.  
    5.             //Adding Page Header/Footer sections
    6.             rpt.Sections.InsertPageHF();
    7.             rpt.Sections[0].BackColor =Color.LightGray;
    8.  
    9.             //Adding Detail section
    10.             rpt.Sections.Insert(1, newDetail());
    11.             rpt.Sections[1].BackColor =Color.PeachPuff;
    12.             rpt.Sections[1].Height =1.5f;
    13.  
    14.             //Adding label to display first column's name
    15.             GrapeCity.ActiveReports.SectionReportModel.Label lblCategoryID =new GrapeCity.ActiveReports.SectionReportModel.Label();
    16.             lblCategoryID.Location =newPointF(0, 0.05F);
    17.             lblCategoryID.Text ="Category ID";
    18.             lblCategoryID.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Center;
    19.             lblCategoryID.Font =new System.Drawing.Font("Arial", 10, FontStyle.Bold);
    20.             rpt.Sections[0].Controls.Add(lblCategoryID);
    21.  
    22.             //Adding label to display second column's name
    23.             GrapeCity.ActiveReports.SectionReportModel.Label lblCategoryName =new GrapeCity.ActiveReports.SectionReportModel.Label();
    24.             lblCategoryName.Location =newPointF(1.459f, 0.05f);
    25.             lblCategoryName.Size =newSizeF(1.094f,0.2f);
    26.             lblCategoryName.Text ="Category Name";
    27.             lblCategoryName.Font =new System.Drawing.Font("Arial", 10, FontStyle.Bold);
    28.             rpt.Sections[0].Controls.Add(lblCategoryName);
    29.  
    30.             //Adding label to display third column's name
    31.             GrapeCity.ActiveReports.SectionReportModel.Label lblDescription =new GrapeCity.ActiveReports.SectionReportModel.Label();
    32.             lblDescription.Location =newPointF(3.114f, 0.05f);
    33.             lblDescription.Text ="Description";
    34.             lblDescription.Font =new System.Drawing.Font("Arial", 10, FontStyle.Bold);
    35.             rpt.Sections[0].Controls.Add(lblDescription);
    36.  
    37.             //Adding label to display fourth column's name
    38.             GrapeCity.ActiveReports.SectionReportModel.Label lblPicture =new GrapeCity.ActiveReports.SectionReportModel.Label();
    39.             lblPicture.Location =newPointF(5.219f, 0.05f);
    40.             lblPicture.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Center;
    41.             lblPicture.Text ="Picture";
    42.             lblPicture.Font =new System.Drawing.Font("Arial", 10, FontStyle.Bold);
    43.             rpt.Sections[0].Controls.Add(lblPicture);
    44.  
    45.             //Adding Textbox to display first column's records
    46.             GrapeCity.ActiveReports.SectionReportModel.TextBox txtCategoryID =new GrapeCity.ActiveReports.SectionReportModel.TextBox();
    47.             txtCategoryID.Location =newPointF(0,0);
    48.             txtCategoryID.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Center;
    49.             rpt.Sections[1].Controls.Add(txtCategoryID);
    50.  
    51.             //Adding Textbox to display second column's records
    52.             GrapeCity.ActiveReports.SectionReportModel.TextBox txtCategoryName =new GrapeCity.ActiveReports.SectionReportModel.TextBox();
    53.             txtCategoryName.Location =newPointF(1.459f,0);
    54.             rpt.Sections[1].Controls.Add(txtCategoryName);
    55.  
    56.             //Adding Textbox to display third column's records
    57.             GrapeCity.ActiveReports.SectionReportModel.TextBox txtDescription =new GrapeCity.ActiveReports.SectionReportModel.TextBox();
    58.             txtDescription.Location =newPointF(3.114f,0);
    59.             rpt.Sections[1].Controls.Add(txtDescription);
    60.  
    61.             //Adding Picture control to display image
    62.             GrapeCity.ActiveReports.SectionReportModel.Picture picture =newPicture();
    63.             picture.Location =newPointF(5.219f,0);
    64.             rpt.Sections[1].Controls.Add(picture);
    65.  
    66.             // Setting report's data source
    67.             conn =newOleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\rogerwang\Documents\ComponentOne Samples\ActiveReports Developer 7\Data\NWIND.mdb;Persist Security Info=False");
    68.             System.Data.OleDb.OleDbCommand cmd =new System.Data.OleDb.OleDbCommand("SELECT * FROM Categories", conn);
    69.             conn.Open();
    70.             reader = cmd.ExecuteReader();
    71.             rpt.DataSource = reader;
    72.  
    73.             // Assigning DataField properties of controls in the detail section
    74.             txtCategoryID.DataField ="CategoryID";
    75.             txtCategoryName.DataField ="CategoryName";
    76.             txtDescription.DataField ="Description";
    77.             picture.DataField ="Picture";   
    78.  
    79.             rpt.Run();
    80.             this.viewer1.Document = rpt.Document;
    81.             reader.Close();
    82.             conn.Close();
    83.  
    84.         }