2013年1月10日 星期四

如何使用LocalReport - WebForm (3)


實作部分,報表繫結與報表下載

/// <summary>
        /// 報表繫結
        /// </summary>
        /// <param name="rpt">報表檢視器</param>
        /// <param name="dsName">資料集名稱</param>
        /// <param name="RptPath">報表路徑</param>
        /// <param name="dt">繫結之資料表</param>
        public static void ReportBind(
            ReportViewer rpt, 
            string dsName, 
            string RptPath, 
            DataTable dt)
        {
            //Report存放路徑,EX:~/bin/Report
            string Path = ConfigurationManager.AppSettings["RptPath"].ToString();
            rpt.LocalReport.DataSources.Clear();
            rpt.ProcessingMode = ProcessingMode.Local;
            rpt.LocalReport.ReportPath = 
                HttpContext.Current.Server.MapPath(
                string.Format("{0}\\{1}", Path, RptPath));
            rpt.LocalReport.DataSources.Add(new ReportDataSource(dsName, dt));
            rpt.LocalReport.Refresh();

        }
        /// <summary>
        /// 報表繫結
        /// </summary>
        /// <param name="rpt">報表檢視器</param>
        /// <param name="dsName">資料集名稱</param>
        /// <param name="RptPath">報表路徑</param>
        /// <param name="dt">繫結之資料表</param>
        /// <param name="PType">PDF,Excel,Word,Image</param>
        public static void ReportExport(
            ReportViewer rpt,
            string dsName,
            string RptPath,
            DataTable dt,
            string PType)
        {
            ReportBind(rpt, dsName, RptPath, dt);
            //
            Microsoft.Reporting.WebForms.Warning[] tWarnings;
            string[] tStreamids;
            string tMimeType;
            string tEncoding;
            string tExtension;
            //呼叫ReportViewer.LoadReport的Render function,將資料轉成想要轉換的格式,並產生成Byte資料
            byte[] tBytes =
                rpt.LocalReport.Render(
                PType, null, out tMimeType,
                out tEncoding, out tExtension,
                out tStreamids, out tWarnings);
            //將Byte內容寫到Client
            string DisplayName =
                rpt.LocalReport.DisplayName == "" ?
                "report" : rpt.LocalReport.DisplayName;
            if (HttpContext.Current.Request.Browser.Browser == "IE")
            {
                DisplayName =
                    HttpContext.Current.Server.UrlPathEncode(
                    DisplayName);
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = tMimeType;
            HttpContext.Current.Response.
                AppendHeader("Content-Disposition",
                String.Format("attachment; filename={0}.{1}",
                DisplayName, tExtension));
            HttpContext.Current.Response.BinaryWrite(tBytes);
            HttpContext.Current.Response.End();
        }

建置或發行時需要注意的設定:
  • 報表相關的DLL需一設定複製到本機為True
  • 報表專案的報表檔,需要也需要設定為複製
image
Web.config設定
  • 報表繫結程式中,取得報表檔的方法會參考此路徑,因為報表專案在發行後會將報表檔案(RDLC)複製到bin目錄下。
  • 在此我們可以將此發行後的檔案隨意搬移,如將檔案移到Report的資料夾,就可以自行定義。
image
顯示報表與下載程式碼如下:

protected void btnShowReport_Click(object sender, EventArgs e)
{
UtilityUI.ReportBind(
this.ReportViewer1
,"TB1"
,"rpt110.rdlc"
, GetReportData()
);
}

protected void btnDownload_Click(object sender, EventArgs e)
{
this.ReportViewer1.LocalReport.DisplayName = "訂單明細";
UtilityUI.ReportExport(
this.ReportViewer1
, "TB1"
, "rpt110.rdlc"
, GetReportData()
,"PDF"
);
}


結果畫面如下:
image

沒有留言:

張貼留言