【问题标题】:Render SSRS 2008 Report in web page without Report Viewer在没有报表查看器的网页中呈现 SSRS 2008 报表
【发布时间】:2013-11-06 18:35:53
【问题描述】:

我有一个用 C# 编写的网络应用程序,我需要能够在 aspx 页面上呈现SSRS 报告而不使用报告查看器控件。

因为 div 标签内的 HTML 将是完美的。我通过ReportingService2010 引用将应用程序附加到我的SSRS 实例。

我在网上找到了一些示例,但适用于ReportingServices2005,无法将它们移植过来。

我该怎么做?

【问题讨论】:

    标签: c# sql-server-2008 reporting-services ssrs-2008


    【解决方案1】:

    这是我从大约一年前完成的一个项目中提取出来的。

    几个关键点:

    • 您需要将凭据传递给报表服务器。
    • 您需要创建一个图像路径,以便您的报告中的任何图像都呈现并显示在 html Report/GraphFiles/“这应该是相对于您的应用程序 url”中
    • 如果您的报告有任何参数,您需要添加它们。
    • 您肯定需要 tweek 代码才能运行。

    它使用 ReportExecutionService 引用,您将不得不使用它,但具体细节应该都在这里。

    我真的很想花时间清理一下,但我没有时间抱歉,我希望它有所帮助

    class RenderReport
        {
    
            public struct ReportServerCreds
                {
                    public string UserName { get; set; }
                    public string Password { get; set; }
                    public string Domain { get; set; }
    
                }
    
                public ReportServerCreds GetReportCreds()
                {
    
                    ReportServerCreds rsc = new ReportServerCreds();
                    rsc.UserName = ConfigurationManager.AppSettings["reportserveruser"].ToString();
                    rsc.Password = ConfigurationManager.AppSettings["reportserverpassword"].ToString();
                    rsc.Domain = ConfigurationManager.AppSettings["reportserverdomain"].ToString();
    
                    return rsc;
                }
    
               public enum SSRSExportType
                { 
                    HTML,PDF
                }
    
                public string RenderReport(string reportpath,SSRSExportType ExportType)
                {
                    using (ReportExecutionService.ReportExecutionServiceSoapClient res = new   ReportExecutionService.ReportExecutionServiceSoapClient("ReportExecutionServiceSoap"))
                    {
    
                        ReportExecutionService.ExecutionHeader ExecutionHeader = new ReportExecutionService.ExecutionHeader();
                        ReportExecutionService.TrustedUserHeader TrusteduserHeader = new ReportExecutionService.TrustedUserHeader();
    
                        res.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
    
                        ReportServerCreds rsc = GetReportCreds();
                        res.ClientCredentials.Windows.ClientCredential.Domain = rsc.Domain;
                        res.ClientCredentials.Windows.ClientCredential.UserName = rsc.UserName;
                        res.ClientCredentials.Windows.ClientCredential.Password = rsc.Password;
    
    
                        res.Open();
                        ReportExecutionService.ExecutionInfo ei = new ReportExecutionService.ExecutionInfo();
    
                        string format =null;
                        string deviceinfo =null;
                        string mimetype = null;
    
                        if (ExportType.ToString().ToLower() == "html")
                        {
                            format = "HTML4.0";
                            deviceinfo = @"<DeviceInfo><StreamRoot>/</StreamRoot><HTMLFragment>True</HTMLFragment></DeviceInfo>";
                        }
                        else if (ExportType.ToString().ToLower() == "pdf")
                        {
                            format = "PDF";
                            mimetype = "";
                        }
    
    
                        byte[] results = null;
                        string extension = null;
                        string Encoding = null;
                        ReportExecutionService.Warning[] warnings;
                        string[] streamids = null;
                        string historyid = null;
                        ReportExecutionService.ExecutionHeader Eheader;
                        ReportExecutionService.ServerInfoHeader serverinfoheader;
                        ReportExecutionService.ExecutionInfo executioninfo;
    
    
    
                        // Get available parameters from specified report.
                        ParameterValue[] paramvalues = null;
                        DataSourceCredentials[] dscreds = null;
                        ReportParameter[] rparams = null;
    
                        using (ReportService.ReportingService2005SoapClient lrs = new ReportService.ReportingService2005SoapClient("ReportingService2005Soap"))
                        {
    
    
                            lrs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
                            lrs.ClientCredentials.Windows.ClientCredential.Domain = rsc.Domain;
                            lrs.ClientCredentials.Windows.ClientCredential.UserName = rsc.UserName;
                            lrs.ClientCredentials.Windows.ClientCredential.Password = rsc.Password;
    
    
                            lrs.GetReportParameters(reportpath,historyid,false,paramvalues,dscreds,out rparams);
    
                        }
    
    
    
                        // Set report parameters here
                        //List<ReportExecutionService.ParameterValue> parametervalues = new List<ReportExecutionService.ParameterValue>();
    
                        //string enumber = Session["ENumber"] as string;
                        //parametervalues.Add(new ReportExecutionService.ParameterValue() { Name = "ENumber", Value = enumber });
    
                        //if (date != null)
                        //{
                        //    DateTime dt = DateTime.Today;
                            //parametervalues.Add(new ReportExecutionService.ParameterValue() { Name = "AttendanceDate", Value = dt.ToString("MM/dd/yyyy")});
                        //}
    
                        //if (ContainsParameter(rparams, "DEEWRID"))
                        //{
                            //parametervalues.Add(new ReportExecutionService.ParameterValue() { Name = "DEEWRID", Value = deewrid });
                        //}
    
                        //if (ContainsParameter(rparams, "BaseHostURL"))
                        //{
    //                        parametervalues.Add(new ReportExecutionService.ParameterValue() { Name = "BaseHostURL", Value = string.Concat("http://", Request.Url.Authority) });
                        //}
    
    
                        //parametervalues.Add(new ReportExecutionService.ParameterValue() {Name="AttendanceDate",Value=null });
                        //parametervalues.Add(new ReportExecutionService.ParameterValue() { Name = "ENumber", Value = "E1013" });
    
    
    
                        try
                        {
    
                            Eheader = res.LoadReport(TrusteduserHeader, reportpath, historyid, out serverinfoheader, out executioninfo);
                            serverinfoheader = res.SetExecutionParameters(Eheader, TrusteduserHeader, parametervalues.ToArray(), null, out executioninfo);
                            res.Render(Eheader, TrusteduserHeader, format, deviceinfo, out results, out extension, out mimetype, out Encoding, out warnings, out streamids);
    
                            string exportfilename = string.Concat(enumber, reportpath);
    
                            if (ExportType.ToString().ToLower() == "html")
                            {
                                //write html
                                string html = string.Empty;
                                html = System.Text.Encoding.Default.GetString(results);
    
                                html = GetReportImages(res, Eheader, TrusteduserHeader, format, streamids, html);
    
                                return html;
                            }
                            else if (ExportType.ToString().ToLower() == "pdf")
                            {
                                //write to pdf
    
    
                                Response.Buffer = true;
                                Response.Clear();
                                Response.ContentType = mimetype;
    
    
    
                                //Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.pdf", exportfilename));
                                Response.BinaryWrite(results);
                                Response.Flush();
                                Response.End();
    
                            }
    
    
                        }
                        catch (Exception e)
                        {
                            Response.Write(e.Message);
                        }
                    }
    
                }
    
    
                string GetReportImages(ReportExecutionService.ReportExecutionServiceSoapClient res, 
                                       ReportExecutionService.ExecutionHeader EHeader,
                                        ReportExecutionService.TrustedUserHeader tuh,
                                       string reportFormat, string[] streamIDs, string html)
                {
                    if (reportFormat.Equals("HTML4.0") && streamIDs.Length > 0)
                    {
                        string devInfo;
                        string mimeType;
                        string Encoding;
                        int startIndex;
                        int endIndex;
                        string fileExtension = ".jpg";
    
                        string SessionId;
    
                        Byte[] image;
    
                        foreach (string streamId in streamIDs)
                        {
                            SessionId = Guid.NewGuid().ToString().Replace("}", "").Replace("{", "").Replace("-", "");
                            //startIndex = html.IndexOf(streamId);
                            //endIndex = startIndex + streamId.Length;
    
                            string reportreplacementname = string.Concat(streamId, "_", SessionId, fileExtension);
                            html = html.Replace(streamId, string.Concat(@"Report\GraphFiles\", reportreplacementname));
    
    
                            //html = html.Insert(endIndex, fileExtension);
                            //html = html.Insert(startIndex, @"Report/GraphFiles/" + SessionId + "_");
    
                            devInfo = "";
                            //Image = res.RenderStream(reportFormat, streamId, devInfo, out encoding, out mimeType);
                            res.RenderStream(EHeader,tuh, reportFormat, streamId, devInfo, out image , out Encoding, out mimeType);
    
    
    
                            System.IO.FileStream stream = System.IO.File.OpenWrite(HttpContext.Current.Request.PhysicalApplicationPath + "Report\\GraphFiles\\" + reportreplacementname);
                            stream.Write(image, 0, image.Length);
                            stream.Close();
                            mimeType = "text/html";
                        }
                    }
    
                    return html;
                }
    
                bool ContainsParameter(ReportParameter[] parameters, string paramname)
                {
                        if(parameters.Where(i=>i.Name.Contains(paramname)).Count() != 0)
                        {
                            return true;
                        }
                        return false;
                    }
        }
    

    执行:

    第一个参数是报告在服务器上的位置。 第二个是 SSRSExportType 枚举

    RenderReport("ReportPathOnServer",SSRSExportType.HTML);
    

    【讨论】:

    • 这适用于 SQL 2008 R2 吗?我将它粘贴到我项目的一个新页面中,并且存在大量不兼容性。
    • 您好,我有一个类似的实现。当我从带有 iframe 中同一域的图像的临时文件加载报告时遇到了一个问题。用于调整图像大小的 SSRS 生成的 js 无法正常工作。调整大小功能似乎在加载图像之前运行。你有没有遇到过类似的问题?
    • 听起来您需要将那段 javascript 嵌入到文档就绪函数中,例如 codeproject.com/Tips/632672/…
    • 非常感谢,我的解决方案基于此。为了避免将图像写入磁盘(并设置权限等),我只是将其转换为 Base64 并将它们嵌入到 HTML img 标签的 src 属性中,效果很好(并且允许您将 html 作为文件从您的浏览器,然后通过电子邮件发送给其他人等)。它在 IE、FF 和 Chrome 中运行良好。我刚刚得到了图像字符串: var imageString = "data:image/" + imageFormat + ";base64," + Convert.ToBase64String(image);然后调用 html = html.Replace("/" + streamId, imageString);图片格式来自字节。
    【解决方案2】:

    如果您只是想显示报表的 HTML 渲染,并且希望它看起来像应用程序的原生对象,没有任何参数或工具栏,那么您可以直接调用报表的 URL 并包含 "&rc: URL 中的 Toolbar=false”。这将隐藏报表查看器控件的工具栏。此方法在URL Access Parameter Reference msdn article 下进行了描述。不完全是你要求的,但它可能达到目的。

    如果您将结果嵌入到现有的 HTML 文档中,下面是一个省略 HTML 和 Body 部分的示例调用:

    http://ServerName/ReportServer?%2fSome+Folder%2fSome+Report+Name&rs:Command=Render&rc:Toolbar=false&rc:HTMLFragment=true
    

    【讨论】:

    • 我无法使用报表查看器控件。我需要将报表嵌入到现有页面和表单中,而不使用 RV 控件。
    • 是否有 URL 访问不起作用的原因?这些年来,我以这种方式在现有应用程序中嵌入了十多次报告。
    • 报告查看器控件才是问题所在。客户的浏览器上不允许有 ActiveX 控件。我可以在没有 RV 控件的情况下在 iframe 中呈现,但这需要页面表单内的表单,这显然是不允许的。没有办法使用 URL 访问来返回 HTML 流??
    • 使用 URL 可以强制呈现,而无需嵌入报表查看器控制器。它只是在服务器上将报表呈现为 HTML 并返回 HTML 呈现的文档。
    • 这就是我正在寻找并一直在努力实现的目标。我可以看一个例子吗?
    【解决方案3】:

    绝对是一个老问题,但如果您使用的是 ASP.NET MVC,您可以尝试this open source solution。它使用 HTML 帮助程序并在 iframe 中呈现 .aspx 页面。该 repo 有一个服务器端、本地渲染和匿名示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多