彩票走势图

DevExpress开发教程之报表打印设计实现篇

原创|其它|编辑:郝浩|2012-10-12 17:04:30.000|阅读 4781 次

概述:知道如何用 DevExpress的进行报表打印设计吗?本文将和大家分享一下制作过程。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

知道如何用 DevExpress的进行报表打印设计吗?本文和大家分享一下制作过程:

1、封装设计组件。

图片如下:

DevExpress开发教程之如何进行报表打印设计

2、代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    /// <summary>
    /// 文件名称:DesignReportControl.cs
    /// 功能摘要:封装报表设计模块。
    /// 原始作者:kingmond@126.com
    /// 编写日期:2009/12/07
    /// 使用说明:请参照各个方法函数的定义。
    /// 修改记录:
    /// 功能            日期            修改人
    /// </summary>
    public partial class DesignReportControl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DesignReportControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设计面板属性
        /// </summary>
        public DevExpress.XtraReports.UserDesigner.XRDesignPanel DesignPanel
        {
            get { return this.xrDesignPanel1; }
        }
    }
}

namespace Demo
{
    partial class DesignReportControl
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.dockManager1 = new DevExpress.XtraBars.Docking.DockManager();
            this.panelContainer2 = new DevExpress.XtraBars.Docking.DockPanel();
            this.panelDesignReportExplorer = new DevExpress.XtraBars.Docking.DockPanel();
            this.controlContainer1 = new DevExpress.XtraBars.Docking.ControlContainer();
            this.xrDesignReportExplorer1 = new DevExpress.XtraReports.UserDesigner.XRDesignReportExplorer();
            this.xrDesignPanel1 = new DevExpress.XtraReports.UserDesigner.XRDesignPanel();
            this.panelDesignPropertyGrid = new DevExpress.XtraBars.Docking.DockPanel();
            this.controlContainer2 = new DevExpress.XtraBars.Docking.ControlContainer();
            this.xrDesignPropertyGrid1 = new DevExpress.XtraReports.UserDesigner.XRDesignPropertyGrid();
            this.panelContainer1 = new DevExpress.XtraBars.Docking.DockPanel();
            this.panelFieldList = new DevExpress.XtraBars.Docking.DockPanel();
            this.dockPanel1_Container = new DevExpress.XtraBars.Docking.ControlContainer();
            this.xrDesignFieldList1 = new DevExpress.XtraReports.UserDesigner.XRDesignFieldList();
            this.panelDesignToolBox = new DevExpress.XtraBars.Docking.DockPanel();
            this.dockPanel2_Container = new DevExpress.XtraBars.Docking.ControlContainer();
            this.xrDesignToolBox1 = new DevExpress.XtraReports.UserDesigner.XRDesignToolBox();
            ((System.ComponentModel.ISupportInitialize)(this.dockManager1)).BeginInit();
            this.panelContainer2.SuspendLayout();
            this.panelDesignReportExplorer.SuspendLayout();
            this.controlContainer1.SuspendLayout();
            this.panelDesignPropertyGrid.SuspendLayout();
            this.controlContainer2.SuspendLayout();
            this.panelContainer1.SuspendLayout();
            this.panelFieldList.SuspendLayout();
            this.dockPanel1_Container.SuspendLayout();
            this.panelDesignToolBox.SuspendLayout();
            this.dockPanel2_Container.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.xrDesignToolBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // dockManager1
            // 
            this.dockManager1.Form = this;
            this.dockManager1.RootPanels.AddRange(new DevExpress.XtraBars.Docking.DockPanel[] {
            this.panelContainer2,
            this.panelContainer1});
            this.dockManager1.TopZIndexControls.AddRange(new string[] {
            "DevExpress.XtraBars.BarDockControl",
            "System.Windows.Forms.StatusBar"});
            // 
            // panelContainer2
            // 
            this.panelContainer2.Controls.Add(this.panelDesignReportExplorer);
            this.panelContainer2.Controls.Add(this.panelDesignPropertyGrid);
            this.panelContainer2.Dock = DevExpress.XtraBars.Docking.DockingStyle.Right;
            this.panelContainer2.ID = new System.Guid("34e36bc4-3508-42a0-b102-c4c3eecfe6f5");
            this.panelContainer2.Location = new System.Drawing.Point(243, 0);
            this.panelContainer2.Name = "panelContainer2";
            this.panelContainer2.Size = new System.Drawing.Size(200, 249);
            this.panelContainer2.Text = "panelContainer2";
            // 
            // panelDesignReportExplorer
            // 
            this.panelDesignReportExplorer.Controls.Add(this.controlContainer1);
            this.panelDesignReportExplorer.Dock = DevExpress.XtraBars.Docking.DockingStyle.Fill;
            this.panelDesignReportExplorer.ID = new System.Guid("a54ab865-fc8c-44dd-8ae2-d0a9a4579186");
            this.panelDesignReportExplorer.Location = new System.Drawing.Point(0, 0);
            this.panelDesignReportExplorer.Name = "panelDesignReportExplorer";
            this.panelDesignReportExplorer.Options.ShowCloseButton = false;
            this.panelDesignReportExplorer.Options.ShowMaximizeButton = false;
            this.panelDesignReportExplorer.Size = new System.Drawing.Size(200, 124);
            this.panelDesignReportExplorer.TabText = "Design Report Explorer";
            this.panelDesignReportExplorer.Text = "Design Report Explorer";
            // 
            // controlContainer1
            // 
            this.controlContainer1.Controls.Add(this.xrDesignReportExplorer1);
            this.controlContainer1.Location = new System.Drawing.Point(3, 21);
            this.controlContainer1.Name = "controlContainer1";
            this.controlContainer1.Size = new System.Drawing.Size(194, 100);
            this.controlContainer1.TabIndex = 0;
            // 
            // xrDesignReportExplorer1
            // 
            this.xrDesignReportExplorer1.AllowDrop = true;
            this.xrDesignReportExplorer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.xrDesignReportExplorer1.HideSelection = false;
            this.xrDesignReportExplorer1.ImageIndex = 0;
            this.xrDesignReportExplorer1.Location = new System.Drawing.Point(0, 0);
            this.xrDesignReportExplorer1.Name = "xrDesignReportExplorer1";
            this.xrDesignReportExplorer1.SelectedImageIndex = 0;
            this.xrDesignReportExplorer1.Size = new System.Drawing.Size(194, 100);
            this.xrDesignReportExplorer1.TabIndex = 0;
            this.xrDesignReportExplorer1.XRDesignPanel = this.xrDesignPanel1;
            // 
            // xrDesignPanel1
            // 
            this.xrDesignPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.xrDesignPanel1.Location = new System.Drawing.Point(183, 0);
            this.xrDesignPanel1.Name = "xrDesignPanel1";
            this.xrDesignPanel1.Size = new System.Drawing.Size(60, 249);
            this.xrDesignPanel1.TabIndex = 4;
            this.xrDesignPanel1.Text = "xrDesignPanel1";
            // 
            // panelDesignPropertyGrid
            // 
            this.panelDesignPropertyGrid.Controls.Add(this.controlContainer2);
            this.panelDesignPropertyGrid.Dock = DevExpress.XtraBars.Docking.DockingStyle.Fill;
            this.panelDesignPropertyGrid.ID = new System.Guid("0e48f33e-2834-4cbf-ab1e-ca98b5525fa9");
            this.panelDesignPropertyGrid.Location = new System.Drawing.Point(0, 124);
            this.panelDesignPropertyGrid.Name = "panelDesignPropertyGrid";
            this.panelDesignPropertyGrid.Options.ShowCloseButton = false;
            this.panelDesignPropertyGrid.Options.ShowMaximizeButton = false;
            this.panelDesignPropertyGrid.Size = new System.Drawing.Size(200, 125);
            this.panelDesignPropertyGrid.TabText = "Design Property";
            this.panelDesignPropertyGrid.Text = "Design Property";
            // 
            // controlContainer2
            // 
            this.controlContainer2.Controls.Add(this.xrDesignPropertyGrid1);
            this.controlContainer2.Location = new System.Drawing.Point(3, 21);
            this.controlContainer2.Name = "controlContainer2";
            this.controlContainer2.Size = new System.Drawing.Size(194, 101);
            this.controlContainer2.TabIndex = 0;
            // 
            // xrDesignPropertyGrid1
            // 
            this.xrDesignPropertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.xrDesignPropertyGrid1.Location = new System.Drawing.Point(0, 0);
            this.xrDesignPropertyGrid1.Name = "xrDesignPropertyGrid1";
            this.xrDesignPropertyGrid1.Size = new System.Drawing.Size(194, 101);
            this.xrDesignPropertyGrid1.TabIndex = 0;
            this.xrDesignPropertyGrid1.XRDesignPanel = this.xrDesignPanel1;
            // 
            // panelContainer1
            // 
            this.panelContainer1.Controls.Add(this.panelFieldList);
            this.panelContainer1.Controls.Add(this.panelDesignToolBox);
            this.panelContainer1.Dock = DevExpress.XtraBars.Docking.DockingStyle.Left;
            this.panelContainer1.ID = new System.Guid("a9ce15ce-8424-4a61-9c2a-77347d0d6af5");
            this.panelContainer1.Location = new System.Drawing.Point(0, 0);
            this.panelContainer1.Name = "panelContainer1";
            this.panelContainer1.Size = new System.Drawing.Size(183, 249);
            this.panelContainer1.Text = "panelContainer1";
            // 
            // panelFieldList
            // 
            this.panelFieldList.Controls.Add(this.dockPanel1_Container);
            this.panelFieldList.Dock = DevExpress.XtraBars.Docking.DockingStyle.Fill;
            this.panelFieldList.ID = new System.Guid("21cf11bb-063d-4ab2-abef-d4173813b2cf");
            this.panelFieldList.Location = new System.Drawing.Point(0, 0);
            this.panelFieldList.Name = "panelFieldList";
            this.panelFieldList.Options.ShowCloseButton = false;
            this.panelFieldList.Options.ShowMaximizeButton = false;
            this.panelFieldList.Size = new System.Drawing.Size(183, 124);
            this.panelFieldList.TabText = "FieldList";
            this.panelFieldList.Text = "FieldList";
            // 
            // dockPanel1_Container
            // 
            this.dockPanel1_Container.Controls.Add(this.xrDesignFieldList1);
            this.dockPanel1_Container.Location = new System.Drawing.Point(3, 21);
            this.dockPanel1_Container.Name = "dockPanel1_Container";
            this.dockPanel1_Container.Size = new System.Drawing.Size(177, 100);
            this.dockPanel1_Container.TabIndex = 0;
            // 
            // xrDesignFieldList1
            // 
            this.xrDesignFieldList1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.xrDesignFieldList1.HideSelection = false;
            this.xrDesignFieldList1.ImageIndex = 0;
            this.xrDesignFieldList1.Location = new System.Drawing.Point(0, 0);
            this.xrDesignFieldList1.Name = "xrDesignFieldList1";
            this.xrDesignFieldList1.SelectedImageIndex = 0;
            this.xrDesignFieldList1.Size = new System.Drawing.Size(177, 100);
            this.xrDesignFieldList1.TabIndex = 0;
            this.xrDesignFieldList1.XRDesignPanel = this.xrDesignPanel1;
            // 
            // panelDesignToolBox
            // 
            this.panelDesignToolBox.Controls.Add(this.dockPanel2_Container);
            this.panelDesignToolBox.Dock = DevExpress.XtraBars.Docking.DockingStyle.Fill;
            this.panelDesignToolBox.ID = new System.Guid("5902e9b4-da5a-4b7b-8350-24cd28bcc53d");
            this.panelDesignToolBox.Location = new System.Drawing.Point(0, 124);
            this.panelDesignToolBox.Name = "panelDesignToolBox";
            this.panelDesignToolBox.Options.ShowCloseButton = false;
            this.panelDesignToolBox.Options.ShowMaximizeButton = false;
            this.panelDesignToolBox.Size = new System.Drawing.Size(183, 125);
            this.panelDesignToolBox.TabText = "DesignToolBox";
            this.panelDesignToolBox.Text = "DesignToolBox";
            // 
            // dockPanel2_Container
            // 
            this.dockPanel2_Container.Controls.Add(this.xrDesignToolBox1);
            this.dockPanel2_Container.Location = new System.Drawing.Point(3, 21);
            this.dockPanel2_Container.Name = "dockPanel2_Container";
            this.dockPanel2_Container.Size = new System.Drawing.Size(177, 101);
            this.dockPanel2_Container.TabIndex = 0;
            // 
            // xrDesignToolBox1
            // 
            this.xrDesignToolBox1.ActiveGroup = null;
            this.xrDesignToolBox1.AllowSelectedLink = true;
            this.xrDesignToolBox1.Appearance.ItemDisabled.Options.UseTextOptions = true;
            this.xrDesignToolBox1.Appearance.ItemDisabled.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            this.xrDesignToolBox1.Appearance.ItemDisabled.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            this.xrDesignToolBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.xrDesignToolBox1.HotTrackedItemCursor = System.Windows.Forms.Cursors.Arrow;
            this.xrDesignToolBox1.Location = new System.Drawing.Point(0, 0);
            this.xrDesignToolBox1.Name = "xrDesignToolBox1";
            this.xrDesignToolBox1.Size = new System.Drawing.Size(177, 101);
            this.xrDesignToolBox1.TabIndex = 0;
            this.xrDesignToolBox1.Text = "xrDesignToolBox1";
            this.xrDesignToolBox1.View = new DevExpress.XtraNavBar.ViewInfo.VSToolBoxViewInfoRegistrator();
            this.xrDesignToolBox1.XRDesignPanel = this.xrDesignPanel1;
            // 
            // DesignReportControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.xrDesignPanel1);
            this.Controls.Add(this.panelContainer1);
            this.Controls.Add(this.panelContainer2);
            this.Name = "DesignReportControl";
            this.Size = new System.Drawing.Size(443, 249);
            ((System.ComponentModel.ISupportInitialize)(this.dockManager1)).EndInit();
            this.panelContainer2.ResumeLayout(false);
            this.panelDesignReportExplorer.ResumeLayout(false);
            this.controlContainer1.ResumeLayout(false);
            this.panelDesignPropertyGrid.ResumeLayout(false);
            this.controlContainer2.ResumeLayout(false);
            this.panelContainer1.ResumeLayout(false);
            this.panelFieldList.ResumeLayout(false);
            this.dockPanel1_Container.ResumeLayout(false);
            this.panelDesignToolBox.ResumeLayout(false);
            this.dockPanel2_Container.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.xrDesignToolBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private DevExpress.XtraBars.Docking.DockManager dockManager1;
        private DevExpress.XtraBars.Docking.DockPanel panelFieldList;
        private DevExpress.XtraBars.Docking.ControlContainer dockPanel1_Container;
        private DevExpress.XtraBars.Docking.DockPanel panelDesignToolBox;
        private DevExpress.XtraBars.Docking.ControlContainer dockPanel2_Container;
        private DevExpress.XtraBars.Docking.DockPanel panelContainer1;
        private DevExpress.XtraBars.Docking.DockPanel panelContainer2;
        private DevExpress.XtraBars.Docking.DockPanel panelDesignReportExplorer;
        private DevExpress.XtraBars.Docking.ControlContainer controlContainer1;
        private DevExpress.XtraBars.Docking.DockPanel panelDesignPropertyGrid;
        private DevExpress.XtraBars.Docking.ControlContainer controlContainer2;
        private DevExpress.XtraReports.UserDesigner.XRDesignPanel xrDesignPanel1;
        private DevExpress.XtraReports.UserDesigner.XRDesignFieldList xrDesignFieldList1;
        private DevExpress.XtraReports.UserDesigner.XRDesignToolBox xrDesignToolBox1;
        private DevExpress.XtraReports.UserDesigner.XRDesignReportExplorer xrDesignReportExplorer1;
        private DevExpress.XtraReports.UserDesigner.XRDesignPropertyGrid xrDesignPropertyGrid1;

    }
}

