Apr 21, 2012

Generating and exporting SSRS reports programatically using Report Viewer Control

 Clients usually want to generate pdf / Excel file direct from web / desktop application.
Introduction to Report Viewer
“Reportviewer” is good tool to generate report file on fly in pdf / excel format. I have tried here with asp.net web application.
Requirement : Here, requirement is like this. Web application should have invoice list. There would be button against each button and that button’s click event would generate invoice in PDF form.
Solution : I have created one SSRS project, created report named Invoice.rdl and deploy it on my local report server and I would call same page from my application and generate report file. I have narrated this solution below.
Make changes in Web.config related to web.config :
First, I need to use ReportViewer control in web application to generate report. Report viewer control includes an HTTP handler , which needs to be used to display images or to export report . So first we need to add below code in web.config in httphandler.
5.type="Microsoft.Reporting.WebForms.HttpHandler,Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
Add Report Viewer in application : Now, in this web application, I have page having invoice list with “Print Invoice” button against each invoice. I need to create one aspx file say “reports.aspx” , which generate PDF file for me based on parameters (Instead of, to write whole code on button’s click event, I have opted new page to make it generic)
Now, I had opened this page “reports.aspx” and then look in the Toolbox window, under the Data section, for the ReportViewer control. If you do not see the control, you can download , reference it and can use it. Either I can drag control from toolbox and place in my aspx page or need to add the following HTML to the “reports.aspx” (need to register control and add it in page).
01.<%@ Register
02.Assembly="Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Now, what I need to do is that, in page of invoice list, I need to call this “reports.aspx” (on click of print invoice button) and I will pass required values (e.g. InvoiceID, reportname, required file type, like PDF here) in query string. You can also go for different options like hiddenfield or session etc.
I need to work on code behind of reports.aspx. First of all I need to import reporting.webforms library.
1.import Microsoft.Reporting.WebForms 
On page load event of this reports.aspx, I need to write script to run report and generate PDF from report server on fly. I have assigned reportname and reportServerURL below
1.Dim ReportServerUrl As String
2.Dim ReportName As String
3.Dim ReportProject As String
5.ReportName = Request.Params("ReportName")
6.ReportServerUrl = "http://localhost/ReportServer"
7.ReportProject = "Report Project1"
See here, for reportname, I have passed value in query string from my invoie list page. ReportProject is folder where, my report exist (its SSRS project name, so when you deploy it on reportserver, it makes folder with same name)
I have used variable ReportProject and ReportName to set property of ReportPath like below.
1.ReportViewer.ServerReport.ReportPath = "/" +
2.ReportProject + "/" +
Now, get all report parameters of report in variable Paramlist.
1.Dim paramList As Generic.List(Of ReportParameter)
2.paramList = New Generic.List(Of ReportParameter)
Now, I need to set all report parameters values from querystring values. I will get all parameters from report using “GetParameters()” and set value from query string to parameters.
1.Dim pinfo As ReportParameterInfoCollection
2.ReportParameterInfoCollection = paramList.GetParameters()
3.For Each As ReportParameterInfo In pinfo
5.New ReportParameter(p.Name, Request.Params(p.Name))
Then, you need to set the parameters in paramList to the ReportViewer control with below code.
Just make sure here that If you have sharehosting or windows authentication is disable, you must need to use “ReportServerCredentials()”to pass your cretdential info , other wise , you might get error like “The request failed with HTTP status 401: Unauthorized“. but I have excluded same in this post here just to make it simple currently.
Above code has set parameters for invoice report, so "http://<yourDomainName>/reports.aspx?orderID=<@OrderID>&ReportName=<@ReportName>" would display report viewer and user can export this invoice in required format e.g. pdf / csv / excel etc. (Figre 1)
SSRS Report Viewer example
Figure 1 - Report generated in Report Viewer from web application
Generate a PDF output file programmatically
We can also offer another option like, not to show report viewer and direct open invoice file in specific format like pdf / csv / excel, I needed to use “render” method of report viewer.
01.Dim returnValue as byte()
02.Dim mimeType As String ""
03.Dim returnValue As Byte()
04.Dim encoding As String ""
05.Dim streams As String()
06.Dim warnings As Microsoft.Reporting.WebForms.Warning()
09.If Request.Params("exportformat") <> "" Then
10.returnValue = ReportViewer.ServerReport.Render(
18.Response.Buffer = True
21.Response.ContentType = mimeType
25."attachment;filename=" +
26.Request.Params("ReportName") +
27."." + Request.Params("exportformat")
33.End If
So, now as I will click on print button in my application page, it should redirect page to: “http://<yourDomainName>/reports.aspx?orderID=<@OrderID>&ReportName=<@ReportName>&exportformat=<@FileExtension>” and this page would show modalDialogBox of “File Download” ask user for option either to save it or open it (Fig. 2). I have passed here in exportformat=pdf, but I can pass "csv" or "excel" file format as per requirement here. You can now generate report from anywhere in your web application by calling this page with required parameters.
SSRS save as dialog for PDF output 
Figure 2 - Generate PDF using report viewer from application.

No comments:

Post a Comment


Thanks for your visit to this blog.
We would be happy with your Queries/Suggestions.