Pha xử lý hay của crys-achive
Tải Crystal Report Trong các phiên bản Visual Studio thường có kèm theo công cụ Crystal Report để tạo các báo cáo (report) cho ứng dụng. Tuy nhiên, kể từ Visual 2010 thì công cụ Crystal Report đã bị xoá. Nếu chúng ta muốn dùng Crystal Report thì chúng ta phải tải nó về và cài đặt. Một cách phổ biến là dùng Google để tìm kiếm Crystal Report tương ứng với phiên bản Visual Studio. Ví dụ tôi đang dùng Visual Studio 2012 thì trong Google tôi sẽ gõ từ khoá tìm kiếm và sẽ chọn dòng liên kết đầu tiên (ô đỏ): Khi nhấp vào dòng liên kết đầu tiên (ô đỏ) sẽ đến trang chứa liên kết đến nơi để tải Crystal Report: Nhấp vào dòng liên kết (ô đỏ) để tải Crystal Report: Cài đặt tập tin exe bằng cách nhấp đôi chuột trái vào tập tin vừa tải về: Tạo một dự án Windows Form (VS 2012) Chúng ta sẽ tạo một dự án Windows Form (dùng VB) như sau:
Tôi sẽ minh hoạ với cơ sở dữ liệu (SQL Server) ngocminhtran với bảng Genre có lược đồ và dữ liệu như sau: Lược đồ Dữ liệu Tạo một report đơn giản Đầu tiên chúng ta sẽ tạo một tập tin XSD như một nguồn dữ liệu rỗng để chúng ta có thể dùng với các kiểu dữ liệu. Cách tạo XSD như sau:
Mã VB Imports System.Data.SqlClient Imports CrystalDecisions.CrystalReports.Engine Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim rptDoc As ReportDocument = New ReportDocument() Dim ds As MyDataSample = New MyDataSample() ' tập tin .xsd Dim dt As DataTable = New DataTable() ' Đặt tên cho DataTable dt.TableName = "Crystal Report Example" dt = getAllGeres() 'Gọi phương thức getAllGenres ds.Tables(0).Merge(dt) ' Lấy đường dẫn tập tin crystal report (CrystalReport.rpt) Dim reportPath As String = Application.StartupPath & "\" & "CrystalReport.rpt" rptDoc.Load(reportPath) 'gán dataset đến report viewer rptDoc.SetDataSource(ds) CrystalReportViewer1.ReportSource = rptDoc CrystalReportViewer1.DisplayToolbar = True End Sub Public Function getAllGeres() As DataTable Dim connectionString As String connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=ngocminhtran;Integrated Security=True;" Dim Con As SqlConnection = New SqlConnection(connectionString) Dim cmd As SqlCommand = New SqlCommand() Dim ds As DataSet = Nothing Dim adapter As SqlDataAdapter Try Con.Open() cmd.CommandText = "SELECT * FROM Genre" cmd.CommandType = CommandType.Text cmd.Connection = Con ds = New DataSet() adapter = New SqlDataAdapter(cmd) adapter.Fill(ds, "Genre") Catch ex As Exception Throw New Exception(ex.Message) Finally cmd.Dispose() If Con.State <> ConnectionState.Closed Then Con.Close() End If End Try Return ds.Tables(0) End Function End Class Mã C# using System.Data.SqlClient; using CrystalDecisions.CrystalReports.Engine; public class Form1 { private void Form1_Load(object sender, EventArgs e) { ReportDocument rptDoc = new ReportDocument(); // tập tin .xsd MyDataSample ds = new MyDataSample(); DataTable dt = new DataTable(); // Đặt tên cho DataTable dt.TableName = "Crystal Report Example"; dt = getAllGeres();//Gọi phương thức getAllGenres ds.Tables[0].Merge(dt); // Lấy đường dẫn tập tin crystal report (CrystalReport.rpt) string reportPath = Application.StartupPath + "\\" +"CrystalReport.rpt"; rptDoc.Load(reportPath); //gán dataset đến report viewer rptDoc.SetDataSource(ds); CrystalReportViewer1.ReportSource = rptDoc; CrystalReportViewer1.DisplayToolbar = true; } public DataTable getAllGeres() { string connectionString = null; connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=ngocminhtran;Integrated Security=True;"; SqlConnection Con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); DataSet ds = null; SqlDataAdapter adapter = default(SqlDataAdapter); try { Con.Open(); cmd.CommandText = "SELECT * FROM Genre"; cmd.CommandType = CommandType.Text; cmd.Connection = Con; ds = new DataSet(); adapter = new SqlDataAdapter(cmd); adapter.Fill(ds, "Genre"); } catch (Exception ex) { throw new Exception(ex.Message); } finally { cmd.Dispose(); if (Con.State != ConnectionState.Closed) { Con.Close(); } } return ds.Tables(0); } }
Lời kết Bài viết này chỉ minh hoạ tạo một report đơn giản trong ứng dụng Winform. Có nhiều kiểu report và các cách tạo khác nhau có thể xem thêm bài viết:
|