xtraReport控件和水晶报表类似但是还是有点不一样


windows窗体的报表的创建和水晶报表略有不同:

using System;

using System.Collections;

using System.Diagnostics;

using System.Drawing;

using System.IO;

using System.Reflection;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using DevExpress.XtraPrinting;

using DevExpress.XtraReports.UI;

using DevExpress.XtraReports.Native;

using DevExpress.XtraReports.UserDesigner;

using DevExpress.XtraPrinting.Native;

using DevExpress.XtraPrinting.Preview;

using DevExpress.XtraPrinting.Control;

namespace XtraReportsDemos {

 /// <summary>

 /// Summary description for ModuleControl.

 /// </summary>

 public class PreviewControl: ModuleControl {

        static readonly PrintingSystemCommand[] exportCommands = new PrintingSystemCommand[] {

                PrintingSystemCommand.ExportPdf,

                PrintingSystemCommand.ExportHtm,

                PrintingSystemCommand.ExportTxt,

                PrintingSystemCommand.ExportCsv,

                PrintingSystemCommand.ExportMht,

                PrintingSystemCommand.ExportXls,

                PrintingSystemCommand.ExportRtf,

                PrintingSystemCommand.ExportGraphic

            };

        static readonly string[] exportStrings = new string[] {

                   "Export to PDF",

                   "Export to HTML",

                   "Export to TXT",

                   "Export to CSV",

                   "Export to MHT",

                   "Export to XLS",

                   "Export to RTF",

                   "Export to Image"};

  protected class ReflectPrintControl: DevExpress.XtraPrinting.Control.PrintControl {

   public new ProgressReflector ProgressReflector { get { return base.ProgressReflector; }

   }

   public DevExpress.XtraEditors.ProgressBarControl ProgressBar { get { return base.progressBar; }

   }

  }

  public class DesignForm : DevExpress.XtraReports.UserDesigner.XRDesignFormEx {

   protected override void SaveLayout() { }

   protected override void RestoreLayout() { }

  }

  protected System.Windows.Forms.Panel pnlSettings;

        protected XtraReportsDemos.PreviewControl.ReflectPrintControl printControl;

        protected DevExpress.XtraPrinting.PrintingSystem printingSystem;

  protected DevExpress.XtraEditors.ComboBoxEdit cmbExport;

  protected DevExpress.XtraEditors.ComboBoxEdit cmbEdit;

  private System.ComponentModel.IContainer components;

  private Cursor saveCursor;

  private DevExpress.XtraEditors.PanelControl panelControl1;

  protected PrintBarManager fPrintBarManager;


  public PreviewControl() {

   InitializeComponent(); 

   this.cmbExport.SelectedIndex = 0;

   printingSystem.SetCommandVisibility(PrintingSystemCommand.ClosePreview, DevExpress.XtraPrinting.CommandVisibility.None);

   fPrintBarManager = CreatePrintBarManager(printControl);

  }


  public override XtraReport Report {

   get { return fReport; }

   set {

    if (fReport != value) {

     if (fReport != null)

      fReport.Dispose();

     fReport = value;

     if (fReport == null)

      return;

     printingSystem.ClearContent();

     Invalidate();

     Update();

     fileName = XtraReportsDemos.Helper.GetReportPath(fReport, "repx");

     fReport.PrintingSystem = printingSystem;

                    fReport.CreateDocument();

     //printControl.ExecCommand(PrintingSystemCommand.DocumentMap, new object[] {false});

     //previewBar.Buttons[0].Pushed = false;

    }

   }

  } 


  public override void Activate() {

   ProgressReflector.RegisterReflector(printControl.ProgressReflector);

   try {

    base.Activate();

   } finally {

    ProgressReflector.MaximizeValue();

    ProgressReflector.UnregisterReflector(printControl.ProgressReflector);

   }

  }


  #region Windows Form Designer generated code

  /// <summary>

  /// Required method for Designer support - do not modify

  /// the contents of this method with the code editor.

  /// </summary>

  ///

