Exporting a SQL Server Reporting Services 2005 (SSRS) Report Directly to PDF/Excel is a handy way of generating high quality reports without being stuck to using the ReportViewer interface. Sometimes the ReportViewer interface is an unnecessary step, but other times the ReportViewer won't render correctly even though the underlying report is correct. This is especially true when your audience might use Firefox or Safari (or anything other than IE), since the ReportViewer control almost never outputs a readable report. Of course, it would be nice to just have a button on your page that generates a PDF or Excel file in any browser, and uses an SSRS back-end to do all of the report creating and heavy lifting.
The following code will show how to export such a report, including the passing of an arbitrary number of custom parameters. Note that the identity of the application pool that your website runs under will need to have at least "browser" access to the folder containing the report you want to display. This is usually pretty simple if both IIS and SSRS server are within the same domain, but it might be tricky if this is not the case.
Microsoft.Reporting.WebForms.ReportViewer rview = new Microsoft.Reporting.WebForms.ReportViewer();
rview.ServerReport.ReportServerUrl = new Uri(WebConfigurationManager.AppSettings["ReportServer"]); System.Collections.Generic.List<Microsoft.Reporting.WebForms.ReportParameter> paramList =
new System.Collections.Generic.List<Microsoft.Reporting.WebForms.ReportParameter>();
paramList.Add(new Microsoft.Reporting.WebForms.ReportParameter("Param1″, "Value1″));
paramList.Add(new Microsoft.Reporting.WebForms.ReportParameter("Param2″, "Value2″));
rview.ServerReport.ReportPath = "/ReportFolder/ReportName";
rview.ServerReport.SetParameters(paramList);
string mimeType, encoding, extension, deviceInfo;
string[] streamids;Microsoft.Reporting.WebForms.Warning[] warnings;
string format = "PDF";
deviceInfo = "<DeviceInfo>" +"<SimplePageHeaders>True</SimplePageHeaders>" +
"</DeviceInfo>";
byte[] bytes = rview.ServerReport.Render(format, deviceInfo, out mimeType,
out encoding, out extension, out streamids, out warnings);
Response.Clear();
if (format == "PDF")
{
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "filename=output.pdf");
}
else if (format == "Excel")
{
Response.ContentType = "application/excel";
Response.AddHeader("Content-disposition", "filename=output.xls");
}
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
Response.Close();
One potential issue that you might run into upon deploying your project is that your application server may not have the ReportViewer DLLs that are needed. You have two options in this case. The first is to copy the three Microsoft.ReportViewer.*.dll's (ReportViewer.Common.dll, ReportViewer.ProcessingObjectModel.dll, and ReportViewer.WebForms.dll) from your development computer into the BIN folder of your application server (or into the GAC). The second option (though I have not verified it) is to install the SSRS redistributable on the application server (http://www.microsoft.com/downloads/details.aspx?familyid=8a166cac-758d-45c8-b637-dd7726e61367&displaylang=en).
Check out http://www.microsoft.com/sql/technologies/reporting/default.mspx for good SSRS resources, including some nice learning tools and report packs.