Thứ Sáu, 11 tháng 9, 2009

Xây dựng Report với Microsoft Reporting: MicrosoftReportViewer, Client Report Definition .rdlc

Một trong những yêu cầu thường gặp khi xây dựng một phần mềm hoặc website ứng dụng đó là Report. Report có thể được thể hiện trên nhiều dạng khác nhau như danh sách, biểu đồ,…Đồng thời các Report phải được xuất ra các định dạng cơ bản như PDF, Excel, CVS,…Do đó nó cũng đòi hỏi yêu cầu về mặt thiết kế. Có nhiều công cụ lựa chọn để xây dựng Report như ActiveReport, Microsoft Report,…Tôi thích Microsoft Report vì nó mạng lại sự tiện dụng, sức mạnh và linh hoạt.

Microsoft Reporting cung cấp sẵn cho chúng ta các template để thiết kế report như Crystal Report, Report Wizard, Report, đó là các client report definition files. Chúng ta sử dụng chúng để thiết kế, binding và tính toán cho report.

A21319952F68E79E_536_0[1]

Các client report definition này được hiển thị trên WinForms hoặc WebForms thông qua các container có sẵn như MicrosoftReportViewer với đầy đủ các công cụ cần thiết cho việc dàn trang, tìm kiếm, export…rất thuận tiện. Bộ thư việc của Microsoft Reporting cũng hết sức đơn giản bao gồm Microsoft.ReportViewer.Common.dll, Microsoft.ReportViewer.WinForms.dll nếu dùng WinForms và Microsoft.ReportViewer.WebForms.dll nếu dùng cho Web.

A21319952F68E79E_536_1[1]

Ngoài ra Microsoft Reporting cũng hỗ trợ rất nhiều nguồn data khác như như MS SQL, MS Access, Oracle, WebServives, XML, và đặc biệt quan trọng đó là Object Data Source.

  A21319952F68E79E_536_2[1]

Một số tips khi sử dụng Microsoft Reporting.

- Nested Object Binding: Thông thường chúng ta binding sử dụng các Properties của Object. Một vài trường hợp các Properties lại là một object khác. Khi đó chúng ta sử dụng Binding syntax: =Fields!Sales.Value.Customer.Name.

public class CustomerInfo
{
    public string Name {get; set;}
}
 
public class ProductSales
{
    public CustomerInfo Customer {get; set;}
}
 
public class Product
{
    public ProductSales Sales {get; set;}
}







- Collection Object Binding: Trong trường hợp chúng ta cần binding cho một Collections, ví dụ như một Order với nhiều Products. Khi đó chúng ta sử dụng Subreport để thiết kế report kiểu này. Chúng ta thiết kế 2 Report



OrderReport: chứa một Subreport control. Control này bind lấy Product report và define một parameter như Id của Order để truyền đi khi load subreport



ProductReport: Khai báo một parameter để nhận Order id khi load.



Chúng ta implement SubreportProcessing Event để load subreport. Ở đó chúng ta nhận OrderId thông qua string orderId = e.Parameters[0].Values[0].ToString();



và Add data source cho Subreport e.DataSources.Add(new ReportDataSource("SUBREPORT_DATA_SOURCENAME", GetDataSource(orderId )));



- Ẩn hiện Column: Chúng ta sử dụng thuộc tính Visibility để control để quyết định việc ẩn hiện column phụ thuộc vào tham số



A21319952F68E79E_536_3[1]



- Export to PDF hoặc Excel programmatically:






private void generateREPORT(string outputFormat)
        {
            Warning[] warnings = null;
            String[] streamids = null;
            String mimeType = null;
            String encoding = null;
            String extension = null;
            Byte[] bytes = null;
            if (outputFormat == "PDF")
            {
                bytes = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
            }
            else
            {
                bytes = reportViewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);
            }
 
            Response.ContentType = mimeType;
            Response.Clear();
            if (outputFormat == "PDF")
            {
                Response.AddHeader("Content-Disposition", "attachment; filename=RGP_TOTAL.pdf");
            }
            else
            {
                Response.AddHeader("Content-Disposition", "attachment; filename=RGP_TOTAL.xls");
            }
            Response.BinaryWrite(bytes);
            Response.End();
        }


- Landscape Report: mặc định report được xuất ra dưới dạng Portrait (Height lớn hơn (greater) Width). Để xuất report ra dạng Landscape, chúng ta chọn Reports –> Report Properties… Trong Layout tab, thiết lập Width > Height là ok



- Auto Serial Numeber (No.): Thông thường các Report thường dưới dạng Grid với các Rows. Để thiết lập số thứ tự cho từng row (Row No.) chúng ta sử dụng function sau: =RowNumber(Nothing)



Và rất nhiều các kỹ thuật khác.



Chúc các bạn thành công khi sử dụng Microsoft Reporting

Không có nhận xét nào:

Đăng nhận xét