Exporting a SQL Server Reporting Services 2005 Report Directly to PDF or Excel
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 a 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 the 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();
//Web Address of your report server (ex: http://rserver/reportserver)
A0
rview.ServerReport.ReportServerUrl = new Uri(WebConfigurationManager.AppSettings[201DReportServer201D]);
A0
System.Collections.Generic.List<Microsoft.Reporting.WebForms.ReportParameter> paramList = new System.Collections.Generic.List<Microsoft.Reporting.WebForms.ReportParameter>();
A0
paramList.Add(new Microsoft.Reporting.WebForms.ReportParameter(201DParam12033, 201CValue12033));
paramList.Add(new Microsoft.Reporting.WebForms.ReportParameter(201DParam22033, 201CValue22033));
A0
rview.ServerReport.ReportPath = 201C/ReportFolder/ReportName201D;
rview.ServerReport.SetParameters(paramList);
A0
string mimeType, encoding, extension, deviceInfo;
string[] streamids;
Microsoft.Reporting.WebForms.Warning[] warnings;
string format = 201CPDF201D; //Desired format goes here (PDF, Excel, or Image)
A0
deviceInfo =
201C<DeviceInfo>201D +
201C<SimplePageHeaders>True</SimplePageHeaders>201D +
201C</DeviceInfo>201D;
A0
byte[] bytes = rview.ServerReport.Render(format, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
A0
Response.Clear();
A0
if (format == 201CPDF201D)
{
Response.ContentType = 201Capplication/pdf201D;
Response.AddHeader(201DContent-disposition201D, 201Cfilename=output.pdf201D);
}
else if (format == 201CExcel201D)
{
Response.ContentType = 201Capplication/excel201D;
Response.AddHeader(201DContent-disposition201D, 201Cfilename=output.xls201D);
}
A0
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.