  private void InitializeComponent() {

   this.components = new System.ComponentModel.Container();

   this.pnlSettings = new System.Windows.Forms.Panel();

   this.cmbEdit = new DevExpress.XtraEditors.ComboBoxEdit();

   this.cmbExport = new DevExpress.XtraEditors.ComboBoxEdit();

   this.printControl = new XtraReportsDemos.PreviewControl.ReflectPrintControl();

   this.printingSystem = new DevExpress.XtraPrinting.PrintingSystem(this.components);

   this.panelControl1 = new DevExpress.XtraEditors.PanelControl();

   this.pnlSettings.SuspendLayout();

   ((System.ComponentModel.ISupportInitialize)(this.cmbEdit.Properties)).BeginInit();

   ((System.ComponentModel.ISupportInitialize)(this.cmbExport.Properties)).BeginInit();

   ((System.ComponentModel.ISupportInitialize)(this.printingSystem)).BeginInit();

   ((System.ComponentModel.ISupportInitialize)(this.panelControl1)).BeginInit();

   this.panelControl1.SuspendLayout();

   this.SuspendLayout();

   //

   // pnlSettings

   //

   this.pnlSettings.Controls.AddRange(new System.Windows.Forms.Control[] {

                       this.cmbEdit,

                       this.cmbExport});

   this.pnlSettings.Dock = System.Windows.Forms.DockStyle.Top;

   this.pnlSettings.Name = "pnlSettings";

   this.pnlSettings.Size = new System.Drawing.Size(700, 36);

   this.pnlSettings.TabIndex = 0;

   //

   // cmbEdit

   //

   this.cmbEdit.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);

   this.cmbEdit.EditValue = "comboBoxEdit1";

   this.cmbEdit.Location = new System.Drawing.Point(516, 8);

   this.cmbEdit.Name = "cmbEdit";

   //

   // cmbEdit.Properties

   //

   this.cmbEdit.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

