大名鼎鼎的 Crystal Reports(水晶報表)已內(nèi)嵌為微軟 Visual Studio .NET的標(biāo)準(zhǔn)報表工具,同時升級到 Crystal Reports for Visual Studio .NET。它的優(yōu)點(diǎn)是:1、在 .NET 平臺能夠輕松創(chuàng)建交互式的、高質(zhì)量顯現(xiàn)的報表內(nèi)容,這也是 Crystal Reports 長期以來所具有的主要優(yōu)勢;2、使用 Crystal Reports for Visual Studio .NET,可以在 Web 平臺和 Windows 平臺上承載報表,并將 Crystal 報表作為報表 Web 服務(wù)在 Web 服務(wù)器上發(fā)布;3、利用Crystal Report 創(chuàng)建的Web應(yīng)用程序,使用戶可以深化圖表并根據(jù)需要篩選信息。在 .NET 中,圖表實際上就是一個與應(yīng)用程序中其他控件進(jìn)行交互的 Crystal 報表。在這里我向大家介紹如何在 Windows 窗體應(yīng)用程序中瀏覽水晶報表。
設(shè)計步驟:
1、軟件環(huán)境:要求系統(tǒng)已安裝Visual Studio .Net 集成開發(fā)系統(tǒng),僅裝有 .Net Framework SDk 不能實現(xiàn)本例效果,成為 .Net Framework SDK 沒有水晶報表控件。
2、新建一個 Visual C# 項目 Windows 應(yīng)用程序,設(shè)置Form1的Text="水晶報表瀏覽器",StartPosition=CenterScreen //程序開始出現(xiàn)在屏幕中央,其它屬性均保持默認(rèn);
3、從工具箱拖入一個CrystalReportViewer,一個Button,一個openFileDialog,到窗體。
整個設(shè)計器的窗體布局只需將button1置于窗體底邊中部,不再需要其它多余布局。
設(shè)置crystalReportViewer1的屬性如下:
this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
//停靠方式為充滿整個窗體
//展開+DockPadding
this.crystalReportViewer1.DockPadding.Bottom = 50;
//底部空出放置Button的區(qū)域
this.crystalReportViewer1.DockPadding.Left = 5;
this.crystalReportViewer1.DockPadding.Right = 5;
this.crystalReportViewer1.DockPadding.Top = 5;
this.crystalReportViewer1.ReportSource = null;
//先不載入報表資源
設(shè)置button1的屬性如下:
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
//與窗體的下邊保持固定
this.button1.Text = "打開報表";
openFileDialog1是打開文件的控件,設(shè)置其屬性如下:
this.openFileDialog1.Filter
= "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
//提供打開文件對話框的文件類型,
默認(rèn)類型就是此字符串的最前一種定義的類型
this.openFileDialog1.Title = "打開水晶報表";
//打開文件對話框的標(biāo)題
布局效果圖如下:

4、雙擊button1,添加button1_Click點(diǎn)擊事件:
PRivate void button1_Click(object sender, System.EventArgs e)
{
try
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
this.crystalReportViewer1.ReportSource = @openFileDialog1.FileName;
//加載水晶報表,將報表文件綁定到CrystalReportView 控件;
}
catch(Exception error)
{
MessageBox.Show(error.ToString(),"錯誤");
}
}
5、OK!按Ctrl+F5運(yùn)行吧。
可以瀏覽你系統(tǒng)內(nèi)現(xiàn)有的報表實例:
.../Program Files/Microsoft Visual Studio .NET/Crystal Reports/Samples/Reports/Feature Examples/Chart.rpt
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Windowsapplication10
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的設(shè)計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.crystalReportViewer1 = new
CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.Dock= System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.DockPadding.Bottom = 50;
this.crystalReportViewer1.DockPadding.Left = 5;
this.crystalReportViewer1.DockPadding.Right = 5;
this.crystalReportViewer1.DockPadding.Top = 5;
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.Size = new System.Drawing.Size(292, 273);
this.crystalReportViewer1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.Location = new System.Drawing.Point(104, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "打開報表";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// openFileDialog1
//
this.openFileDialog1.Filter = "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
this.openFileDialog1.Title = "打開水晶報表";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.crystalReportViewer1});
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "水晶報表瀏覽器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
this.crystalReportViewer1.ReportSource = @openFileDialog1.FileName;
//加載水晶報表,將資源報表綁定到水晶報表查看器
}
catch(Exception error)
{
MessageBox.Show(error.ToString(),"錯誤"); //處理異常錯誤
}
}
}
}
新聞熱點(diǎn)
疑難解答