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

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

发布于 2012/09/30 00:00

ComponentOne Enterprise

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 

ComponentOne Enterprise | 下载试用

ComponentOne 是一套专注于企业 .NET开发、支持 .NET Core 平台,并完美集成于 Visual Studio 的第三方控件集,包含 300 多种 .NET开发控件,提供表格数据管理、数据可视化、报表和文档、日程安排、输入和编辑、导航和布局、系统提升工具等七大功能,被誉为“.NET开发的‘瑞士军刀’”。

ComponentOne 为您提供专业的产品咨询服务,并由技术支持工程师为您1对1解答。>> 发帖提问

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态