Step1:先建立 Windows Form 應用程式專案。

2014-12-21_181711  

 

Step2:拖拉 CrystalReportViewer 控制項到 Form1 中。你也可以使用 ReportViewer 控制項。

2014-12-21_182547  

 

Step3:在 專案 (滑鼠右鍵) > 加入 > 新增項目。

2014-12-21_183256  

 

Step4:點選 Reporting > Crystal Reports > 新增。

2014-12-21_183718  

 

Step5:點選 使用報表精靈 > 標準 > 確定。

2014-12-21_184111  

 

Step6:展開 建立新連接 > OLE DB (ADO) > SQL Server Native Client 11.0。

2014-12-21_184552  

 

Step7:輸入伺服器位置及帳密,在選擇要連線的資料。

2014-12-21_185050  

 

Step8:最後點選完成。

2014-12-21_185336  

 

Step9:選取要的資料表。

2014-12-21_185836  

 

Step10:選取要的欄位。全部欄位可直接選取資料表。

2014-12-21_190317  

 

Step11:選取要依據哪個欄位做排序。

2014-12-21_191136  

 

Step12:下一步。

2014-12-21_191528  

 

Step13:選取要篩選的欄位,沒有要篩選就下一步。

2014-12-21_191649  

 

Step14:選擇樣式,完成。

2014-12-21_191904  

 

Step15:最後報表就會產生我們要的欄位。

2014-12-21_192323  

在這個排版畫面中,可以從「欄位總管」拖曳欄位到報表內,亦可以在報表中直接刪除不要的欄位及排版。

2014-12-21_193048  

可以在報表內 滑鼠右鍵 > 預覽報表,或點選 主報表 預覽。

2014-12-21_193316  

 

撰寫程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared.Interop;

namespace CrystalReportDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ReportDocument Report1 = new ReportDocument();

            Report1.Load(Application.StartupPath + @"\CrystalReport1.rpt");

            crystalReportViewer1.ReportSource = Report1;

            crystalReportViewer1.Refresh();
        }
    }
}

執行結果:

2014-12-21_195621  

 

 

Reference:http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-stepbystep.htm

arrow
arrow

    mitblog 發表在 痞客邦 留言(0) 人氣()