合并多个C1Reports for WPF/WinForms的内容

发布时间:2012/09/30 00:09 发布者:admin

返回博客中心

C1Report提供了非常灵活的方式来创建和保持多个报表到一个报表定义文件(.XML)中。然而,默认情况下每次预览时只能查看一个报表的内容。本文将介绍如果一次预览多个报表的内容。

主要实现方法是同时加载多个报表,进行内容合并,然后,被加载的每一页报表内容保存到一个图片数组中。这些图片最好将作为一个RenderImage对象添加到C1PrintDocument中,一旦这些图片加载完成,C1PrintDocument也就在预览控件中显示完成。

实现代码如下:

private void Form1_Load(object sender, EventArgs e)
{
      // load the reports
      c1Report1.Load("Report1.xml", "ReportName");
      c1Report2.Load("Report2.xml", "ReportName");
      this.c1Report1.Render();
      this.c1Report2.Render();
 
      // the margins are set to zero to compensate for the margins already present in the reports
      c1PrintDocument1.PageLayout.PageSettings.LeftMargin = "0.0in";
      c1PrintDocument1.PageLayout.PageSettings.TopMargin = "0.0in";
      int i;
      int count=c1Report1.GetPageCount() + c1Report2.GetPageCount();
      Pages = new System.Drawing.Imaging.Metafile[count];
 
      // get pageimages of report1
      foreach (System.Drawing.Imaging.Metafile Page in this.c1Report1.GetPageImages())
      {
           Pages.SetValue(Page, index);
           index++;
      }
 
      // get pageimages of report2
      foreach (System.Drawing.Imaging.Metafile Page in this.c1Report2.GetPageImages())
      {
           Pages.SetValue(Page, index);
           index++;
      }
 
      // add images from array to PrintDocument as RenderImages
      for (i = 0; i <= (Pages.Length - 1); i++)
      {
           RenderImage PageImage = new RenderImage(Pages[ i ]);
           this.c1PrintDocument1.Body.Children.Add(PageImage);
           this.c1PrintDocument1.Reflow();
      }
 
      // Preview the C1PrintDocument
 
      // for winform Application
      c1PrintPreviewControl1.Document = c1PrintDocument1;
      // for WPF Application
      c1DocumentViewer1.Document = c1PrintDocument1.FixedDocumentSequence;
 }

需要注意的是,如果报表数据量较大时,这种方法可能会因为存在显示延时。

源码下载:VS2010 + C1 2012V2  C1Report_Merging.zip


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网