Wednesday, February 4, 2009

How to render the Report to PDF/EXCEL format

I had a client who wanted to get his report directly to PDF file. Previously, he used to use a Report Viewer component in application and then exporting it to the appropriate format.
In this example I render my report to the PDF file that will be located on disk C:\.
This report also accepts one parameter.The key of the module is ReportExecutionService class which contains some methods that we need to use.

Please refer to the BOL


Imports Microsoft.SqlServer.ReportingServices2005

Private Sub CreatePdfFile()
Dim Res As New Execution.ReportExecutionService
Dim params(0) As Execution.ParameterValue
Res.Credentials = System.Net.CredentialCache.DefaultCredentials
Res.Url = "http://servername/ReportServer/ReportExecution2005.asmx?wsdl"
params(0) = New Execution.ParameterValue
params(0).Name = "ParamName"
params(0).Value = Value

Dim ReportPath As String = "/Reports/ReportName"

Res.LoadReport(ReportPath, Nothing)
Dim Format As String = "PDF"
Dim devInfo As String = Nothing
Dim extension As String = Nothing
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
Dim warnings() As Execution.Warning = Nothing
Dim stremIds() As String = Nothing
Dim result() As Byte = Nothing
Res.SetExecutionParameters(params, "en-us") --ParameterLanguage

result = Res.Render(Format, devInfo, extension, mimeType, encoding, warnings, stremIds)
IO.File.WriteAllBytes("C:\Report.pdf", result)
End Sub