您的位置:首页 > 数据库

Porting a sql server report directly to pdf or excel

2010-11-04 11:15 281 查看

PORTINGASQLSERVERREPORTINGSERVICES2005REPORTDIRECTLYTOPDFOREXCEL



















ExportingaSQLServerReportingServices2005(SSRS)ReportDirectlytoPDF/ExcelisahandywayofgeneratinghighqualityreportswithoutbeingstucktousingtheReportViewerinterface.SometimestheReportViewerinterfaceisanunnecessarystep,butothertimestheReportViewerwon'trendercorrectlyeventhoughtheunderlyingreportiscorrect.ThisisespeciallytruewhenyouraudiencemightuseFirefoxorSafari(oranythingotherthanIE),sincetheReportViewercontrolalmostneveroutputsareadablereport.OfcourseitwouldbenicetojusthaveabuttononyourpagethatgeneratesaPDForExcelfileinanybrowser,andusesaSSRSback-endtodoallofthereportcreatingandheavylifting.

Thefollowingcodewillshowhowtoexportsuchareport,includingthepassingofanarbitrarynumberofcustomparameters.Notethattheidentityoftheapplicationpoolthatyourwebsiterunsunderwillneedtohaveatleast"browser"accesstothefoldercontainingthereportyouwanttodisplay.ThisisusuallyprettysimpleifboththeIISandSSRSserverarewithinthesamedomain,butitmightbetrickyifthisisnotthecase.

Microsoft.Reporting.WebForms.ReportViewerrview=newMicrosoft.Reporting.WebForms.ReportViewer();
//WebAddressofyourreportserver(ex:
'target='_blank'>http://rserver/reportserver)
rview.ServerReport.ReportServerUrl=newUri(WebConfigurationManager.AppSettings[”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”;//Desiredformatgoeshere(PDF,Excel,orImage)
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”,“filename=output.pdf”);
}
elseif(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();
OnepotentialissuethatyoumightrunintoupondeployingyourprojectisthatyourapplicationservermaynothavetheReportViewerDLLsthatareneeded.Youhavetwooptionsinthiscase.ThefirstistocopythethreeMicrosoft.ReportViewer.*.dll's(ReportViewer.Common.dll,ReportViewer.ProcessingObjectModel.dll,andReportViewer.WebForms.dll)fromyourdevelopmentcomputerintotheBINfolderofyourapplicationserver(orintotheGAC).Thesecondoption(thoughIhavenotverifiedit),istoinstalltheSSRSredistributableontheapplicationserver(http://www.microsoft.com/downloads/details.aspx?familyid=8a166cac-758d-45c8-b637-dd7726e61367&displaylang=en).

Checkouthttp://www.microsoft.com/sql/technologies/reporting/default.mspxforgoodSSRSresources,includingsomenicelearningtoolsandreportpacks.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