ReportViewer ServerReport Render as Excel

  • I'm trying to render a serverReport to Excel using ReportViewer.ServerReport.Render. I have a report with some parameters that I'm setting dynamically but instead of rendering it as html I would like to automatically render it as Excel. Here is what I have so far:

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

    If Not IsPostBack Then

    Dim cred As New ReportServerCredentials(ConfigurationManager.AppSettings("ReportServerUserName"), ConfigurationManager.AppSettings("ReportServerPassword"), ConfigurationManager.AppSettings("ReportServerDomain"))

    Dim Param(1) As Microsoft.Reporting.WebForms.ReportParameter

    Param(0) = New Microsoft.Reporting.WebForms.ReportParameter("theyear", "2006")

    Param(1) = New Microsoft.Reporting.WebForms.ReportParameter("fieldname", "BASE")

    With ReportViewer1

    .ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote

    With .ServerReport

    .ReportServerCredentials = cred

    .ReportPath = ConfigurationManager.AppSettings("ReportServerDirectory") & "BaseVolDetails"

    .ReportServerUrl = New System.Uri(ConfigurationManager.AppSettings("ReportServerURL"))

    .DisplayName = "Base Volume Details"

    .SetParameters(Param)

    End With

    .ShowParameterPrompts = False

    .ShowPromptAreaButton = False

    Dim s As New System.Collections.Specialized.NameValueCollection

    With s

    .Add("theyear", "2006")

    .Add("fieldname", "BASE")

    End With

    Dim deviceInfo As String

    Dim mimeType As String

    Dim encoding As String

    Dim streams As String()

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

    Dim returnValue As Byte()

    returnValue = ReportViewer1.ServerReport.Render("Excel", deviceInfo, mimeType, encoding, "xls", streams, warnings)

    Response.Write(returnValue)

    End With

    End If

    End Sub

    The report renders as html fine but won't render as Excel.

    Can anyone help? I don't quite understand what the parameters for the ServerReport.Render method are. I couldn't find any examples?

    Thanks.


    Kindest Regards,

    nredman

  • I found the answer. Code is below:

    Dim mimeType As String

    Dim encoding As String

    Dim streams As String()

    Dim extension As String

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

    Dim returnValue As Byte()

    returnValue = ReportViewer1.ServerReport.Render("EXCEL", Nothing, mimeType, encoding, extension, streams, warnings)

    Response.Buffer = True

    Response.Clear()

    Response.ContentType = mimeType

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

    Response.BinaryWrite(returnValue)

    Response.Flush()

    Response.End()


    Kindest Regards,

    nredman

  • Thanks for posting.  This was stumping me for a while... -Joe

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply