Export SSRS Report into PDF Format from ASP.NET Application

How to Export SSRS Report into PDF Format from ASP.NET Application
Export SSRS Report into PDF Format from ASP.NET Application
Now a days, it is often required to execute SSRS Report from ASP.NET web application and export that report on the disk in different formats like PDF,XLS etc.
Prerequisite
In this article, I will show you how to Export SSRS Report into PDF from ASP.NET Web Application.
I assumed that you already developed a SSRS Report and deployed it to the Report Server.
Steps
First of all open
Visual Studio 2010
and create a new website or open an existing website. In that add new web page and give appropriate name.Now in your web page add following controls:
One ScriptManager One Button One MicrosoftReportViewer So your web page design will look like following:
No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>.
Now add following code on button’s click event in your .cs file :
MyReportViewer.ProcessingMode = ProcessingMode.Remote; MyReportViewer.ServerReport.ReportServerUrl = new Uri("http://10.88.141.76:8080/ReportServer2012"); // Report Server URL MyReportViewer.ServerReport.ReportPath = "/StartSSRS/PersonAddressDetails"; // Report Name MyReportViewer.ServerReport.Refresh(); Microsoft.Reporting.WebForms.ReportParameter[] reportParameterCollection = new Microsoft.Reporting.WebForms.ReportParameter[1]; reportParameterCollection[0] = new Microsoft.Reporting.WebForms.ReportParameter(); reportParameterCollection[0].Name = "City"; //Parameter Name reportParameterCollection[0].Values.Add("Seattle"); //Parameter Value MyReportViewer.ServerReport.SetParameters(reportParameterCollection); Warning[] warnings; string[] streamids; string mimeType, encoding, extension, deviceInfo; deviceInfo = "True"; byte[] bytes = MyReportViewer.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings); Response.Buffer = true; Response.Clear(); Response.ContentType = mimeType; /* This header is for saving it as an Attachment and popup window should display to to offer save as or open a PDF file Response.AddHeader("Content-Disposition", "attachment; filename=" + extension); */ /* This header is use for open it in browser. Response.AddHeader("content-disposition", "inline; filename=myfile." + extension); Response.BinaryWrite(bytes); */ //Creatr PDF file on disk string pdfPath = @"D:\TempReports\PersonAddressDetails." + extension; // Path to export Report. System.IO.FileStream pdfFile = new System.IO.FileStream(pdfPath, System.IO.FileMode.Create); pdfFile.Write(bytes, 0, bytes.Length); pdfFile.Close(); Response.Flush(); Response.End();
Now set this page as a startup and then press F5. In browser click on Export Report button. It will export report on the disk.
Now browse to path on which Report is Exported. You will see report over there.
Similarly, If you want to export report in excel, make following changes in line no 18 of above code:
byte[] bytes = MyReportViewer.ServerReport.Render(“EXCEL”, null, out mimeType, out encoding, out extension, out streamids, out warnings);
hope this can help you , Let me know if you have any questions or comments.