[英]Reporting services: Get the PDF of a generated report
I've a reporting services server which has already some running reports, and I need now to generate them through a custom website(running asp.net MVC3). 我有一个报告服务服务器已经有一些运行报告,我现在需要通过自定义网站(运行asp.net MVC3)生成它们。
I need to retrieve this report as stream/byte to send it to the user. 我需要以流/字节的形式检索此报告以将其发送给用户。 No "report viewer" or so.
没有“报告查看器”左右。
Last time I used reporting services was with sql 2005, and We should ad as reference an obscure asmx file. 上次我使用报告服务是使用sql 2005,我们应该作为参考一个不起眼的asmx文件。
What's about now, with sql server reporting 2008 R2, .Net4 and visual studio 2010? 现在是什么,sql server报告2008 R2,.Net4和visual studio 2010? I can't find a tutorial explaining the whole thing.
我找不到解释整个事情的教程。
(in fact I can't find a tutorial where there is no report viewer for sql 2008 r2) (实际上我找不到sql 2008 r2没有报表查看器的教程)
Several methods are provided at MSDN . MSDN提供了几种方法。 I have used URL access often for quick simple PDF buttons in an ASP .NET app.
我经常在ASP .NET应用程序中使用URL访问快速简单的PDF按钮。
Here's a quick hack bit of code doing this. 这是一个快速破解的代码。 It could be cleaned up to use integrated authentication, and there are many ways you could store the report name.
可以将其清理为使用集成身份验证,并且可以通过多种方式存储报告名称。 (I cut and pasted this from some old code I had that would store a report to a database and then later could return that from the db.
(我从一些旧的代码中剪切并粘贴了这个代码,它会将报告存储到数据库中,然后可以从数据库返回该代码。
// First read in the report into memory.
string strReportUser = "RSUserName";
string strReportUserPW = "MySecretPassword";
string strReportUserDomain = "DomainName";
string sTargetURL = "http://SqlServer/ReportServer?" +
"/MyReportFolder/Report1&rs:Command=Render&rs:format=PDF&ReportParam=" +
ParamValue;
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create( sTargetURL );
req.PreAuthenticate = true;
req.Credentials = new System.Net.NetworkCredential(
strReportUser,
strReportUserPW,
strReportUserDomain );
HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();
Stream fStream = HttpWResp.GetResponseStream();
//Now turn around and send this as the response..
byte[] fileBytes = ReadFully( fStream );
// Could save to a database or file here as well.
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader(
"content-disposition",
"attachment; filename=\"Report For " +
ParamValue + ".pdf\"" );
Response.BinaryWrite( fileBytes );
Response.Flush();
HttpWResp.Close();
Response.End();
ReadFully is ReadFully是
public static byte[] ReadFully( Stream input )
{
using ( MemoryStream ms = new MemoryStream() )
{
input.CopyTo( ms );
return ms.ToArray();
}
}
Because you have nested streams, you may want to close the first stream after the copy to avoid a closed stream error. 因为您有嵌套流,所以您可能希望在复制后关闭第一个流以避免关闭流错误。 Also, if you get a 401 Unauthorized, try default credentials.
此外,如果您获得401 Unauthorized,请尝试使用默认凭据。
req.UseDefaultCredentials = true;
req.PreAuthenticate = true;
req.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();
Stream fStream = HttpWResp.GetResponseStream();
//Now turn around and send this as the response..
byte[] fileBytes = ReadFully(fStream);
// Could save to a database or file here as well.
HttpWResp.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.