In this post, we are explaining how SSRS report can be directly exported to PDF or Excel.
Show PDF report accepts 3 parameters, report is
the name of the report on the report server, reportParameters is the parameter required for the report to run, fileName is the name in which
you want the report to be stored
public static
void ShowPdfReport
(string report, Dictionary<string, object> reportParameters, string
fileName)
{
try
{
Screen
screen;
//Get the
server and the server path from Web config
string reportUrl = ConfigurationManager.AppSettings["server"];
reportUrl += "?"
+ ConfigurationManager.AppSettings["serverPath"];
string
reportParametersQT = String.Empty;
foreach (var entry in
reportParameters)
{
reportParametersQT += "&" + entry.Key + "=" + entry.Value;
}
/*Creating a web request using the reportUrl,the report,the
format in which the report is to be displayed and the report parameters.
Here rs is a built in parameter of Reporting Services and I
am instructing reporting services to render(rs:Command=Render)the report in PDF
format(rs:Format=PDF),to export the report in Excel all you have to do is
change the format to (rs:Format=Excel)*/
WebRequest req = WebRequest.Create(reportUrl + report + "&rs:Command=Render&rs:Format=PDF"
+ reportParametersQT);
//Passing the credentials of the report server
req.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);
//Creating a response object
WebResponse
response = req.GetResponse();
Stream
stream = response.GetResponseStream();
screen.Response.Clear();
string enCodeFileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
// The word attachment in Addheader is used to
directly show the save dialog box in browser
screen.Response.AddHeader("content-disposition",
"attachment; filename=" +
enCodeFileName);
screen.Response.BufferOutput = false; // to prevent buffering
screen.Response.ContentType =
response.ContentType;
byte[]
buffer = new byte[1024];
int
bytesRead = 0;
while
((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
screen.Response.OutputStream.Write(buffer, 0, bytesRead);
}
screen.Response.End();
}
catch
(Exception e)
{
}
}
About Author
Saju M Verghese works with Systems Plus and is working on Dot Net technology projects.
He can be contacted at: saju.v@spluspl.com
Saju M Verghese works with Systems Plus and is working on Dot Net technology projects.
He can be contacted at: saju.v@spluspl.com
Awesome :)...
ReplyDeleteGood article for a Desktop application. Even though it needs to setup SSRS server.
ReplyDeleteCould you tell me how can i implement this in a web application?
ReplyDeleteRegards