3、把DesignReportControl和DesignToolBar和DesignFormattingToolBar组合为DesignReportForm。

图片如下:

DevExpress开发教程之如何进行报表打印设计

4、DesignReportForm主要代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;

namespace Demo
{
    /// <summary>
    /// 文件名称:DesignReportForm.cs
    /// 功能摘要:封装报表设计模块。
    /// 原始作者:kingmond@126.com
    /// 编写日期:2007/12/07
    /// 使用说明:请参照各个方法函数的定义。
    /// 修改记录:
    /// 功能            日期            修改人
    /// </summary>
    public partial class DesignReportForm : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DesignReportForm(string fileName)
        {
            InitializeComponent();
            this.xrDesignToolBar1.XRDesignPanel = this.designReportControl1.DesignPanel;
            this.xrDesignFormattingToolBar1.XRDesignPanel = this.designReportControl1.DesignPanel;
            this.designReportControl1.DesignPanel.FileName = fileName;
            this.txtFileName.Caption = fileName;
            this.xrDesignToolBar1.Buttons[0].Visible = false;
        }
        /// <summary>
        /// 打开设计对话框
        /// </summary>
        /// <param name="report">报表对象</param>
        public DialogResult ShowDialog(XtraReport report)
        {   
            this.designReportControl1.DesignPanel.OpenReport(report);               
            return this.ShowDialog();
        }        
    }
}

