请输入您要查询的百科知识:

 

词条 CrystalReportViewer
释义

报表查看器(CrystalReportViewer)作为一个控件,它需要一个承载它的窗体或页面。在应用程序中,通常都需要显示报表。在。net中,我们大多数情况使用了水晶报表,如果我们不是直接将报表发送到打印机打印,那么就需要将报表显示出来,这种情况下需要使用报表查看器。

我们这里只讨论窗体(WinForm)的情况。大多数情况下,我们显示报表的界面都是相同的,因此,我们一般会做一个窗体类,来显示我们不同的报表。在有的实现中,我们是在这个窗体类中实例化我们所需要的报表对象的,当我们每次要显示不同的报表时,都需要去维护这个报表显示窗体类,这样会给我们带来许多不便。更有甚者,把一些报表的逻辑也写到了这个窗体类中,这样严重违反了类的单一职责原则,并且使我们的报表与报表显示之前有很强的依赖性。

为了遵循类的单一职责原则,破除它们之前的相互依赖性,在下面我将提供一个实现水晶报表显示的范例,这个范例的灵感来源于 MessageBox类,只提供了几个静态公共方法用于显示报表:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using ChiefValueAccounting.Util;

namespace ChiefValueAccounting.Win.Print

{

/// <summary>

/// 显示与打印水晶报表。

/// </summary>

public class CrystalReportBox

{

private static CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer;

private static System.Windows.Forms.Form frmReport;

static CrystalReportBox()

{

InitializeComponent();

}

private static void InitializeComponent()

{

frmReport = new Form();

crystalReportViewer = new CrystalDecisions.Windows.Forms.CrystalReportViewer();

frmReport.SuspendLayout();

//

// crystalReportViewer

//

crystalReportViewer.ActiveViewIndex = -1;

crystalReportViewer.DisplayGroupTree = false;

crystalReportViewer.Dock = System.Windows.Forms.DockStyle.Fill;

crystalReportViewer.Location = new System.Drawing.Point(0, 0);

crystalReportViewer.Name = "crystalReportViewer";

crystalReportViewer.ReportSource = null;

crystalReportViewer.Size = new System.Drawing.Size(708, 482);

crystalReportViewer.TabIndex = 0;

//

// frmReport

//

frmReport.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

frmReport.ClientSize = new System.Drawing.Size(708, 482);

frmReport.Controls.Add(crystalReportViewer);

frmReport.Name = "FormPrintReportEx";

frmReport.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;

frmReport.Text = "Report Viewer";

frmReport.WindowState = System.Windows.Forms.FormWindowState.Maximized;

frmReport.MaximizeBox = false;

frmReport.MinimizeBox = false;

frmReport.ShowInTaskbar = false;

frmReport.FormBorderStyle = FormBorderStyle.FixedDialog;

frmReport.ResumeLayout(false);

}

/// <summary>

/// 显示水晶报表,要求调用方设置好报表对象的所有属性。

/// </summary>

/// <param name="reportClass">强类型的报表对象</param>

public static void ShowReport(CrystalDecisions.CrystalReports.Engine.ReportClass reportClass, System.Windows.Forms.IWin32Window owner)

{

try

{

crystalReportViewer.ReportSource = reportClass;

frmReport.ShowDialog(owner);

}

catch(Exception ex)

{

SimpleLogger.Log(ex);

Global.ShowError(ex);

}

}

/// <summary>

/// 显示水晶报表,要求调用方设置好报表对象的所有属性。

/// </summary>

/// <param name="reportDocument">水晶报表文档对象</param>

public static void ShowReport(CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument, System.Windows.Forms.IWin32Window owner)

{

try

{

crystalReportViewer.ReportSource = reportDocument;

frmReport.ShowDialog(owner);

}

catch(Exception ex)

{

SimpleLogger.Log(ex);

Global.ShowError(ex);

}

}

/// <summary>

/// 显示水晶报表。

/// </summary>

/// <param name="reportName">报表名称</param>

/// <param name="dataSource">报表数据源</param>

public static void ShowReport(string reportName, object dataSource, System.Windows.Forms.IWin32Window owner)

{

try

{

ReportDocument rd = new ReportDocument();

rd.Load(reportName);

rd.SetDataSource(dataSource);

frmReport.ShowDialog(owner);

}

catch(Exception ex)

{

SimpleLogger.Log(ex);

Global.ShowError(ex);

}

}

}

}

也就是说,对于报表中的一些业务逻辑设置,是不应该放到这个显示类中的,而是应该由调用方负责进行设置与刷新。

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/26 15:38:20