using Reportviewer, export data in pdf and webarchive format

So many people ask about this and finally I came out with solution that you can directly use to export data in pdf and webarchive format using ReportViewer.


Export in webarhive format
==================
                   byte[] bytes = ReportViewer1.LocalReport.Render("HTML4.0", null, out mimeType, out encoding, out extension, out streamids, out warnings);
                    Response.Clear();
                    Response.ContentType = "text/html";
                    Response.AddHeader("Content-Length", bytes.Length.ToString());
                    Response.AddHeader("Content-Disposition", "OnlyHtmlInline; filename=" + "PrintHTML.mhtml");
                    Response.BinaryWrite(bytes);
                    Response.End();
                    Response.Close();
You can also use FileStream like below:
                   byte[] bytes = ReportViewer1.LocalReport.Render( "MHTML", null, out mimeType, out encoding,  out extension, out streamids, out warnings);
                     FileStream fs = new FileStream(@"c:\output.mht", FileMode.Create);
                     fs.Write(bytes, 0, bytes.Length);
                     fs.Close();
Export in pdf format
==============                 
                   byte[] bytes = ReportViewer1.LocalReport.Render("Pdf", null, out mimeType, out encoding, out extension, out streamids, out warnings);
                   Response.Clear();
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Length", bytes.Length.ToString());
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + "PrintPDF.pdf");
                    Response.BinaryWrite(bytes);
                    Response.End();
                    Response.Close();

No comments:

Post a Comment

Flipkart