   this.cmbEdit.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {

                           new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "Edit", -1, true, true, false, DevExpress.Utils.HorzAlignment.Center, null)});

   this.cmbEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;

   this.cmbEdit.Properties.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(this.cmbEdit_Properties_ButtonClick);

   this.cmbEdit.Size = new System.Drawing.Size(75, 18);

   this.cmbEdit.TabIndex = 2;

   //

   // cmbExport

   //

   this.cmbExport.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);

   this.cmbExport.Location = new System.Drawing.Point(596, 8);

   this.cmbExport.Name = "cmbExport";

   //

   // cmbExport.Properties

   //

   this.cmbExport.Properties.ActionButtonIndex = 1;

   this.cmbExport.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

   this.cmbExport.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {

                             new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "Export", -1, true, true, false, DevExpress.Utils.HorzAlignment.Center, null),

                             new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo, "", 0, true, true, false, DevExpress.Utils.HorzAlignment.Center, null)});

   this.cmbExport.Properties.DropDownRows = 8;

   this.cmbExport.Properties.Items.AddRange(exportStrings);

   this.cmbExport.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;

   this.cmbExport.Properties.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(this.cmbExport_Properties_ButtonClick);

   this.cmbExport.Size = new System.Drawing.Size(104, 18);

   this.cmbExport.TabIndex = 1;

   this.cmbExport.SelectedIndexChanged += new System.EventHandler(this.cmbExport_SelectedIndexChanged);

   this.cmbExport.CloseUp += new DevExpress.XtraEditors.Controls.CloseUpEventHandler(this.cmbExport_CloseUp);

   //

   // printControl

   //

   this.printControl.Dock = System.Windows.Forms.DockStyle.Fill;

   this.printControl.IsMetric = false;

   this.printControl.Location = new System.Drawing.Point(2, 2);

   this.printControl.Name = "printControl";

   this.printControl.PrintingSystem = this.printingSystem;

   this.printControl.Size = new System.Drawing.Size(696, 293);

   this.printControl.TabIndex = 1;

   this.printControl.TabStop = false;

   this.printControl.Text = "printControl1";

   //

   // panelControl1

   //

   this.panelControl1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;

   this.panelControl1.Controls.AddRange(new System.Windows.Forms.Control[] {

                      this.printControl});

   this.panelControl1.Dock = System.Windows.Forms.DockStyle.Fill;

   this.panelControl1.DockPadding.All = -1;

   this.panelControl1.Location = new System.Drawing.Point(0, 77);

   this.panelControl1.Name = "panelControl1";

   this.panelControl1.Size = new System.Drawing.Size(700, 297);

   this.panelControl1.TabIndex = 4;

   this.panelControl1.Text = "panelControl1";

   //

   // PreviewControl

   //

   this.Controls.AddRange(new System.Windows.Forms.Control[] {

                    this.panelControl1,

                    this.pnlSettings});

   this.Name = "PreviewControl";

   this.Size = new System.Drawing.Size(700, 396);

   this.pnlSettings.ResumeLayout(false);

   ((System.ComponentModel.ISupportInitialize)(this.cmbEdit.Properties)).EndInit();

   ((System.ComponentModel.ISupportInitialize)(this.cmbExport.Properties)).EndInit();

   ((System.ComponentModel.ISupportInitialize)(this.printingSystem)).EndInit();

   ((System.ComponentModel.ISupportInitialize)(this.panelControl1)).EndInit();

   this.panelControl1.ResumeLayout(false);

   this.ResumeLayout(false);

  }

  #endregion

  protected PrintBarManager CreatePrintBarManager(PrintControl pc) {

   PrintBarManager printBarManager = new PrintBarManager();

   printBarManager.Form = printControl;

   printBarManager.Initialize(pc);

   printBarManager.MainMenu.Visible = false;

   printBarManager.AllowCustomization = false;

   return printBarManager;

  }

  private void ShowDesignerForm(Form designForm, Form parentForm) {

            designForm.MinimumSize = parentForm.MinimumSize;

            if(parentForm.WindowState == FormWindowState.Normal)

       designForm.Bounds = parentForm.Bounds;

            designForm.WindowState = parentForm.WindowState;

   parentForm.Visible = false;    

   designForm.ShowDialog();

            parentForm.Visible = true;

  }

  private void cmbEdit_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {

   XtraReport report = CreateReport();

   System.Diagnostics.Debug.Assert(report != null);

   if(File.Exists(fileName))

    report.LoadLayout(fileName);

   XRDesignFormExBase designForm = new CustomDesignForm();

   try {

    designForm.OpenReport(report);

    designForm.FileName = fileName;

    ShowDesignerForm(designForm, this.FindForm());

    if (designForm.FileName != fileName && File.Exists(designForm.FileName))

     File.Copy(designForm.FileName, fileName, true);

   }

   finally {

    designForm.Dispose();

    report.Dispose();

   }

   if(System.IO.File.Exists(fileName)) {

    fReport.LoadLayout(fileName);

                fReport.CreateDocument();

    InitializeControls();

   }

  }

  protected virtual void InitializeControls() {

  }

  private void RestoreProcess() {

   Cursor.Current = saveCursor;

  } 

  private void SetWaitProcess() {

   saveCursor = Cursor.Current;

   Cursor.Current = Cursors.AppStarting;

  }  

  private void ExportByIndex(int index) {

   if (fReport == null)

    return;

   ProgressReflector.RegisterReflector(printControl.ProgressReflector);

   SetWaitProcess();

   try {

                printingSystem.ExecCommand(exportCommands[index]);

   }

   finally {

    ProgressReflector.UnregisterReflector(printControl.ProgressReflector);

    RestoreProcess();  

   }

  }

  private void cmbExport_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {

   if (e.Button == cmbExport.Properties.Buttons[0])

    ExportByIndex(cmbExport.SelectedIndex);

  }

  private void cmbExport_SelectedIndexChanged(object sender, System.EventArgs e) {

   if (cmbExport.SelectedItem != null)

    cmbExport.Properties.Buttons[0].Caption = cmbExport.SelectedItem.ToString();

  }

  private void cmbExport_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e) {

   if(e.CloseMode != DevExpress.XtraEditors.PopupCloseMode.Normal) return;

   int index = cmbExport.Properties.Items.IndexOf(e.Value);

   if (index != -1)

    ExportByIndex(index);

  }

 }

 public class Helper {

  public const int ICC_USEREX_CLASSES = 0x00000200;

  [StructLayout(LayoutKind.Sequential, Pack=1)]

   public class INITCOMMONCONTROLSEX {

   public int  dwSize = 8; //ndirect.DllLib.sizeOf(this);

   public int  dwICC;

  }

  [DllImport("comctl32.dll")]

  public static extern bool InitCommonControlsEx(INITCOMMONCONTROLSEX icc);

  [DllImport("kernel32.dll")]

  public static extern IntPtr LoadLibrary(string libname);


  [DllImport("kernel32.dll")]

  public static extern bool FreeLibrary(IntPtr hModule);


  public static string GetReportPath(DevExpress.XtraReports.UI.XtraReport fReport, string ext) {

   System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

   string repName = fReport.Name;

   if(repName.Length == 0) repName = fReport.GetType().Name;

   string dirName = Path.GetDirectoryName(asm.Location);

   return Path.Combine(dirName, repName + "." + ext);

  }

  public static Image LoadImage(string name) {

   Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("XtraReportsDemos." + name));

   bmp.MakeTransparent(Color.Magenta);

   return bmp;

  }

        public static void PatchConnections(string startupPath) {

            string ignore = global::XtraReportsDemos.Properties.Settings.Default.nwindConnectionString;

            ignore = global::XtraReportsDemos.Properties.Settings.Default.countriesDBConnectionString;

            ignore = global::XtraReportsDemos.Properties.Settings.Default.CarsDBConnectionString;

            ignore = global::XtraReportsDemos.Properties.Settings.Default.StyleSheetLavenderPath;

            ignore = global::XtraReportsDemos.Properties.Settings.Default.BiolifeTxtPath;

            string[] connections = new String[] { "nwindConnectionString", "countriesDBConnectionString", "CarsDBConnectionString" };

            foreach(string s in connections)

                PatchConnection(s, startupPath);

            string[] pathes = new String[] { "StyleSheetLavenderPath", "BiolifeTxtPath" };

            foreach(string s in pathes)

                PatchPath(s, startupPath);

        }

        static void PatchPath(string propertyName, string startupPath) {

            string path = (string)global::XtraReportsDemos.Properties.Settings.Default[propertyName];

            string fileName = System.IO.Path.GetFileName(path);

            string newPath = DevExpress.Utils.FilesHelper.FindingFileName(startupPath, "Data//" + fileName, false);

            if(System.IO.File.Exists(newPath))

                global::XtraReportsDemos.Properties.Settings.Default[propertyName] = newPath;

        }

        static void PatchConnection(string propertyName, string startupPath) {

            string s = (string)global::XtraReportsDemos.Properties.Settings.Default[propertyName];

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @".*Data Source=(?<dataSource>[^;]*)");

            System.Text.RegularExpressions.Group matchGroup = m.Groups["dataSource"];

            if(matchGroup.Success) {

                string fileName = System.IO.Path.GetFileName(matchGroup.Value);

                string path = DevExpress.Utils.FilesHelper.FindingFileName(startupPath, "Data//" + fileName, false);

                if(System.IO.File.Exists(path))

                    global::XtraReportsDemos.Properties.Settings.Default[propertyName] = s.Replace(matchGroup.Value, path);

            }

        }

 }

 public class ReportNames {

        public const string

   PivotGrid = "Pivot Grid",

            MailMerge = "Mail Merge",

            OddEvenStyles = "Products by Categories",

            CrossBands = "Cross Band Controls",

            AnchorVertical = "Vertical Anchoring",

            NorthwindTraders_Products = "Products List",

            NorthwindTraders_Catalog = "Fall Catalog",

            NorthwindTraders_Invoice = "Invoice",

            MasterDetailReport = "Suppliers",

            MultiColumnReport = "Customers",

            IListDatasource = "Fishes",

            CustomDraw = "Population",

            ShrinkGrow = "Employees",

            BarCodes_ProductLabels = "Product Labels",

            BarCodes_BarcodeTypes = "Barcode Types",

            RichText = "Cars",

            Subreports = "Customers List",

            TableReport = "Customer Order",

            TreeView = "Countries",

            Charts = "Prices",

            Shapes = "Shapes",

            SideBySideReports = "Employee Comparison";

 }

}

2、至于水晶报表的就是首先拖动一个水晶报表浏览器。然后新建一个水晶报表后直接选择报表源就可以了




那么web的两种用法基本相同 其详细用法我就不多说了。