首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Visual C# 應用 Crystal Reports 打造專業報表查看器

     編輯:關於C語言

前言:Crystal Reports 在 .NET 中的地位

Crystal Reports (水晶報表) 內嵌為 Visual Studio .NET 標準報表工具,升級為 Crystal Reports for Visual Studio .NET。其優勢在於:.NET 平台輕松創建交互式高質量報表;支持 Windows/Web 雙平台承載,可發布為報表 Web Service;Web 應用中用戶可深化鑽取圖表、篩選信息,報表本質上成為與其他控件交互的控件。本文演示在 Windows Forms 應用中構建報表瀏覽器。

開發環境要求

  • 必須安裝 Visual Studio .NET 集成開發環境 (含 Crystal Reports 控件)
  • 僅安裝 .NET Framework SDK 不足夠,缺少報表控件

步驟 1:創建項目與窗體佈局

  1. 新建 Visual C# Windows 應用程序 項目
  2. 設置 Form1:Text="水晶報表瀏覽器", StartPosition=CenterScreen
  3. 從工具箱拖入三個控件:
    • CrystalReportViewer (命名為 crystalReportViewer1)
    • Button (命名為 button1, Text="打開報表")
    • OpenFileDialog (命名為 openFileDialog1)

步驟 2:控件屬性精細配置

CrystalReportViewer 佈局佔滿窗體

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; // 初始不載入報表

Button 固定底部居中

button1.Anchor = AnchorStyles.Bottom;
button1.Text = "打開報表";

OpenFileDialog 過濾報表文件

openFileDialog1.Filter = "Crystal Report (*.rpt)|*.rpt|所有文件(*.*)|*.*";
openFileDialog1.Title = "打開水晶報表";

步驟 3:核心代碼 - 打開報表邏輯

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(), "錯誤");
    }
}

完整 Form1 代碼 (含設計器生成部分)

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(), "錯誤");
            }
        }
    }
}

步驟 4:運行測試

  1. 按 Ctrl+F5 運行
  2. 點擊「打開報表」選擇 .rpt 文件
  3. 測試樣本報表路徑:
    ...\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Samples\Reports\Feature Examples\Chart.rpt

進階應用方向

  • 動態數據綁定:配合 DataSet / DataTable 動態生成報表數據源
  • 參數化報表:通過 ParameterFields 傳遞查詢條件
  • Web 部署:使用 CrystalReportWebFormViewer 在 ASP.NET 中展示
  • 導出功能:支持 PDF、Excel、Word 等格式導出
  • 打印控制:程序化控制打印機設置、份數、頁碼範圍

總結

本文展示了在 WinForms 中集成 Crystal Reports 的最小化完整流程:環境準備、控件佈局、屬性配置、文件綁定。掌握這一基礎,即可向動態數據報表、參數化查詢、Web 發布、多格式導出等企業級報表應用進階。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved