2012年2月12日 星期日

RDLC (三) 第一支報表

這篇我們將介紹如何撰寫第一支RDLC報表,首先我們先來看一下整個Web站台的目錄結構
以下說明各目錄或檔案的功用
  • 在App_Code底下我們建立一個Report的目錄,用來儲放報表檔使用的資料集物件。
  • 根目錄底下的Report目錄則儲放報表檔(Customers.rdlc)。
  • first.aspx嵌入ReportViewer控制項。
  • first.aspx.cs處理ReportViewer該讀取那個報表檔及資料繫結部份。
接著我們正式開始撰寫報表,首先我們先建立資料集物件,建立資料集物件的目的在於作為報表檔的Report Data,在App_Code目錄下的Report目錄上點選滑鼠右鍵新增項目
選擇資料集並將檔名命名為Customers.xsd後按下新增按鈕,
接著我們將Toolbox裡的DataTable拖曳至設計畫面並增加相關欄位
在完成資料集後我們進行報表設計,在根目錄下的Report目錄上按滑鼠右鍵新增報表,並將報表檔名命名為Customers.rdlc
進入報表設計畫面後我們先將事先定義好的資料集加入Report Data,如果沒有看到Report Data畫面,則需要利用滑鼠點擊報表設計畫面,然後同時按下Ctrl+Alt+D鍵
在Data Source下拉選單挑選Customers及在Available datasets下拉選單挑選Customer並將Name命名為Customer
加入資料集後將頁籤切換至Toolbox,將Table控制項拖曳到報表畫面後新增一個欄位
再依序將Report Data中的Customer資料集欄位拖曳至設計畫面裡Table的Data區塊
完成報表設計後我們進行網頁程式的撰寫,以下為aspx、aspx.cs內容及執行畫面
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="first.aspx.cs" Inherits="first" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <rsweb:ReportViewer ID="rptviewer" runat="server" Width="100%" Height="600px" />        
    </div>
    </form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class first : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;");
        cmd.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle FROM Customers";
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        try
        {
            da.Fill(dt);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Connection.Close();
        }

        rptviewer.LocalReport.ReportPath = Server.MapPath("/WebSite/Report/Customers.rdlc");
        rptviewer.LocalReport.DataSources.Clear();
        rptviewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("Customer", dt));
        rptviewer.LocalReport.Refresh();
    }
}


程式說明
  • LocalReport.ReportPath是指定報表所在路徑。
  • ReportDataSource("Customer", dt))是將dt(實際產出的資料)與資料集對應。
  • LocalReport.Refresh()使報表將資料render。

針對程式部份有幾處需要注意的
  1. aspx程式需要包含ScriptManager控制項,否則會產生錯誤。
  2. aspx.cs程式需將報表相關程式放在Page_Init事件,否則執行時畫面會一直refresh。
  3. dt的欄位名稱與資料集中的欄位名稱不同時,需要特別處理。