Saturday, October 31, 2015

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 :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 no18 of above code:
?
1
byte[] bytes = MyReportViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamids, out warnings);