Visual Studio 2022:在单个Crystal Reports查看器/文件中展示多报表
问题
我希望在单个Crystal Reports查看器中显示多份报表,或生成包含多份报表的单个文件,但不清楚具体实现方法。
目前我已实现单份报表在Crystal Reports查看器中的显示功能,代码如下:
private void btnDisplayInViewer_Click(object sender, EventArgs e) { String Application_Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); cryRpt = new ReportDocument(); cryRpt.Load(Application_Path + "\Order_Report.rpt"); (cryRpt.ReportDefinition.ReportObjects["tOrder_Ref_Display"] as TextObject).Text = "1"; crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); }
同时也实现了将单份报表输出到文件的功能,代码如下:
private void btnToPrinter_Click(object sender, EventArgs e) { String Application_Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); cryRpt = new ReportDocument(); cryRpt.Load(Application_Path + "\Order_Report.rpt"); (cryRpt.ReportDefinition.ReportObjects["tOrder_Ref_Display"] as TextObject).Text = "1"; cryRpt.PrintToPrinter(1/*Copies*/, true/*collated*/, 0/*Start Page*/, 0/*End Page*/); }
请问有无示例代码可实现上述任一场景的多报表合并,以在单个查看器/文件中展示?
解决方案
我使用PDFSharp组件实现了多份Crystal Report合并为单个PDF文件的需求,具体操作如下:
1. 安装PDFSharp
通过Visual Studio的工具 > NuGet包管理器 > 管理解决方案的NuGet包搜索并安装PDFSharp组件。
2. 添加引用与using指令
确保项目已添加PDFSharp相关引用,并在代码文件顶部添加以下using指令:
using PdfSharp; using PdfSharp.Pdf.IO; using PdfSharp.Pdf; using PdfSharp.Drawing; using System.Diagnostics; using System.IO;//File.Delete
3. 示例代码
以下代码可循环生成多份报表,导出为临时PDF后合并成单个文档:
private void btnExport_Multiple_PDFs_Click(object sender, EventArgs e) { String Destination_File_Location_Name = "c:\\Temp\\"; String PDF_File_Extension = ".pdf"; String Report_Name = "Report.rpt"; #region Nested method to add pages. void CopyPages(PdfDocument from, PdfDocument to) { for (int i = 0; i < from.PageCount; i++) { to.AddPage(from.Pages[i]); } } #endregion Nested method to add pages. ExportOptions CrExportOptions; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions(); String Application_Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); PdfDocument outPdf = new PdfDocument(); #region Loop through a number of times to generate multiple reports. for (int m = 1; m < 4; m++) { CrDiskFileDestinationOptions.DiskFileName = Destination_File_Location_Name + m.ToString() + PDF_File_Extension; cryRpt = new ReportDocument(); cryRpt.Load(Application_Path + "\\" + Report_Name); (cryRpt.ReportDefinition.ReportObjects["tOrder_Ref_Display"] as TextObject).Text = m.ToString(); CrExportOptions = cryRpt.ExportOptions; { CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; } cryRpt.Export(); PdfDocument one = PdfReader.Open(CrDiskFileDestinationOptions.DiskFileName, PdfDocumentOpenMode.Import); CopyPages(one, outPdf); // Remove the Crystal Report File thats been exported File.Delete(CrDiskFileDestinationOptions.DiskFileName); } #endregion Loop through a number of times to generate multiple reports. #region Check Temp Directory Exists. If not the create. if (!Directory.Exists(Destination_File_Location_Name)) { Directory.CreateDirectory(Destination_File_Location_Name); } #endregion Check Temp Directory Exists. If not the create. // Save the Output PDF to file outPdf.Save(Destination_File_Location_Name + PDF_File_Extension); // Preview the PDF Process.Start(Destination_File_Location_Name + PDF_File_Extension); }
功能说明
代码通过循环生成多份Crystal Report,将每份报表导出为临时PDF文件,再通过PDFSharp将临时PDF的页面合并到一个最终PDF中,最后自动删除临时文件并打开合并后的PDF文档。
内容的提问来源于stack exchange,提问作者Yuppski
相关产品推荐
相关产品推荐

