要添加Web查看器,您可以使用以下ASPX页面。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="Runtime_Report_Creation.Report" %> <%@ Register Assembly="Stimulsoft.Report.Web" Namespace="Stimulsoft.Report.Web" TagPrefix="cc1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Runtime Report Creation</title> </head> <body> <form id="form1" runat="server"> <cc1:StiWebViewer ID="StiWebViewer1" runat="server" /> </form> </body> </html>
所有必要的操作都可以在网页的Page_Load事件中完成。首先,创建新的报表对象,读取数据并将其连接到报表字典。
protected void Page_Load(object sender, EventArgs e) { DataSet data = new DataSet(); data.ReadXmlSchema(Server.MapPath(@"Data\Demo.xsd")); data.ReadXml(Server.MapPath(@"Data\Demo.xml")); StiReport report = new StiReport(); report.RegData(data); //Fill dictionary report.Dictionary.Synchronize(); ...
然后我们需要创建报表组件。首先,使用将显示标题的Text组件将Header Band添加到报表页面。
... StiPage page = report.Pages[0]; //Create HeaderBand StiHeaderBand headerBand = new StiHeaderBand(); headerBand.Height = 0.5; headerBand.Name = "HeaderBand"; page.Components.Add(headerBand); //Create text on header StiText headerText = new StiText(new RectangleD(0, 0, 5, 0.5)); headerText.Text = "CompanyName"; headerText.HorAlignment = StiTextHorAlignment.Center; headerText.Name = "HeaderText"; headerText.Brush = new StiSolidBrush(System.Drawing.Color.LightGreen); headerBand.Components.Add(headerText); ...
接下来,使用添加表达式的Text组件添加数据带。此表达式组合了客户数据源中的行号和CompanyName数据列。
... //Create Databand StiDataBand dataBand = new StiDataBand(); dataBand.DataSourceName = "Customers"; dataBand.Height = 0.5; dataBand.Name = "DataBand"; page.Components.Add(dataBand); //Create text StiText dataText = new StiText(new RectangleD(0, 0, 5, 0.5)); dataText.Text = "{Line}.{Customers.CompanyName}"; dataText.Name = "DataText"; dataBand.Components.Add(dataText); ...
最后,使用Text组件添加页脚页带,该组件在页面底部显示文本。最后一个操作是将创建的报表分配给WebViewer,WebViewer将自动构建和显示它。
... //Create FooterBand StiFooterBand footerBand = new StiFooterBand(); footerBand.Height = 0.5; footerBand.Name = "FooterBand"; page.Components.Add(footerBand); //Create text on footer StiText footerText = new StiText(new RectangleD(0, 0, 5, 0.5)); footerText.Text = "Count - {Count()}"; footerText.HorAlignment = StiTextHorAlignment.Right; footerText.Name = "FooterText"; footerText.Brush = new StiSolidBrush(System.Drawing.Color.LightGreen); footerBand.Components.Add(footerText); StiWebViewer1.Report = report; }
示例代码的结果如下图所示: