大名鼎鼎的 Crystal Reports(水晶報表)已內嵌為微軟 Visual Studio .NET的標準報表工具,同時升級到 Crystal Reports for Visual Studio .NET。它的優點是:1、在 .NET 平臺能夠輕松創建交互式的、高質量顯現的報表內容,這也是 Crystal Reports 長期以來所具有的主要優勢;2、使用 Crystal Reports for Visual Studio .NET,可以在 Web 平臺和 Windows 平臺上承載報表,并將 Crystal 報表作為報表 Web 服務在 Web 服務器上發布;3、利用Crystal Report 創建的Web應用程序,使用戶可以深化圖表并根據需要篩選信息。在 .NET 中,圖表實際上就是一個與應用程序中其他控件進行交互的 Crystal 報表。在這里我向大家介紹如何在 Windows 窗體應用程序中瀏覽水晶報表。
設計步驟:
1、軟件環境:要求系統已安裝Visual Studio .Net 集成開發系統,僅裝有 .Net Framework SDk 不能實現本例效果,成為 .Net Framework SDK 沒有水晶報表控件。
2、新建一個 Visual C# 項目 Windows 應用程序,設置Form1的Text="水晶報表瀏覽器",StartPosition=CenterScreen //程序開始出現在屏幕中央,其它屬性均保持默認;
3、從工具箱拖入一個CrystalReportViewer,一個Button,一個openFileDialog,到窗體。
整個設計器的窗體布局只需將button1置于窗體底邊中部,不再需要其它多余布局。
設置crystalReportViewer1的屬性如下:
this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill; //停靠方式為充滿整個窗體 //展開+DockPadding this.crystalReportViewer1.DockPadding.Bottom = 50; //底部空出放置Button的區域 this.crystalReportViewer1.DockPadding.Left = 5; this.crystalReportViewer1.DockPadding.Right = 5; this.crystalReportViewer1.DockPadding.Top = 5; this.crystalReportViewer1.ReportSource = null; //先不載入報表資源
設置button1的屬性如下:
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom; //與窗體的下邊保持固定 this.button1.Text = "打開報表";
openFileDialog1是打開文件的控件,設置其屬性如下:
this.openFileDialog1.Filter = "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*"; //提供打開文件對話框的文件類型, 默認類型就是此字符串的最前一種定義的類型 this.openFileDialog1.Title = "打開水晶報表"; //打開文件對話框的標題
4、雙擊button1,添加button1_Click點擊事件:
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運行吧。
可以瀏覽你系統內現有的報表實例:
...\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> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗體設計器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 // } /// <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> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </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> /// 應用程序的主入口點。 /// </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(),"錯誤"); //處理異常錯誤 } } } }
事實上,將報表綁定到CrystalReportViewer控件有很多
|