NEWS

[DEVEXPRESS] Hướng dẫn tạo Report động - Tutorial create dynamic xtraReport in VB.NET

[DEVEXPRESS] Hướng dẫn tạo Report động  - Tutorial create dynamic xtraReport in VB.NET
Đăng bởi: Thảo Meo - Lượt xem: 35506 12:55:07, 24/07/2016DEVEXPRESS   In bài viết

Hôm nay, mình hướng dẫn các bạn cách tạo report động, sử dụng Xtra Report trong Devexpress. Như các bạn đã biết, để thiết kế report mình thường tạo những report tĩnh, tất là dữ liệu thay đổi được còn các field name thì thường mình cố định. Vì thế, nó sẽ rất bất tiện, nếu ta thiết kế nhiều mẫu report cho mỗi người dùng.

Vd: Nhân viên A muốn có report hiện đầy đủ thông tin và preview ở trang đứng. Nhưng nhân viên B lại muốn hiển thị ít thông tin, chỉ hiển thị những thông tin cần thiết, và in trang nằm.

Thì mình phải thiết kế nhiều report cho mỗi người dùng khác nhau. Hay thiết kế report có template chung nhưng lại khác table name, thì mình cũng phải mất nhiều thời gian để thiết kế lại những report cho mỗi người cần sử dụng. Chính vì vậy, hôm nay, mình viết hướng dẫn này giúp các bạn thiết kế nhanh Report, cho người dùng có thể tùy biến cao.

Giao diện chương trình demo:

Dynamic XtraReport Devexpress in vb.net

Ở demo này, mình tích hợp cho người dùng có thể chèn tiêu đề report một cách linh động và cho phép chọn trang báo cáo xem kiểu đứng hay nằm.

Mình có thể hợp thêm Watermask dưới dạng Text hay Picture. 

Chương trình demo trên: mình sử dụng Visual Studio 2015 và Devexpress 15.2. Vì thế, các bạn muốn chạy ứng dụng hay cài đặt những chương trình trên, các bạn có thể tìm kiếm tại trang web http://laptrinhvb.net ở thanh tìm kiếm trên cùng nha.

Video Demo:

Bây giờ, mình bắt đầu code

1. Import thư viện vào

Imports System
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
Imports System.Data.SqlClient
Imports DevExpress.XtraPrinting.Drawing
Imports DevExpress.XtraSplashScreen
Imports System.Threading

2. Viết các hàm kết nối và lấy database truy vấn.

Dim con As New SqlConnection
    Public Sub Taoketnoi()
        Dim str As String = "Data Source=.;Initial Catalog=NORTHWND;Integrated Security=True"
        con.ConnectionString = str
        con.Open()
    End Sub
    Public Sub Dongketnoi()
        con.Close()
    End Sub
    Public Function LayDulieu(query As String) As DataSet
        Taoketnoi()
        Dim dt As New DataSet
        Dim da As New SqlDataAdapter
        da.SelectCommand = New SqlCommand(query, con)
        da.Fill(dt)
        Dongketnoi()
        Return dt
    End Function

3. Viết hàm tạo pageHeader, pageDetail, reportHeader và reportFooter

Public Sub InitBands(ByVal rep As XtraReport)
        ' Create bands
        Dim detail As New DetailBand()
        Dim pageHeader As New PageHeaderBand()
        Dim reportHeader As New ReportHeaderBand()
        Dim reportFooter As New ReportFooterBand()
        reportHeader.Height = 40
        detail.Height = 20
        reportFooter.Height = 380
        pageHeader.Height = 20
        ' Place the bands onto a report
        rep.Bands.AddRange(New DevExpress.XtraReports.UI.Band() {reportHeader, detail, pageHeader, reportFooter})
    End Sub

4. Viết hàm tạo XRTable trong xtraReport Devexpress

