VC#.Net中瀏覽Crystal Report (FROM 阿寶的專欄)

來源:互聯網
上載者:User
大名鼎鼎的 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;  //停靠方式為充滿整個表單//展開+DockPaddingthis.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控制項有很多方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.