5、工具ReportUtil:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace Demo
{
    /// <summary>
    /// 文件名称:ReportUtil.cs
    /// 功能摘要:封装报表设计工具。
    /// 原始作者:kingmond@126.com
    /// 编写日期:2007/12/07
    /// 使用说明:请参照各个方法函数的定义。
    /// 修改记录:
    /// 功能            日期            修改人
    /// </summary>
    class ReportUtil
    {
        /// <summary>
        /// 设计打印报表
        /// </summary>
        /// <param name="reportFile">报表格式文件</param>
        /// <param name="schemaFile">数据集格式文件</param>
        public static void DesignReport(string reportFile, string schemaFile)
        {
            XtraReport rpt = XtraReport.FromFile(reportFile, true);
            DataSet ds = new DataSet();
            ds.ReadXmlSchema(schemaFile);
            rpt.DataSource = ds;
            DesignReportForm dlg = new DesignReportForm(reportFile);
            dlg.ShowDialog(rpt);
        }
        /// <summary>
        /// 打开显示报表
        /// </summary>
        /// <param name="printingSystem">打印系统</param>
        /// <param name="reportFile">报表格式文件</param>
        /// <param name="dataSource">数据源</param>
        public static void OpenReport(PrintingSystem printingSystem, string reportFile, object dataSource)
        {
            XtraReport rpt = XtraReport.FromFile(reportFile, true);
            rpt.DataSource = dataSource;
            rpt.PrintingSystem = printingSystem;
            rpt.CreateDocument();
        }
    }
}

6、打印预览PrintPreviewControl的设计:

DevExpress开发教程之如何进行报表打印设计

7、主要代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace Demo
{
    /// <summary>
    /// 文件名称:PrintPreviewControl.cs
    /// 功能摘要:封装打印预览组件。
    /// 原始作者:kingmond@126.com
    /// 编写日期:2007/12/07
    /// 使用说明:请参照各个方法函数的定义。
    /// 修改记录:
    /// 功能            日期            修改人
    /// </summary>
    public partial class PrintPreviewControl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PrintPreviewControl()
        {
            InitializeComponent();
        }
        public void OpenReport(string reportFile, object dataSource)
        {
            ReportUtil.OpenReport(this.printingSystem1, reportFile, dataSource);            
        }
    }
}

8、设计时使用

{
                string path = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\'));
                ReportUtil.DesignReport(path + @"\ReportSchema\Delivery.repx", 
path +             @"\DataSourceSchema\DeliveryDataSet.xml");
}

9、打印预览时使用:

 

{
                string path = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\'));
                this.printPreviewControl1.OpenReport(path + @"\ReportSchema\Delivery.repx", GetDeliveryDataSet());
}

具体讲解见后续篇章。



标签:DevExpress报表报表控件

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP