Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E2305。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |
这个示例展示了如何重写报表的 打印预览 窗体 (由 ReportPrintTool 类提供) 中的导出报表文档为图像文件的功能。 这是有用的,例如,如果需要静默地把报表保存到磁盘上预定义的位置,使用预定义的图像格式设置,并且不需要与最终用户交互时。
要完成此任务,则接管打印预览窗体中的 PrintingSystemCommand.ExportGraphic 命令。 注意,要防止标准的导出处理被调用,则只需要启用 handled 属性。
这种方法可以用于重写任何其他可以由最终用户在打印预览窗体中执行的标准操作。 可用的命令已在 PrintingSystemCommand 枚举中列出。
C# | 复制代码 |
---|---|
(Form1.cs) using System; using System.Windows.Forms; using System.Drawing.Imaging; using DevExpress.XtraPrinting; using DevExpress.XtraReports.UI; // ... private void button1_Click(object sender, EventArgs e) { // Create a report instance, assigned to a Print Tool. ReportPrintTool pt = new ReportPrintTool(new XtraReport1()); // Generate the report's document. This step is required // to activate its PrintingSystem and access it later. pt.Report.CreateDocument(false); // Override the ExportGraphic command. pt.PrintingSystem.AddCommandHandler(new ExportToImageCommandHandler()); // Show the report's print preview. pt.ShowPreview(); } public class ExportToImageCommandHandler : ICommandHandler { public virtual void HandleCommand(PrintingSystemCommand command, object[] args, IPrintControl control, ref bool handled) { if (!CanHandleCommand(command, control)) return; // Export a document to png. control.PrintingSystem.ExportToImage("C:\\Report.png", ImageFormat.Png); // Set handled to true to avoid the standard exporting procedure to be called. handled = true; } public virtual bool CanHandleCommand(PrintingSystemCommand command, IPrintControl control) { // This handler is used for the ExportGraphic command. return command == PrintingSystemCommand.ExportGraphic; } } |
Visual Basic | 复制代码 |
---|---|
(Form1.vb) Imports System Imports System.Windows.Forms Imports System.Drawing.Imaging Imports DevExpress.XtraPrinting Imports DevExpress.XtraReports.UI ' ... Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click ' Create a report instance, assigned to a Print Tool. Dim pt As New ReportPrintTool(New XtraReport1()) ' Generate the report's document. This step is required ' to activate its PrintingSystem and access it later. pt.Report.CreateDocument(False) ' Override the ExportGraphic command. pt.PrintingSystem.AddCommandHandler(New ExportToImageCommandHandler()) ' Show the report's print preview. pt.ShowPreview() End Sub Public Class ExportToImageCommandHandler Implements ICommandHandler Public Overridable Sub HandleCommand(ByVal command As PrintingSystemCommand, _ ByVal args() As Object, ByVal control As IPrintControl, _ ByRef handled As Boolean) Implements ICommandHandler.HandleCommand If (Not CanHandleCommand(command, control)) Then Return End If ' Export a document to png. control.PrintingSystem.ExportToImage("C:\Report.png", ImageFormat.Png) ' Set handled to true to avoid the standard exporting procedure to be called. handled = True End Sub Public Overridable Function CanHandleCommand(ByVal command As PrintingSystemCommand, _ ByVal control As IPrintControl) As Boolean Implements ICommandHandler.CanHandleCommand ' This handler is used for the ExportGraphic command. Return command = PrintingSystemCommand.ExportGraphic End Function End Class |