DX打印XtraGrid

场景:
        使用XtraGrid打印各种报表,要求能显示表头、条件,设置纸张类型等。
代码:
      

/// <summary>   
        /// DX打印   
        /// </summary>   
        ///
        public class DXPrintHelper
        {
            private PrintingSystem printSystem; private string mReportName; private string mCondition;
            /// <summary>       
            /// 直接打印 (A4)       
            /// </summary>       
            /// <param name="Printable"></param>       
            ///
            public DXPrintHelper(IPrintable Printable, bool landscape)
            {
                printSystem = new PrintingSystem();
                mReportName = "";
                mCondition = "";
                PrintableComponentLink pcl = new PrintableComponentLink();
                pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
                pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
                pcl.Component = Printable;
                pcl.Landscape = landscape; printSystem.Links.Add(pcl);
                pcl.CreateDocument();
                PrinterSettingsUsing pst = new PrinterSettingsUsing();
                pst.UseMargins = false;
                pst.UsePaperKind = false;
                printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
                printSystem.PageSettings.PaperName = "A4";
                printSystem.PageSettings.Landscape = landscape;
                printSystem.PageSettings.LeftMargin = 2;
                printSystem.PageSettings.RightMargin = 2;
                printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
            }
            /// <summary>       
            /// 带标题的打印(A4)      
            /// /// </summary>       
            /// <param name="Printable"></param>       
            /// <param name="ReportName"></param>       
            ///
            public DXPrintHelper(IPrintable Printable, string ReportName, bool landscape)
            {
                printSystem = new PrintingSystem();
                mReportName = ReportName; mCondition = "";
                PrintableComponentLink pcl = new PrintableComponentLink();
                pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
                pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
                pcl.Component = Printable;
                pcl.Landscape = landscape;
                printSystem.Links.Add(pcl);
                pcl.CreateDocument();
                PrinterSettingsUsing pst = new PrinterSettingsUsing();
                pst.UseMargins = false;
                pst.UsePaperKind = false;
                printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
                printSystem.PageSettings.PaperName = "A4";
                printSystem.PageSettings.LeftMargin = 2;
                printSystem.PageSettings.RightMargin = 2;
                printSystem.PageSettings.Landscape = landscape;
                printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
            }
            /// <summary>      
            /// /// 带标题、查询条件打印(A4)      
            /// /// </summary>       
            /// <param name="Printable"></param>       
            /// <param name="ReportName"></param>       
            /// <param name="Condition"></param>       
            ///
            public DXPrintHelper(IPrintable Printable, string ReportName, string Condition, bool landscape)
            {
                printSystem = new PrintingSystem();
                mReportName = ReportName; mCondition = Condition;
                PrintableComponentLink pcl = new PrintableComponentLink();
                pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
                pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
                pcl.Component = Printable; pcl.Landscape = landscape; printSystem.Links.Add(pcl); pcl.CreateDocument();
                PrinterSettingsUsing pst = new PrinterSettingsUsing();
                pst.UseMargins = true;
                //pst.UsePaperKind = false;         
                printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
                printSystem.PageSettings.PaperName = "A4";
                printSystem.PageSettings.Landscape = landscape;
                printSystem.PageSettings.LeftMargin = 30;
                printSystem.PageSettings.RightMargin = 30;
                printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
            }
            /// <summary>       
            /// 自定义纸张类型打印       
            /// </summary>      
            /// /// <param name="Printable"></param>       
            /// <param name="paperKind"></param>       
            ///
            public DXPrintHelper(IPrintable Printable, PaperKind paperKind, bool landscape)
            {
                printSystem = new PrintingSystem();
                mReportName = "";
                mCondition = "";
                PrintableComponentLink pcl = new PrintableComponentLink();
                pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
                pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
                pcl.Component = Printable; pcl.Landscape = landscape;
                printSystem.Links.Add(pcl); pcl.CreateDocument();
                PrinterSettingsUsing pst = new PrinterSettingsUsing();
                pst.UseMargins = false; pst.UsePaperKind = true;
                printSystem.PageSettings.PaperKind = paperKind;
                //printSystem.PageSettings.PaperName = "A4";          
                printSystem.PageSettings.Landscape = landscape;
                printSystem.PageSettings.LeftMargin = 2;
                printSystem.PageSettings.RightMargin = 2;
                printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
            }
            /// <summary>       
            /// 自定义标题和纸张类型       
            /// </summary>       
            /// <param name="Printable"></param>       
            /// <param name="ReportName"></param>       
            /// <param name="paperKind"></param>       
            ///
            public DXPrintHelper(IPrintable Printable, string ReportName, PaperKind paperKind, bool landscape)
            {
                printSystem = new PrintingSystem();
                mReportName = ReportName;
                mCondition = "";
                PrintableComponentLink pcl = new PrintableComponentLink();
                pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
                pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
                pcl.Component = Printable;
                pcl.Landscape = landscape;
                printSystem.Links.Add(pcl);
                pcl.CreateDocument();
                PrinterSettingsUsing pst = new PrinterSettingsUsing();
                pst.UseMargins = true;
                pst.UsePaperKind = true;
                printSystem.PageSettings.PaperKind = paperKind;           
                //printSystem.PageSettings.PaperName = "A4";          
                printSystem.PageSettings.Landscape = landscape;
                printSystem.PageSettings.LeftMargin = 30;
                printSystem.PageSettings.RightMargin = 30;
                printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
            }
            /// <summary>       
            /// 自定义标题、查询条件、纸张类型       
            /// </summary>       
            /// <param name="Printable"></param>       
            /// <param name="ReportName"></param>       
            /// <param name="Condition"></param>       
            /// <param name="paperKind"></param>       
            ///
            public DXPrintHelper(IPrintable Printable, string ReportName, string Condition, PaperKind paperKind, bool landscape)
            {
                printSystem = new PrintingSystem();
                mReportName = ReportName; mCondition = Condition;
                PrintableComponentLink pcl = new PrintableComponentLink();
                pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
                pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
                pcl.Component = Printable;
                pcl.Landscape = landscape;
                printSystem.Links.Add(pcl);
                pcl.CreateDocument();
                PrinterSettingsUsing pst = new PrinterSettingsUsing();
                pst.UseMargins = true;
                pst.UsePaperKind = true;
                printSystem.PageSettings.PaperKind = paperKind;           
                //printSystem.PageSettings.PaperName = "A4";        
                printSystem.PageSettings.Landscape = landscape;
                printSystem.PageSettings.LeftMargin = 30;
                printSystem.PageSettings.RightMargin = 30;
                printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
            }
            private void link_CreateMarginalHeaderArea(object sender, DevExpress.XtraPrinting.CreateAreaEventArgs e)
            {
                if (mReportName != "")
                {
                    e.Graph.Font = new Font("宋体", 15, FontStyle.Bold);
                    e.Graph.BackColor = Color.Transparent;
                    RectangleF r = new RectangleF(0, 20, 0, e.Graph.Font.Height + 20);
                    PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mReportName, Color.Black, r, BorderSide.None);
                    brick.Alignment = BrickAlignment.Center;
                    brick.AutoWidth = true;
                }
                if (mCondition != "")
                {
                    e.Graph.Font = new Font("宋体", 10);
                    e.Graph.BackColor = Color.Transparent;
                    RectangleF r = new RectangleF(0, 50, 0, e.Graph.Font.Height + 20);
                    PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mCondition, Color.Black, r, BorderSide.None);
                    brick.Alignment = BrickAlignment.Center;
                    brick.AutoWidth = true;
                }
            }
            private void link_CreateMarginalFooterArea(object sender, CreateAreaEventArgs e)
            {
                string format = "第{0}页 共{1}页";
                e.Graph.Font = new Font("宋体", 10);
                e.Graph.BackColor = Color.Transparent;
                RectangleF r = new RectangleF(0, 5, 0, e.Graph.Font.Height + 20);
                PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, format, Color.Black, r, BorderSide.None);
                brick.Alignment = BrickAlignment.Far;
                brick.AutoWidth = true;
                brick = e.Graph.DrawPageInfo(PageInfo.DateTime, "打印时间:" + DateTime.Today.ToLongDateString(), Color.Black, r, BorderSide.None);
                brick.Alignment = BrickAlignment.Near;
                brick.AutoWidth = true;
            }
            /// <summary>      
            /// 直接打印       
            /// </summary>      
            ///
            public void Print() { printSystem.Print(); }              
            /// <summary>       
            /// 预览打印       
            /// </summary>      
            ///
            public void Preview()
            {
                printSystem.PreviewFormEx.ShowDialog();
                //进行预览       
            }

            public void Designe() { }
        }


调用:

    /// <summary>       
    /// 客户高级查询打印       
    /// </summary>      
    /// /// <param name="sender"></param>       
    /// <param name="e"></param>       
    ///
    private void simpleButton29_Click(object sender, EventArgs e)       
   {           
        DXPrintHelper print = new DXPrintHelper(this.CustomGrid2, "客户列表", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), true);
        print.Preview();       
    }

你可能感兴趣的:(object,String,Class,报表)