Crystal Reports (水晶報表) 內嵌為 Visual Studio .NET 標準報表工具,升級為 Crystal Reports for Visual Studio .NET。其優勢在於:.NET 平台輕松創建交互式高質量報表;支持 Windows/Web 雙平台承載,可發布為報表 Web Service;Web 應用中用戶可深化鑽取圖表、篩選信息,報表本質上成為與其他控件交互的控件。本文演示在 Windows Forms 應用中構建報表瀏覽器。
Text="水晶報表瀏覽器", StartPosition=CenterScreenCrystalReportViewer (命名為 crystalReportViewer1)Button (命名為 button1, Text="打開報表")OpenFileDialog (命名為 openFileDialog1)crystalReportViewer1.Dock = DockStyle.Fill;
// 底部預留 50 像素給按鈕,四周 5 像素邊距
crystalReportViewer1.DockPadding.Bottom = 50;
crystalReportViewer1.DockPadding.Left = 5;
crystalReportViewer1.DockPadding.Right = 5;
crystalReportViewer1.DockPadding.Top = 5;
crystalReportViewer1.ReportSource = null; // 初始不載入報表
button1.Anchor = AnchorStyles.Bottom;
button1.Text = "打開報表";
openFileDialog1.Filter = "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
openFileDialog1.Title = "打開水晶報表";
private void button1_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// 核心:將選中的 .rpt 文件綁定到 Viewer
crystalReportViewer1.ReportSource = openFileDialog1.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "錯誤");
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CrystalDecisions.Windows.Forms; // 關鍵命名空間
namespace WindowsApplication10
{
public class Form1 : System.Windows.Forms.Form
{
private CrystalReportViewer crystalReportViewer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && components != null) components.Dispose();
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.crystalReportViewer1 = new 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 = 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 Size(292, 273);
this.crystalReportViewer1.TabIndex = 0;
// button1
this.button1.Anchor = AnchorStyles.Bottom;
this.button1.Location = new Point(104, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "打開報表";
this.button1.Click += new EventHandler(this.button1_Click);
// openFileDialog1
this.openFileDialog1.Filter = "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
this.openFileDialog1.Title = "打開水晶報表";
// Form1
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.Controls.AddRange(new Control[] { this.button1, this.crystalReportViewer1 });
this.Name = "Form1";
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "水晶報表瀏覽器";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main() => Application.Run(new Form1());
private void button1_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
crystalReportViewer1.ReportSource = openFileDialog1.FileName;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "錯誤");
}
}
}
}
...\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Samples\Reports\Feature Examples\Chart.rptDataSet / DataTable 動態生成報表數據源ParameterFields 傳遞查詢條件CrystalReportWebFormViewer 在 ASP.NET 中展示本文展示了在 WinForms 中集成 Crystal Reports 的最小化完整流程:環境準備、控件佈局、屬性配置、文件綁定。掌握這一基礎,即可向動態數據報表、參數化查詢、Web 發布、多格式導出等企業級報表應用進階。