Porting a SQL Server report directly to PDF or Excel

Source: Internet
Author: User
Porting 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 wocould 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 =NewMicrosoft. Reporting. webforms. reportviewer ();
// Web address of your Report Server (ex: http: // rserver/reportserver)
 
 
 
Rview. serverreport. reportserverurl =NewUri (webconfigurationmanager. receivettings ["reportserver"]);
 
 
System. Collections. Generic. List <Microsoft. Reporting. webforms. reportparameter> paramlist =NewSystem. Collections. Generic. List <Microsoft. Reporting. webforms. reportparameter> ();
 
 
Paramlist. Add (NewMicrosoft. Reporting. webforms. reportparameter ("param1", "value1 ″));
 
Paramlist. Add (NewMicrosoft. Reporting. webforms. reportparameter ("param2", "value2 ″));
 
 
Rview. serverreport. reportpath = "/reportfolder/reportname ";
 
Rview. serverreport. setparameters (paramlist );
 
 
StringMimetype, encoding, extension, deviceinfo;
 
String[] Streamids;
 
Microsoft. Reporting. webforms. Warning [] warnings;
StringFormat = "pdf ";// Desired format goes here (PDF, Excel, or image)
 
 
 
Deviceinfo =
"<Deviceinfo>" +
 
"<Simplepageheaders> true </simplepageheaders>" +
 
"</Deviceinfo> ";
 
 
Byte[] Bytes = rview. serverreport. Render (format, deviceinfo,OutMimetype,OutEncoding,OutExtension,OutStreamids,OutWarnings );
 
 
Response. Clear ();
 
 
If(Format = "pdf ")
 
{
 
Response. contenttype = "application/pdf ";
Response. addheader ("content-disposition", using filename=output ");
 
}
 
Else If(Format = "Excel ")
{
 
Response. contenttype = "application/Excel ";
 
Response. addheader ("content-disposition", using 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. d Ll, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.