實作部分,報表繫結與報表下載
///
/// 報表繫結
///
/// 報表檢視器
/// 資料集名稱
/// 報表路徑
/// 繫結之資料表
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();
}<br>
/// <summary><br> /// 報表繫結<br> /// </summary><br> /// <param name="rpt">報表檢視器</param><br> /// <param name="dsName">資料集名稱</param><br> /// <param name="RptPath">報表路徑</param><br> /// <param name="dt">繫結之資料表</param><br> /// <param name="PType">PDF,Excel,Word,Image</param><br> public static void ReportExport(<br> ReportViewer rpt, <br> string dsName, <br> string RptPath, <br> DataTable dt, <br> string PType) <br> {<br> ReportBind(rpt, dsName, RptPath, dt);<br> //<br> Microsoft.Reporting.WebForms.Warning[] tWarnings;<br> string[] tStreamids;<br> string tMimeType;<br> string tEncoding;<br> string tExtension;<br> //呼叫ReportViewer.LoadReport的Render function,將資料轉成想要轉換的格式,並產生成Byte資料<br> byte[] tBytes = <br> rpt.LocalReport.Render(<br> PType, null, out tMimeType, <br> out tEncoding, out tExtension, <br> out tStreamids, out tWarnings);<br> //將Byte內容寫到Client<br> string DisplayName = <br> rpt.LocalReport.DisplayName == "" ? <br> "report" : rpt.LocalReport.DisplayName;<br> if (HttpContext.Current.Request.Browser.Browser == "IE")<br> {<br> DisplayName = <br> HttpContext.Current.Server.UrlPathEncode(<br> DisplayName);<br> }<br> HttpContext.Current.Response.Clear();<br> HttpContext.Current.Response.ContentType = tMimeType;<br> HttpContext.Current.Response.<br> AppendHeader("Content-Disposition", <br> String.Format("attachment; filename={0}.{1}", <br> DisplayName, tExtension));<br> HttpContext.Current.Response.BinaryWrite(tBytes);<br> HttpContext.Current.Response.End();<br> }
建置或發行時需要注意的設定:
- 報表相關的DLL需一設定複製到本機為True
- 報表專案的報表檔,需要也需要設定為複製

Web.config設定
- 報表繫結程式中,取得報表檔的方法會參考此路徑,因為報表專案在發行後會將報表檔案(RDLC)複製到bin目錄下。
- 在此我們可以將此發行後的檔案隨意搬移,如將檔案移到Report的資料夾,就可以自行定義。

顯示報表與下載程式碼如下:
protected void btnShowReport_Click(object sender, EventArgs e)<br>{<br>UtilityUI.ReportBind(<br>this.ReportViewer1<br>,"TB1"<br>,"rpt110.rdlc"<br>, GetReportData()<br>);<br>}
protected void btnDownload_Click(object sender, EventArgs e)<br>{<br>this.ReportViewer1.LocalReport.DisplayName = "訂單明細";<br>UtilityUI.ReportExport(<br>this.ReportViewer1<br>, "TB1"<br>, "rpt110.rdlc"<br>, GetReportData()<br>,"PDF"<br>);<br>
}<br>
結果畫面如下:
