[英]Add a pdf to a Reporting Services report after rendering report as pdf
[英]Reporting services: Get the PDF of a generated report
我有一个报告服务服务器已经有一些运行报告,我现在需要通过自定义网站(运行asp.net MVC3)生成它们。
我需要以流/字节的形式检索此报告以将其发送给用户。 没有“报告查看器”左右。
上次我使用报告服务是使用sql 2005,我们应该作为参考一个不起眼的asmx文件。
现在是什么,sql server报告2008 R2,.Net4和visual studio 2010? 我找不到解释整个事情的教程。
(实际上我找不到sql 2008 r2没有报表查看器的教程)
MSDN提供了几种方法。 我经常在ASP .NET应用程序中使用URL访问快速简单的PDF按钮。
这是一个快速破解的代码。 可以将其清理为使用集成身份验证,并且可以通过多种方式存储报告名称。 (我从一些旧的代码中剪切并粘贴了这个代码,它会将报告存储到数据库中,然后可以从数据库返回该代码。
// 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是
public static byte[] ReadFully( Stream input )
{
using ( MemoryStream ms = new MemoryStream() )
{
input.CopyTo( ms );
return ms.ToArray();
}
}
因为您有嵌套流,所以您可能希望在复制后关闭第一个流以避免关闭流错误。 此外,如果您获得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.