本帖最后由 alizee1025 于 2019-5-10 13:48 编辑
通常,像一些文档类的报表,如质检报告,合同,这类报表是由多个独立的部分组成。这些内容页,可为固定的部分,也有根据不同情况调整的部分。如质检报告,都会有封皮,尾页,检测内容等,检测内容可根据所检测的产品而调整,那这个时候,如果每次都重新设计如此体量的大报表,不仅耗费时间,精力,人力,而且还很枯燥,因为总是重复的设计。现在有了 ReportCombiner 类,就可以任意合并子报表了,再也不会像原来的,导出为PDF 后,再进行合并,现在可以将多个报表,拼成一个报表,并随意导出你想要的格式。
除了合并之外,我们也可以使用这个方法,给报表内容中加分页,这样在创建 Word 类报表的时候,就更具有 Word 风格。
接下来,我们来学习下,如何使用 ReportCombiner 类。
1. 首先需要将 GrapeCity.ActiveReports.Core.Rendering dll 添加到项目中。如果需要将合并的报表导出为pdf格式,也需要添加此引用:GrapeCity.ActiveReports.Export.Pdf
请在安装目录下查找:C:\Program Files (x86)\Common Files\GrapeCity\ActiveReports 13
2. 示例代码如下:
VB 代码:
- Viewer1.LoadDocument(Application.StartupPath & "\..\..\PageReport1.rdlx")
- Dim combiner = New GrapeCity.ActiveReports.ReportsCore.Tools.ReportCombiner()
- Dim r1 = New GrapeCity.ActiveReports.PageReport()
- r1.Load(New System.IO.FileInfo("c:\temp\Report1.rdlx"))
- Dim r2 = New GrapeCity.ActiveReports.PageReport()
- r2.Load(New System.IO.FileInfo("c:\temp\Report2.rdlx"))
- Dim r3 = New GrapeCity.ActiveReports.PageReport()
- r3.Load(New System.IO.FileInfo("c:\temp\Report3.rdlx"))
- combiner.AddReport(r1)
- Dim options = New GrapeCity.ActiveReports.ReportsCore.Tools.LocationOptions
- options.Gap = "5in" 'adds a 5 inch gap from the first report. By default this gap is 1 inch.
- options.PageBreakBefore = True 'adds a page break.
- combiner.AddReport(r2, options)
- combiner.AddReport(r3)
- 'PDF Rendering extension
- Dim pdfRe = New GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension()
- Dim provider = New GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(New System.IO.DirectoryInfo("c:\temp"), "CombinedReport")
- combiner.BuildReport().Document.Render(pdfRe, provider)
-
复制代码
C# 代码:
- viewer1.LoadDocument(Application.StartupPath + @"\..\..\PageReport1.rdlx");
- var combiner = new GrapeCity.ActiveReports.ReportsCore.Tools.ReportCombiner();
- var r1 = new GrapeCity.ActiveReports.PageReport();
- r1.Load(new System.IO.FileInfo(@"c:\temp\Report1.rdlx"));
- var r2 = new GrapeCity.ActiveReports.PageReport();
- r2.Load(new System.IO.FileInfo(@"c:\temp\Report2.rdlx"));
- var r3 = new GrapeCity.ActiveReports.PageReport();
- r3.Load(new System.IO.FileInfo(@"c:\temp\Report3.rdlx"));
- combiner.AddReport(r1);
- combiner.AddReport(r2, new LocationOptions() { PageBreakBefore = true, Gap = "5in" }); //adds second report after a page break and a 5 inch gap from the first report. By default this gap is 1 inch.
- combiner.AddReport(r3);
- //PDF Rendering extension
- var pdfRe = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
- var provider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(new System.IO.DirectoryInfo(@"c:\temp"), "CombinedReport");
- combiner.BuildReport().Document.Render(pdfRe, provider);
-
复制代码
除此之外,也可以进行以下操作:
1. 如果想在 r1 后 插入 报表 r4, 可以使用 索引插入:
VB:
- combiner.Insert(1, r4, New GrapeCity.ActiveReports.ReportsCore.Tools.LocationOptions())
- report = combiner.BuildReport()
-
复制代码
c#:
- combiner.Insert(1, r4, new LocationOptions());
- report = combiner.BuildReport();
-
复制代码
2. 也可直接添加一组报表
vb:
- Dim reports As IEnumerable(Of GrapeCity.ActiveReports.PageReport) = {r1, r2, r3, r4}
- combiner.AddRange(reports, New GrapeCity.ActiveReports.ReportsCore.Tools.LocationOptions())
- report = combiner.BuildReport()
-
复制代码
C#
- combiner.AddRange(new PageReport[] {r1, r2, r3, r4 }, new LocationOptions())
- report = combiner.BuildReport();
-
复制代码
删除指定位置报表
VB:
C#
- combiner.RemoveAt(1);
- report = combiner.BuildReport();
-
复制代码
如果想删除组合报表中的所有 报表2,也可以的:
VB:
- combiner.RemoveAt(R2)
- report = combiner.BuildReport()
-
复制代码
C#
- combiner.RemoveAt(1);
- report = combiner.BuildReport();
-
复制代码
|
|