本指南演示了如何批量打印几个报表,而不是每次发送一个报表到打印机。 此外,在本例中,为第一个报表调用“打印”对话框,其他报表被打印时不出现任何对话框,而是使用相同的打印设置。 注意,为完成此任务,需要使用 XtraReport.PrintDialogXtraReport.Print 方法,也要接管报表的 XtraReport.PrintingSystemPrintingSystemBase.StartPrint 事件。

注意,为了使此示例正确地工作,三个命名为 XtraReport1XtraReport2XtraReport3 的报表必须被 添加 到项目中。

下列代码演示了如何完成此任务。

C#CopyCode image复制代码
using System.Drawing.Printing;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...

private PrinterSettings prnSettings;
private void button1_Click(object sender, System.EventArgs e) {
   XtraReport1 report1 = new XtraReport1();
   XtraReport[] reports = new XtraReport[] {new XtraReport2(), new XtraReport3()}; 
   report1.PrintingSystem.StartPrint += 
       new PrintDocumentEventHandler(report1StartPrintEventHandler);
   foreach(XtraReport report in reports){
      report.PrintingSystem.StartPrint += new PrintDocumentEventHandler
          (reportsStartPrintEventHandler);
   }
   
   report1.PrintDialog();
   foreach(XtraReport report in reports){
      report.Print();
   }
}

private void report1StartPrintEventHandler(object sender, PrintDocumentEventArgs e){
   prnSettings = e.PrintDocument.PrinterSettings;
}

private void reportsStartPrintEventHandler(object sender, PrintDocumentEventArgs e){
   int pageCount = e.PrintDocument.PrinterSettings.ToPage;
   e.PrintDocument.PrinterSettings = prnSettings;

   // Do this if your reports contain a different number of pages,
   // and you always need to print all pages in a report.
   e.PrintDocument.PrinterSettings.ToPage = pageCount;
}
Visual BasicCopyCode image复制代码
Imports System.Drawing.Printing
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
' ...

Dim PrnSettings As New PrinterSettings()

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ 
Handles Button1.Click
   Dim report1 As New XtraReport1()
   Dim reports() As XtraReport = {New XtraReport2(), New XtraReport3()}

   AddHandler report1.PrintingSystem.StartPrint, AddressOf report1StartPrintEventHandler

   Dim report As XtraReport
   For Each report In reports
      AddHandler report.PrintingSystem.StartPrint, AddressOf reportsStartPrintEventHandler
   Next report

   report1.PrintDialog()

   For Each report In reports
      report.Print()
   Next report
End Sub

Private Sub report1StartPrintEventHandler(ByVal sender As Object, _ 
ByVal e As PrintDocumentEventArgs)
   PrnSettings = e.PrintDocument.PrinterSettings
End Sub

Private Sub reportsStartPrintEventHandler(ByVal sender As Object, _ 
ByVal e As PrintDocumentEventArgs)
   Dim PageCount As Integer = e.PrintDocument.PrinterSettings.ToPage
   e.PrintDocument.PrinterSettings = PrnSettings

   ' Do this if your reports contain a different number of pages,
   ' and you always need to print all pages in a report.
   e.PrintDocument.PrinterSettings.ToPage = PageCount
End Sub

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E1765。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