Opening SSRS Report in PDF format via ASP.NET

I had to create a report where the report data was displayed over several columns. Just a simple report for admins with everyone's name and phone extension. SSRS allows you to do this by specifying the number of columns in your report but this view is not available in HTML mode. So if you are showing your report within an ASP.NET application then the user only sees one column of data. If they export the report to PDF format then the report looks as you intended it but obviously that's not the best approach from a user's viewpoint. So I wanted my report to come in PDF format right away so the user could see the report as it was designed. Doing this is pretty easy.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   'Get report data

   Dim dsReport As New dataset

   Dim obj As New Insitu.Data.PhoneBook

   dsReport = obj.GetPeople("")

   obj = Nothing

   'Create report datasource

   Dim rdsReport As New Microsoft.Reporting.WebForms.ReportDataSource

   rdsReport.Name = "ReportData"

   rdsReport.Value = dsReport.Tables(0)

   'Initialize report viewer

   Me.ReportViewer1.LocalReport.ReportPath = "reports\ExtLN.rdlc"

   Me.ReportViewer1.LocalReport.DataSources.Clear()

   Me.ReportViewer1.LocalReport.DataSources.Add(rdsReport)

   Me.ReportViewer1.LocalReport.Refresh()

   Dim result As Byte()

   Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing

   Dim DeviceInfo As String = "<DeviceInfo>" _

      & " <OutputFormat>PDF</OutputFormat>" _

      & " <PageWidth>11in</PageWidth>" _

      & " <PageHeight>8.5in</PageHeight>" _

      & " <MarginTop>0.1in</MarginTop>" _

      & " <MarginLeft>0.1in</MarginLeft>" _

      & " <MarginRight>0.1in</MarginRight>" _

      & " <MarginBottom>0.1in</MarginBottom>" _

      & "</DeviceInfo>"

   Dim streamIDs As String() = Nothing

   Dim mimeType As String = "application/pdf"

   Dim encoding As String = Nothing

   Dim extension As String = Nothing

   'Render report in PDF format

   result = Me.ReportViewer1.LocalReport.Render("PDF", DeviceInfo, mimeType, encoding, extension, streamIDs, warnings)

   Response.ClearContent()

   Response.ContentType = mimeType

   Response.AddHeader(
"content-disposition", "attachment; filename=Report." & extension)

   Response.BinaryWrite(result)

   Response.End()

End Sub

 

 

Published Friday, December 05, 2008 8:55 AM by bunbun

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)