限制ReportViewr匯出格式(二)

限制ReportViewr匯出格式(二)

另一個限制ReportViewr匯出格式,隱藏是在網頁上原有的匯出功能,自己再一個匯出的按鈕,讓使用者匯出檔案

1.新增個函式

protected void Export(String SFormat, String FileName)
{
//'依照SFormat傳回前端儲存()
//'SFormat: "PDF","EXCEL"
byte[] result = null;
String[] streamid = null;
String mimetype = null;
String Encoding = null;
String extension = null;
String deviceInfo = null;
Warning[] warnings = null;
result = ReportViewer.ServerReport.Render(SFormat, deviceInfo, out mimetype, out Encoding, out extension, out streamid, out warnings);
Response.ClearContent();
Response.AppendHeader("content-length", result.Length.ToString());
if (FileName.Length > 0)
{
// Response.AppendHeader("content-disposition", "attachment;filename=" + Server.UrlPathEncode(FileName));
if (Request.Browser.Browser == "IE")
{
FileName = Server.UrlPathEncode(FileName);
}
string strContentDisposition = String.Format("{0}; filename=\"{1}\"", "attachment", FileName);
Response.AddHeader("Content-Disposition", strContentDisposition);
}
Response.ContentType = mimetype;
Response.BinaryWrite(result);
Response.Flush();
Response.End();
}

2.呼叫方法

protected void btnExport_Click(object sender, EventArgs e)
{
Export("EXCEL","aa.xls");
}