Paresh Jagatia’s Weblog

Coolest things on ASP.NET Web developement…

Export ReportViewer Report to PDF, Excel, HTML

Posted by pareshjagatia on May 3, 2008

The following code helps exporting the Report into PDF and Excel formats using the Render method of the ServerReport. Use below code after the report is loaded (refresh() is called on the reportviewer control).

1 string mimeType;
2  string encoding;
3  string fileNameExtension;
4  Warning[] warnings;
5  string[] streamids;
6  byte[] exportBytes = reportViewer.ServerReport.Render(PDF, null, out mimeType, out encoding, out fileNameExtension, out streamids, out warnings);
7  HttpContext.Current.Response.Buffer = true;
8  HttpContext.Current.Response.Clear();
9  HttpContext.Current.Response.ContentType = mimeType;
10  HttpContext.Current.Response.AddHeader(content-disposition, attachment; filename=ExportedReport. + fileNameExtension);
11  HttpContext.Current.Response.BinaryWrite(exportBytes);
12  HttpContext.Current.Response.Flush();
13  HttpContext.Current.Response.End();

 

To get Excel file use Excelas the parameter in the Render method.
Sometimes you might need to send the Report content into email. To Achieve this you can use the above code where you need to pass HTML4.0as the first parameter to the Render method. After you get the byte array from the Render method you can convert it to string as below

 

2  1 string mimeType;
2  string encoding;
3  Warning[] warnings;
4  string[] streamids;
5  string fileNameExtension;
6  byte[] htmlBytes = PerksReportViewer.ServerReport.Render(HTML4.0, null, out mimeType, out encoding, out fileNameExtension, out streamids, out warnings);
7  string reportHtml = System.Text.Encoding.UTF8.GetString(htmlBytes);
8  return reportHtml;
About these ads

4 Responses to “Export ReportViewer Report to PDF, Excel, HTML”

  1. mahoni said

    Perfect solution, but how could i quit the confirm dialog: open-save-cancel?

    Thanks!

  2. Agustín said

    Thanks a lot friend, you save my life!!!

    Keep up working like this!!!

  3. Tom said

    Hi, I spend many time to resolv this problem.
    Thanks, great job ;)

  4. document said

    Heya i am for the first time here. I came across this board and I to find
    It really helpful & it helped me out much. I am hoping to
    present one thing again and aid others such as you helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: