Monday, March 26, 2012

How to use the Render method to save a report directly to disk ?

Hi there,

Is there a way to programmatically save a RS results into Excel format using the render method ?

I had read about that capability but I can't seem to find any sample code on how to do it. Is this a parameter that you have to set in the render method ?

Any suggestion or tips are much appreciated !

Thanks !

Here's one way...

http://sqljunkies.com/WebLog/roman/archive/category/370.aspx

rs -i "C:\RS Script\MyScript.rss" -s http://myserver/reportserver

Here is the code from MyScript.rss. It renders the Product Line Sales report, it's one of the sample reports that comes with RS:

Public Sub Main()
Dim format as string = "EXCEL"
Dim fileName as String = "C:\RS Script\Product Line Sales.xls"
Dim reportPath as String = "/SampleReports/Product Line Sales"

' Prepare Render arguments
Dim historyID as string = Nothing
Dim deviceInfo as string = Nothing
Dim showHide as string = Nothing
Dim results() as Byte
Dim encoding as string
Dim mimeType as string
Dim warnings() AS Warning = Nothing
Dim reportHistoryParameters() As ParameterValue = Nothing
Dim streamIDs() as string = Nothing

results = rs.Render(reportPath, format, _
Nothing, Nothing, Nothing, _
Nothing, Nothing, encoding, mimeType, _
reportHistoryParameters, warnings, streamIDs)

' Open a file stream and write out the report
Dim stream As FileStream = File.OpenWrite(fileName)
stream.Write(results, 0, results.Length)
stream.Close()
End Sub

cheers,

Andrew

|||

Hi andrew,

Thanks for the quick reply !

Yup, that works ! For now I am using this to save my reports to Excel.

I am not sure whether it is actually possible for the Render method to actually save a report to Excel without having to "see the report" or perhaps changing one of the parameters.

Can anyone verify this ?

Thanks.

|||

Hi there,

Glad to hear. Not sure what you mean about having to see the report? If you take a look at running from a URL and specifying the Excel Export option in the query parameters, you should not have to see the report. You would need to either define default parameters or specify required parameters for this to work.

You should be able to automate everything.

cheers,

Andrew

|||

Hi Andrew,

Well the problem with what I am doing is that I need to use the Render method so as not to expose the reporting server URL to outside sources. In other words through ASMX. I am trying not to "render" the report to the browser in Excel format.

So the regular way of rendering a report in the browser is the following:-

Response.ClearContent()

Response.AppendHeader("content-length", result.Length.ToString())

Response.ContentType = mimeType

Response.BinaryWrite(result)

Response.Flush()

Response.Close()

This will render the report in Excel to the browser. I was wondering whether there was a way to save the file as an excel without having to render it to a browser. I am also curious whether there was just more than setting the Format parameter to "EXCEL" and whether there were other parameters that I need to set ? (just like you mentioned in the post)

Thanks

No comments:

Post a Comment