Public Sub InitDetailsBasedonXRTable(ByVal rep As XtraReport)
        Dim ds As DataSet = (CType(rep.DataSource, DataSet))
        Dim colCount As Integer = ds.Tables(0).Columns.Count
        Dim colWidth As Integer = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right)) / colCount
        rep.Margins = New System.Drawing.Printing.Margins(20, 20, 20, 20)
        Dim tieude As New XRLabel
        tieude.Text = txtTieude.Text
        tieude.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter
        tieude.ForeColor = Color.Orange
        tieude.Font = New Font("Tahoma", 20, FontStyle.Bold, GraphicsUnit.Pixel)
        tieude.Width = Convert.ToInt32(rep.PageWidth - 50)
        ' Create a table to represent headers
        Dim tableHeader As New XRTable()
        tableHeader.Height = 40
        tableHeader.BackColor = Color.Gray
        tableHeader.ForeColor = Color.White
        tableHeader.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter
        tableHeader.Font = New Font("Tahoma", 12, FontStyle.Bold, GraphicsUnit.Pixel)
        tableHeader.Width = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right))
        tableHeader.Padding = New DevExpress.XtraPrinting.PaddingInfo(5, 5, 5, 5, 100.0F)
        Dim headerRow As New XRTableRow()
        headerRow.Width = tableHeader.Width
        tableHeader.Rows.Add(headerRow)
        tableHeader.BeginInit()
        ' Create a table to display data
        Dim tableDetail As New XRTable()
        tableDetail.Height = 20
        tableDetail.Width = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right))
        tableDetail.Font = New Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Pixel)
        Dim detailRow As New XRTableRow()
        detailRow.Width = tableDetail.Width
        tableDetail.Rows.Add(detailRow)
        tableDetail.Padding = New DevExpress.XtraPrinting.PaddingInfo(5, 5, 5, 5, 100.0F)
        tableDetail.BeginInit()
        ' Create table cells, fill the header cells with text, bind the cells to data
        For i As Integer = 0 To colCount - 1
            Dim headerCell As New XRTableCell()
            headerCell.Text = ds.Tables(0).Columns(i).Caption
            Dim detailCell As New XRTableCell()
            detailCell.DataBindings.Add("Text", Nothing, ds.Tables(0).Columns(i).Caption)
            If i = 0 Then
                headerCell.Borders = DevExpress.XtraPrinting.BorderSide.Left Or DevExpress.XtraPrinting.BorderSide.Top Or DevExpress.XtraPrinting.BorderSide.Bottom
                detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Left Or DevExpress.XtraPrinting.BorderSide.Top Or DevExpress.XtraPrinting.BorderSide.Bottom
            Else
                headerCell.Borders = DevExpress.XtraPrinting.BorderSide.All
                detailCell.Borders = DevExpress.XtraPrinting.BorderSide.All
            End If
            If i = 0 Then
                headerCell.Width = 50
                detailCell.Width = 50
                detailCell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter
            ElseIf i = 1 Then
                headerCell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft
                headerCell.Width = 130
                detailCell.Width = 130
            ElseIf i = 2 Then
                headerCell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft
                headerCell.Width = 70
                detailCell.Width = 70
            ElseIf i = 4 Then
                headerCell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft
                headerCell.Width = 145
                detailCell.Width = 145
            Else
                headerCell.Width = colWidth
                detailCell.Width = colWidth
            End If
            detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Bottom Or DevExpress.XtraPrinting.BorderSide.Left Or DevExpress.XtraPrinting.BorderSide.Right

            ' Place the cells into the corresponding tables
            headerRow.Cells.Add(headerCell)
            detailRow.Cells.Add(detailCell)
        Next i
        tableHeader.EndInit()
        tableDetail.EndInit()
        ' Place the table onto a report's Detail band
        rep.Bands(BandKind.ReportHeader).Controls.Add(tieude)
        rep.Bands(BandKind.PageHeader).Controls.Add(tableHeader)
        rep.Bands(BandKind.Detail).Controls.Add(tableDetail)
    End Sub

Ở các câu lệnh trên các bạn có thể chỉnh sửa để cấu hình height và width từng field name cho hợp lý, cũng như canh chỉnh margin, padding hay font chữ...

6. Viết hàm tạo Watermask dạng text

Sub SetTextWatermark(ps As XtraReport)
        ' Create the text watermark.
        Dim textWatermark As New Watermark()

        ' Set watermark options.
        textWatermark.Text = "HTTP://LAPTRINHVB.NET"
        textWatermark.TextDirection = DirectionMode.ForwardDiagonal
        textWatermark.Font = New Font(textWatermark.Font.FontFamily, 40)
        textWatermark.ForeColor = Color.DodgerBlue
        textWatermark.TextTransparency = 150
        textWatermark.ShowBehind = False
        textWatermark.PageRange = "1,3-5"

        ' Add the watermark to a document.
        ps.Watermark.CopyFrom(textWatermark)
    End Sub

7. Viết hàm tạo Watermask dạng hình ảnh (picture)

Public Sub SetPictureWatermark(ps As XtraReport)
        ' Create the picture watermark.
        Dim pictureWatermark As New Watermark()

        ' Set watermark options.
        pictureWatermark.Image = Bitmap.FromFile("logo.png")
        pictureWatermark.ImageAlign = ContentAlignment.MiddleCenter
        pictureWatermark.ImageTiling = False
        pictureWatermark.ImageViewMode = ImageViewMode.Zoom
        pictureWatermark.ImageTransparency = 150
        pictureWatermark.ShowBehind = True
        'pictureWatermark.PageRange = "2,4"

        ' Add the watermark to a document.
        ps.Watermark.CopyFrom(pictureWatermark)
    End Sub

8. Viết sự kiện cho nút tạo Dynamic Report theo câu truy vấn query và những lựa chọn page setup hay watermask ở dạng nào

Private Sub btnCreateReport_Click(sender As Object, e As EventArgs) Handles btnCreateReport.Click
        SplashScreenManager.ShowForm(Me, GetType(loading), True, True, False)
        ' Create XtraReport instance
        Dim rep As New XtraReport()
        If grpPagesetup.Text = 0 Then
            rep.PaperKind = System.Drawing.Printing.PaperKind.A4
        Else
            rep.PaperKind = System.Drawing.Printing.PaperKind.A4Rotated
        End If
        If grpWaterMark.Text = 0 Then
            SetTextWatermark(rep)
        Else
            SetPictureWatermark(rep)
        End If

        rep.DataSource = LayDulieu(txtQuery.Text)
        rep.DataMember = (CType(rep.DataSource, DataSet)).Tables(0).TableName
        InitBands(rep)
        InitDetailsBasedonXRTable(rep)
        rep.ShowRibbonPreviewDialog()
        SplashScreenManager.CloseForm(False)
    End Sub

Chúc các bạn thành công. Mọi câu hỏi thắc mắc đến bài viết xin truy cập http://hoidap.laptrinhvb.net để được support.yes

Link download

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn tạo Report động  - Tutorial create dynamic xtraReport in VB.NET
Đăng bởi: Thảo Meo - Lượt xem: 35506 12:55:07, 24/07/2016DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.