【Stimulsoft Reports.WPF教程】运行时创建报表

【下载Stimulsoft Reports.WPF最新版本】

此示例项目显示了在运行时创建报表的可能性。使用代码创建报表后,可以在WPF查看器中显示它。首先,初始化组件并加载未来报表的数据:

private System.Data.DataSet dataSet1 = new System.Data.DataSet();
 
public Window1()
{
    StiOptions.Wpf.CurrentTheme = StiOptions.Wpf.Themes.Office2013Theme;
    Stimulsoft.Report.Wpf.StiThemesHelper.LoadTheme(this);
    InitializeComponent();
 
    dataSet1.ReadXmlSchema("..\\..\\Data\\Demo.xsd");
    dataSet1.ReadXml("..\\..\\Data\\Demo.xml");
 
    dataSet1.DataSetName = "Demo";
}

所有必要的操作都可以在应用程序的Button_Click 事件中完成。首先,创建新的报表对象,注册数据并将其与报表dictionary同步:

private void Button_Click(object sender, RoutedEventArgs e)
{
    StiReport report = new StiReport();
 
    //Add data to datastore
    report.RegData(dataSet1);
 
    //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组件添加页脚页带,该组件在页面底部显示文本。最后一个操作是WPF查看器的调用,它将自动构建和显示报表:

...
    //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);
 
    report.ShowWithWpf();
}

示例代码的结果如下图所示:

Stimulsoft

查看原文,下载示例

猜你喜欢

转载自blog.csdn.net/xiaochuachua/article/details/82801952
今日推荐