简体   繁体   English

报告服务:获取生成的报告的PDF

[英]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.

相关问题 将报告呈现为pdf后,将pdf添加到Reporting Services报告中 - Add a pdf to a Reporting Services report after rendering report as pdf 生成Reporting Services 2005 PDF报告并将其存储在磁盘上 - Generating a Reporting Services 2005 PDF report and storing it on disk 在代码中获取Sql Server Reporting Services报表描述 - Get Sql Server Reporting Services report description in code 如何在 ASP.Net 和 C# web 页面中将 Reporting Services 报表显示为内联 PDF? - How to display a Reporting Services report as an inline PDF, in a ASP.Net and C# web page? 报告服务-报告参数丢失状态 - Reporting Services - Report Paramaters Losing State 此版本的Reporting Services无效或不支持此报告的定义 - The definition of this report is not valid or supported by this version of Reporting Services 使用 Reporting Services 将报告直接发送到打印机 - Send a report directly to printer with Reporting Services Sql Reporting服务 - 在报告中查找项目 - 加载时 - Sql Reporting services - find item in report - on load 多次打印报告,(SSRS 报告服务) - Print a report Multiple times, (SSRS reporting services) Asp.Net,VB,SQL Server Reporting Services ..从目录动态生成的报表,单击后即可在报表查看器中查看? - Asp.Net, VB, SQL Server Reporting Services..dynamically generated reports from directory to be viewed on report viewer on click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM