簡體   English   中英

在沒有報表查看器的情況下在網頁中呈現SSRS 2008報表

[英]Render SSRS 2008 Report in web page without Report Viewer

我有一個用C#編寫的Web應用程序,我需要能夠在aspx頁面上呈現SSRS報告, 而無需使用Report Viewer控件。

由於div標簽內的HTML是完美的。 我已通過ReportingService2010參考將應用程序附加到我的SSRS實例。

我在網上找到了一些示例,但這些示例適用於ReportingServices2005 ,無法將其移植過來。

我怎樣才能做到這一點?

我從大約一年前制定的項目中撤出了這個計划。

一些要點:

  • 您需要將憑據傳遞到報表服務器。
  • 您需要創建一個圖像路徑,以便報表中的所有圖像都可以呈現並顯示在html Report / GraphFiles /中。“這應該相對於您的應用程序網址”
  • 並且如果您的報告中包含任何參數,則需要添加它們。
  • 您肯定需要花些時間來執行代碼。

它使用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);

如果您只是試圖顯示報告的HTML呈現,並且希望它看起來像應用程序的本機對象,而沒有任何參數或工具欄,則可以直接調用報告的URL並包含“&rc:Toolbar = false” ”中的“”。 這將隱藏“報表查看器”控件的工具欄。 URL訪問參數參考msdn文章下介紹了此方法。 並非完全符合您的要求,但可以達到目的。

如果將結果嵌入到現有的HTML文檔中,那么以下示例調用將忽略HTML和Body部分:

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

絕對是一個老問題,但是如果您使用的是ASP.NET MVC,則可以嘗試此開源解決方案 它使用HTML幫助器,並在iframe中呈現.aspx頁。 存儲庫有一個服務器端,本地渲染和匿名示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM