原創|使用教程|編輯:龔雪|2021-09-07 10:08:57.387|閱讀 320 次
概述:本文主要介紹如何在打印/導出時設置紙張格式,并向報表添加自定義信息,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
WinForm平臺下的DevExpress Reporting控件允許您自定義紙張格式、方向,并將自定義信息添加到報表中。請注意,以下方法適用于實現 IPrintable 接口的控件(例如 XtraGrid、XtraPivotGrid、XtraScheduler、XtraTreeList、XtraCharts、Layout Control、XtraVerticalGrid 等)。
獲取工具下載 - DevExpress WinForm v21.1
要開始學習本教程,請啟動Microsoft Visual Studio并創建一個新的Windows Forms應用程序或打開一個現有應用程序,然后運行Toolbox并將實現接口所需控件拖放到窗體上。
接下來,您可以將控件綁定到數據或手動填充它。
IPrintable接口允許您自定義打印設置并使用 PrintableComponentLink 打印控件,以下代碼演示了如何創建 PrintableComponentLink、將控件分配給其 PrintableComponentLinkBase.Component 屬性、調整其打印設置以及打印控件。
C#
using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using DevExpress.XtraPrinting.Links; using DevExpress.XtraPrintingLinks; //... public partial class Form1 : XtraForm { //... private void gridControl1_Load(object sender, EventArgs e) { PreviewPrintableComponent(gridControl1, gridControl1.LookAndFeel); } void PreviewPrintableComponent(IPrintable component, UserLookAndFeel lookAndFeel) { // Create a link that will print a control. PrintableComponentLink link = new PrintableComponentLink() { PrintingSystemBase = new PrintingSystemBase(), Component = component, Landscape = true, PaperKind = PaperKind.A5, Margins = new Margins(20,20,20,20) }; // Show the report. link.ShowRibbonPreview(lookAndFeel); } }
VB.NET
Imports DevExpress.LookAndFeel Imports DevExpress.XtraEditors Imports DevExpress.XtraPrinting Imports DevExpress.XtraPrinting.Links Imports DevExpress.XtraPrintingLinks '... Public Partial Class Form1 Inherits XtraForm '... Private Sub gridControl1_Load(sender As Object, e As EventArgs) PreviewPrintableComponent(gridControl1, gridControl1.LookAndFeel) End Sub Private Sub PreviewPrintableComponent(component As IPrintable, lookAndFeel As UserLookAndFeel) ' Create a link that will print a control. Dim link As New PrintableComponentLink() With { _ .PrintingSystemBase = New PrintingSystem(), _ .Component = component, _ .Landscape = True, _ .PaperKind = PaperKind.A5, _ .Margins = New Margins(20, 20, 20, 20) _ } ' Show the report. link.ShowRibbonPreview(lookAndFeel) End Sub End Class
創建報表頁眉或者頁腳來向報表添加自定義信息,訂閱CreateReportHeader事件來添加報表頭,如下所示。
C#
using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using DevExpress.XtraPrinting.Links; using DevExpress.XtraPrintingLinks; //... public partial class Form1 : XtraForm { //... void PreviewPrintableComponent(IPrintable component, UserLookAndFeel lookAndFeel) { // Create a link that will print a control. //... // Subscribe to the CreateReportHeaderArea event used to generate the report header. link.CreateReportHeaderArea += link_CreateReportHeaderArea; // Show the report. link.ShowRibbonPreview(lookAndFeel); } }
VB.NET
Imports DevExpress.LookAndFeel Imports DevExpress.XtraEditors Imports DevExpress.XtraPrinting Imports DevExpress.XtraPrinting.Links Imports DevExpress.XtraPrintingLinks '... Public Partial Class Form1 Inherits XtraForm '... Private Sub PreviewPrintableComponent(component As IPrintable, lookAndFeel As UserLookAndFeel) ' Create a link that will print a control. '... ' Subscribe to the CreateReportHeaderArea event used to generate the report header. AddHandler link.CreateReportHeaderArea, AddressOf Link_CreateReportHeaderArea ' Show the report. link.ShowRibbonPreview(lookAndFeel) End Sub End Class
按如下方式處理 CreateReportHeader 事件。
C#
using System.Drawing; using DevExpress.XtraPrinting; private void link_CreateReportHeaderArea(object sender, CreateAreaEventArgs e) { string reportHeader = "Categories Report"; e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center); e.Graph.Font = new Font("Tahoma", 14, FontStyle.Bold); RectangleF rec = new RectangleF(0, 0, e.Graph.ClientPageSize.Width, 50); e.Graph.DrawString(reportHeader, Color.Black, rec, BorderSide.None); }
VB.NET
Imports System.Drawing Imports DevExpress.XtraPrinting Private Sub link_CreateReportHeaderArea(ByVal sender As System.Object, _ ByVal e As CreateAreaEventArgs) _ Handles PrintableComponentLink1.CreateReportHeaderArea Dim reportHeader As String = "Categories Report" e.Graph.StringFormat = New BrickStringFormat(StringAlignment.Center) e.Graph.Font = New Font("Tahoma", 14, FontStyle.Bold) Dim rec As RectangleF = New RectangleF(0, 0, e.Graph.ClientPageSize.Width, 50) e.Graph.DrawString(reportHeader, Color.Black, rec, BorderSide.None) End Sub
下圖說明了包含指定打印選項和附加自定義信息的結果報表。
除了打印預覽窗口中提供的導出功能外,您還可以通過PrintableComponentLink對象導出報表。
C#
PrintableComponentLink link = new PrintableComponentLink(); link.ExportToPdf(@"c:\gridcontrol.pdf");
VB.NET
Dim link As New PrintableComponentLink() link.ExportToPdf("c:\gridcontrol.pdf")
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
更多產品正版授權詳情及優惠,歡迎咨詢
DevExpress技術交流群4:715863792 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網