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.

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.

1. 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.

2. Now in your web page add following controls:

  • One  ScriptManager
  • One Button
  • One MicrosoftReportViewer

So your web page design will look like following:

2-Export SSRS Report into PDF Format from ASP.NET Application

3. 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();

4. 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.

4-Export SSRS Report into PDF Format from ASP.NET Application

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);

			

7 thoughts on “Export SSRS Report into PDF Format from ASP.NET Application

  1. i am able to save pdf file but, the browser is getting black.
    but as per my requirement , report has to be saved as pdf in back end and able to see the browser with the report .

Leave a Reply

Your email address will not be published. Required fields are marked *