Difficulty in exposing an SSRS excel report over the internet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dallas Developer
    New Member
    • Oct 2008
    • 1

    #1

    Difficulty in exposing an SSRS excel report over the internet

    Hi,

    Below is the code I have tried. I get an xls file with the right name in a dialog box to open, save or cancel. However, when I try to open it, I get the message that the Excel 97-2003 file is corrupted.

    Appreciate if you could help me with appropriate modifications to the below code.

    Thanks,

    Swamy



    Code:
    Protected Overrides Sub GenerateReport() 
    Dim ReportFileName As String = "/ITG/BatteryReporting/"
    
    Dim Params As New StringBuilder 
    Try
    
    If ReportTypeRB1.Checked Then
    
    ReportFileName = String.Format("{0}TrendBaseValueTabular", ReportFileName) 
    ElseIf ReportTypeRB2.Checked Then
    
    ReportFileName = String.Format("{0}TrendBaseValueGraphDataPoints", ReportFileName) 
    End If
    
    Dim YellowValueText As String = ""
    
    Dim RedValueText As String = ""
    
    Dim BaseValueText As String = ""
    
    If Me.ReportTypeRB1.Checked Then
    
    YellowValueText = Me.YellowTB.Text 
    RedValueText = Me.RedTB.Text
    
    BaseValueText = Me.BaseTB.Text 
    End If
    
    Params.Append(Me.GetReport_P_Param()) 
    Params.Append(Me.GetReport_R_Param(MyMaster.TreeViewEntityTableHierarchyForSelectedPage))
    
    Params.Append(Me.GetReport_T_Param(Me.ColumnToTrendDD))Params.Append(Me.GetReport_V_Param(YellowValueText, MyAppConstants.ReportWarningColor, _ 
    RedValueText, MyAppConstants.ReportAlertColor, BaseValueText))
    
    Params.Append(Me.GetReport_I_Params(True, (Not Me.ReportTypeRB1.Checked), "", "", ""))
    
    Dim URL As String = String.Format("{0}?{1}&{2}&rs:Format=Excel&rs:Command=Render&rc:Parameters=false", _ 
    MyAppSettings.SSRSServerURL, ReportFileName, Params.ToString)
    
     
    
    Dim MyRequest As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest) 
    MyRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
    
    Dim HttpResponse As HttpWebResponse = CType(MyRequest.GetResponse(), HttpWebResponse)Dim SR As BinaryReader = New BinaryReader(HttpResponse.GetResponseStream(), Encoding.Unicode) 
    Response.ClearHeaders()
    
    Response.ClearContent()
    
    Response.ContentType = "application/octet-stream"
    
    'Response.ContentType = "application/vnd.ms-excel"
    
    Response.AddHeader("content-disposition", "attachment; filename=" & Chr(34) & Left(Me.PageName, (Me.PageName.Length - 5)) & ".xls" & Chr(34)) 
    Dim Count As Integer = 1024
    
    Dim Buffer(Count - 1) As Byte
    
    Count = SR.Read(CType(Buffer, Byte()), 0, Count) 
    Do Until Count = 0
    
    ' modify each byte in buffer here 
    
    Response.BinaryWrite(Buffer)
    
    Count = SR.Read(Buffer, 0, Count)
    
    Loop
    
    Response.Flush()
    
    Response.Close()
    
    Catch ex As Exception 
    Throw New ApplicationException("Trending Report not found.")
    
    End Try
    
    End Sub
    Last edited by Curtis Rutland; Oct 16 '08, 02:50 AM. Reason: Added code tags -- Please use the # button
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    In line 55 in your posted code...you are declaring:
    Dim Count As Integer = 1024

    How do you know that count is 1024?
    What happens if this number should be bigger?

    I think this is where your problem is.

    Or maybe its when you are getting the file in the first place.

    When you get the file, you can write it's stream directly into the Response if you want to.....

    Here's a Very simple example of writing an image to the Response:
    [code=vbnet]
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load

    Response.Conten tType = "image/jpg"
    Dim fs As FileStream = File.OpenRead(S erver.MapPath(" ~/Images/DogObedienceTra ining.jpg"))
    Dim b(fs.Length) As Byte
    For i = 0 To fs.Length - 1
    b(i) = fs.ReadByte
    Next
    fs.Close()
    Response.Binary Write(b)
    End Sub[/code]

    The above code is in the Page_Load event of an ASPX page. When called on, this ASPX page does not produce HTML as it normally would...it sends a picture instead. That's why the ContentType was changed to "image/jpg". The file is read off of the server and dumped directly into the the Response stream.

    -Frinny

    Comment

    Working...