This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region 報表繫結(下載) | |
/// <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(); | |
} | |
#endregion | |
#region 報表繫結 | |
/// <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) | |
{ | |
string Path = ConfigurationManager.AppSettings["RptPath"].ToStr(); | |
Path = (Path == "") ? "~/bin/Report" : Path;//預設值 | |
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(); | |
} | |
#endregion |