用代码实现ReportViewer的导出功能

public partial class _Default : System.Web.UI.Page
{

String m_currentReport;

private void saveRptAs(String s_rptType)
{

Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
// string deviceInfo;

byte[] bytes = ReportViewer1.ServerReport.Render(
s_rptType, 
nullout mimeType, out encoding, out extension,
out streamids, out warnings);

/*
FileStream stream = File.OpenWrite(@"C:\Documents and Settings\michael.shorten\Local 
Settings\Temp\sample.pdf");
stream.Write(bytes, 0, bytes.Length);
stream.Close();
*/

Response.Buffer 
= true;
Response.Clear();
Response.ContentType 
= mimeType;
Response.AddHeader(
"content-disposition""attachment; filename=sample."+extension);
Response.BinaryWrite(bytes);
Response.Flush();

}

protected void Page_Load(object sender, EventArgs e)
{

if (!(Request.QueryString["rpt"]== null))
{
m_currentReport 
= Request.QueryString["rpt"].ToString();
showReport(m_currentReport);
}

}

private void showReport(string sReportPath)
{

// Set Processing Mode

ReportViewer1.ProcessingMode 
= ProcessingMode.Remote;

// Set Report Server and path

ReportViewer1.ServerReport.ReportServerUrl 
= new Uri(http://localhost/ReportServer);
ReportViewer1.ServerReport.ReportPath = sReportPath;

}

protected void btnSaveExcel_Click(object sender, EventArgs e)
{

saveRptAs(
"Excel");

}

protected void btnSavePDF_Click(object sender, EventArgs e)
{

saveRptAs(
"PDF");

}

}

From: https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=139828&SiteID=1
posted @ 2007-10-30 22:33  emanlee  阅读(2821)  评论(0编辑  收藏  举报