Step1:先建立 Windows Form 應用程式專案。
Step2:拖拉 CrystalReportViewer 控制項到 Form1 中。你也可以使用 ReportViewer 控制項。
Step3:在 專案 (滑鼠右鍵) > 加入 > 新增項目。
Step4:點選 Reporting > Crystal Reports > 新增。
Step5:點選 使用報表精靈 > 標準 > 確定。
Step6:展開 建立新連接 > OLE DB (ADO) > SQL Server Native Client 11.0。
Step7:輸入伺服器位置及帳密,在選擇要連線的資料。
Step8:最後點選完成。
Step9:選取要的資料表。
Step10:選取要的欄位。全部欄位可直接選取資料表。
Step11:選取要依據哪個欄位做排序。
Step12:下一步。
Step13:選取要篩選的欄位,沒有要篩選就下一步。
Step14:選擇樣式,完成。
Step15:最後報表就會產生我們要的欄位。
在這個排版畫面中,可以從「欄位總管」拖曳欄位到報表內,亦可以在報表中直接刪除不要的欄位及排版。
可以在報表內 滑鼠右鍵 > 預覽報表,或點選 主報表 預覽。
撰寫程式碼:
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();
}
}
}
執行結果:
Reference:http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-stepbystep.htm
