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