显示水晶报表的公用类
在应用程序中,通常我们都需要显示报表。在.net中,我们大多数情况使用了水晶报表,如果我们不是直接将报表发送到打印机打印,那么就需要将报表显示出来,这种情况下需要使用报表查看器(CrystalReportViewer)。using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ChiefValueAccounting.Util;
{
/// <summary>
/// 显示与打印水晶报表。
/// </summary>
public class CrystalReportBox
{
private static CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer;
private static System.Windows.Forms.Form frmReport;
{
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>
/// <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>
/// <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>
/// <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);
}
}
}
}
也就是说,对于报表中的一些业务逻辑设置,是不应该放到这个显示类中的,而是应该由调用方负责进行设置与刷新。
欢迎大家提出宝贵的反馈信息!