469,621 Members | 1,745 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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



Expand|Select|Wrap|Line Numbers
  1. Protected Overrides Sub GenerateReport() 
  2. Dim ReportFileName As String = "/ITG/BatteryReporting/"
  3.  
  4. Dim Params As New StringBuilder 
  5. Try
  6.  
  7. If ReportTypeRB1.Checked Then
  8.  
  9. ReportFileName = String.Format("{0}TrendBaseValueTabular", ReportFileName) 
  10. ElseIf ReportTypeRB2.Checked Then
  11.  
  12. ReportFileName = String.Format("{0}TrendBaseValueGraphDataPoints", ReportFileName) 
  13. End If
  14.  
  15. Dim YellowValueText As String = ""
  16.  
  17. Dim RedValueText As String = ""
  18.  
  19. Dim BaseValueText As String = ""
  20.  
  21. If Me.ReportTypeRB1.Checked Then
  22.  
  23. YellowValueText = Me.YellowTB.Text 
  24. RedValueText = Me.RedTB.Text
  25.  
  26. BaseValueText = Me.BaseTB.Text 
  27. End If
  28.  
  29. Params.Append(Me.GetReport_P_Param()) 
  30. Params.Append(Me.GetReport_R_Param(MyMaster.TreeViewEntityTableHierarchyForSelectedPage))
  31.  
  32. Params.Append(Me.GetReport_T_Param(Me.ColumnToTrendDD))Params.Append(Me.GetReport_V_Param(YellowValueText, MyAppConstants.ReportWarningColor, _ 
  33. RedValueText, MyAppConstants.ReportAlertColor, BaseValueText))
  34.  
  35. Params.Append(Me.GetReport_I_Params(True, (Not Me.ReportTypeRB1.Checked), "", "", ""))
  36.  
  37. Dim URL As String = String.Format("{0}?{1}&{2}&rs:Format=Excel&rs:Command=Render&rc:Parameters=false", _ 
  38. MyAppSettings.SSRSServerURL, ReportFileName, Params.ToString)
  39.  
  40.  
  41.  
  42. Dim MyRequest As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest) 
  43. MyRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
  44.  
  45. Dim HttpResponse As HttpWebResponse = CType(MyRequest.GetResponse(), HttpWebResponse)Dim SR As BinaryReader = New BinaryReader(HttpResponse.GetResponseStream(), Encoding.Unicode) 
  46. Response.ClearHeaders()
  47.  
  48. Response.ClearContent()
  49.  
  50. Response.ContentType = "application/octet-stream"
  51.  
  52. 'Response.ContentType = "application/vnd.ms-excel"
  53.  
  54. Response.AddHeader("content-disposition", "attachment; filename=" & Chr(34) & Left(Me.PageName, (Me.PageName.Length - 5)) & ".xls" & Chr(34)) 
  55. Dim Count As Integer = 1024
  56.  
  57. Dim Buffer(Count - 1) As Byte
  58.  
  59. Count = SR.Read(CType(Buffer, Byte()), 0, Count) 
  60. Do Until Count = 0
  61.  
  62. ' modify each byte in buffer here 
  63.  
  64. Response.BinaryWrite(Buffer)
  65.  
  66. Count = SR.Read(Buffer, 0, Count)
  67.  
  68. Loop
  69.  
  70. Response.Flush()
  71.  
  72. Response.Close()
  73.  
  74. Catch ex As Exception 
  75. Throw New ApplicationException("Trending Report not found.")
  76.  
  77. End Try
  78.  
  79. End Sub
Oct 16 '08 #1
1 1431
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.         Response.ContentType = "image/jpg"
  4.         Dim fs As FileStream = File.OpenRead(Server.MapPath("~/Images/DogObedienceTraining.jpg"))
  5.         Dim b(fs.Length) As Byte
  6.         For i = 0 To fs.Length - 1
  7.             b(i) = fs.ReadByte
  8.         Next
  9.         fs.Close()
  10.         Response.BinaryWrite(b)
  11.     End Sub
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
Oct 16 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Wes Groleau | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.