【问题标题】:ReportViewer - Hide PDF ExportReportViewer - 隐藏 PDF 导出
【发布时间】:2010-12-02 18:17:01
【问题描述】:

我在 VB.Net 2005 应用程序中使用了 ReportView 组件。如何禁用 PDF 导出功能,只保留 MS Excel 格式?

【问题讨论】:

标签: asp.net reporting-services reportviewer


【解决方案1】:

我遇到了完全相同的问题,用下面的C#方法解决了,找到here!:

public void DisableUnwantedExportFormat(ReportViewer ReportViewerID, string strFormatName)
{
    FieldInfo info;

    foreach (RenderingExtension extension in ReportViewerID.LocalReport.ListRenderingExtensions())
     {
        if (extension.Name == strFormatName)
        {
             info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
            info.SetValue(extension, false);
        }
    }
}

在 page_load 上:

DisableUnwantedExportFormat(ReportViewer1, "PDF");

【讨论】:

  • 您好,我使用了您的,它仅适用于 PDF 和 WORD。如何用 Excel 试试??
  • 哎呀,我想通了。它区分大小写,必须是“Excel”。感谢您的代码。 ^^
  • 也适用于服务器端处理的报告;只需将 .LocalReport 更改为 .ServerReport(在 SSRS 2016 上测试)。
  • 一段不错的代码,用于服务器端报告,非常有用且易于实现。
【解决方案2】:

这是禁用导出选项的方法,只需将除 Excel 之外的所有选项标记为 false。
*不要忘记重新启动 Reporting Services 服务。

文件:InstallPath\Reporting Services\ReportServer\rsreportserver.config

已启用:

<Extension Name="EXCEL"
Type="Microsoft.ReportingServices.Rendering.ExcelRenderer.ExcelRenderer,Microsoft.ReportingServices.ExcelRendering"/>

已禁用:

<Extension Name="EXCEL"
Type="Microsoft.ReportingServices.Rendering.ExcelRenderer.ExcelRenderer,Microsoft.ReportingServices.ExcelRendering"
Visible="false"/>

【讨论】:

    【解决方案3】:

    这个简单的 jQuery 方法对我有用:

     $(document).ready(function () {
         $("a[title='PDF']").parent().hide();  // Remove from export dropdown.
         $("a[title='MHTML (web archive)']").parent().hide();  
         $("a[title='TIFF file']").parent().hide();  
     });
    

    【讨论】:

    • 工作就像一个魅力。简单又不乱!
    • 简单易行的方法,无需修改配置文件。
    【解决方案4】:

    使用上面的 jon 代码作为参考,我设法在运行时将“Excel”隐藏在程序中。 但是,我不是 VB.net,所以我在 C# 中放了一个示例。对此感到抱歉,但我希望这会有所帮助。还有一件事,报告嵌入在 ASP.net 页面中。

      // This is the Load event of the reports itself.
      // Call the recursive method.
      protected void ReportViewerResults_Load(object sender, EventArgs e)
      {
        CustomizeRV((System.Web.UI.Control)sender);
      }
    
      // Patterned from Jon.
      // Traverse all controls/child controls to get the dropdownlist.
      // The first dropdown list is the ZoomGroup, followed by the ExportGroup.
      // We just wanted the ExportGroup.
      // When a dropdownlist is found, create a event handler to be used upon rendering.
      private void CustomizeRV(System.Web.UI.Control reportControl)
      {
        foreach (System.Web.UI.Control childControl in reportControl.Controls)
        {
          if (childControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
          {
            System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)childControl;
            ddList.PreRender += new EventHandler(ddList_PreRender);
          }
          if (childControl.Controls.Count > 0)
          {
            CustomizeRV(childControl);
          }
        }
      }
    
      // This is the event handler added from CustomizeRV
      // We just check the object type to get what we needed.
      // Once the dropdownlist is found, we check if it is for the ExportGroup.
      // Meaning, the "Excel" text should exists.
      // Then, just traverse the list and disable the "Excel".
      // When the report is shown, "Excel" will no longer be on the list.
      // You can also do this to "PDF" or if you want to change the text.
      void ddList_PreRender(object sender, EventArgs e)
      {
        if (sender.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
        {
          System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)sender;
          System.Web.UI.WebControls.ListItemCollection listItems = ddList.Items;
    
          if ((listItems != null) && (listItems.Count > 0) && (listItems.FindByText("Excel") != null))
          {
            foreach (System.Web.UI.WebControls.ListItem list in listItems)
            {
              if (list.Text.Equals("Excel")) 
              {
                list.Enabled = false;
              }
            }
          }
        }
      }
    

    我试图将默认项目选择为“PDF”,但找不到启用“导出”文本按钮的方法。 :-(

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。我可以在报表呈现时隐藏不需要的导出选项,但在钻取报表的情况下它不起作用。以下代码适用于父报表和钻取报表,使用 LocalReport:

          private void SuppressExportButton(ReportViewer rv, string optionToSuppress)
          {
              var reList = rv.LocalReport.ListRenderingExtensions();
              foreach (var re in reList)
              {
                  if (re.Name.Trim().ToUpper() == optionToSuppress.Trim().ToUpper()) // Hide the option
                  {
                      re.GetType().GetField("m_isVisible", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(re, false);
                  }
              }
          }
      

      诀窍是从页面 PreRender 方法调用该方法:

          protected void Page_PreRender(object sender, System.EventArgs e)
          {
              SuppressExportButton(rvMain, "PDF");
              SuppressExportButton(rvMain, "Word");
          }
      

      【讨论】:

        【解决方案6】:

        我设法通过一些修补来禁用 PDF 导出按钮。 ReportViewer 类没有任何面向公众的功能来禁用“导出到 PDF”工具栏按钮。为了做到这一点,看看下面的代码:

        在您的 reportViewer 页面的 OnLoad 事件期间调用此函数:

         Private Sub CustomizeRV(ByVal ctrl As Control)
            For Each c As Control In ctrl.Controls
              If TypeOf c Is ToolStrip Then
                Dim ts As ToolStrip = DirectCast(c, ToolStrip)
                For i As Integer = 0 To ts.Items.Count - 1
                  If ts.Items(i).Name = "export" Then
                    Dim exp As ToolStripDropDownButton = ts.Items(i)
                    AddHandler exp.DropDownOpening, AddressOf disableButton
                  End If
                Next
              End If
              If c.HasChildren Then
                CustomizeRV(c)
              End If
            Next
          End Sub
        

        我无法在此处设置工具条按钮的 Visible 属性,因为导出选项已加载 OnDropDownOpened。相反,我添加了一个处理程序来负责在单击工具条下拉菜单时禁用导出选项。处理函数如下:

          Private Sub disableButton(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim btn As ToolStripDropDownButton = DirectCast(sender, ToolStripDropDownButton)
            btn.DropDownItems(1).Visible = False
          End Sub
        

        所以基本上,Onload 您正在添加一个事件处理程序,这样当单击导出下拉按钮时,上述函数将运行 - 使导出到 PDF 不可见。

        该解决方案肯定会起作用,我刚刚完成了它的工作。

        如果您有任何问题,请告诉我。

        【讨论】:

          【解决方案7】:
          public void DisableUnwantedExportFormats()
          {
              FieldInfo info;
          
              foreach (RenderingExtension extension in reportViewer.ServerReport.ListRenderingExtensions())
              {
                  if (extension.Name != "PDF" && extension.Name != "EXCEL") // only PDF and Excel - remove the other options
                  {
                      info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
                      info.SetValue(extension, false);
                  }
          
                  if (extension.Name == "EXCEL") // change "Excel" name on the list to "Excel 97-2003 Workbook"
                  {
                      info = extension.GetType().GetField("m_localizedName", BindingFlags.Instance | BindingFlags.NonPublic);
                      if (info != null) info.SetValue(extension, "Excel 97-2003 Workbook");
                  }
              }
          }
          

          我已经尝试通过添加上述方法DisableUnwantedExportFormats() 来隐藏“导出到 Excel”选项。第一次加载报表时,Excel 选项不可见。

          但是,当我过去在 Drillthrough() 事件中调用相同的方法时,“Excel”和 PDF 选项在导出控件下拉列表中变得可见。我试过在我的Drillthrough() 事件的第一条语句中调用你的方法(就像我在页面加载方法中使用的那样)。

          请告诉我,如何在 Reportviewer 的Drillthrough() 事件中隐藏 excel 选项。

          【讨论】:

            【解决方案8】:

            我已经设法在页面底部使用 JavaScript 在客户端执行此操作。

            var exportSelectBox = document.getElementById("ReportViewer1__ctl1__ctl5__ctl0");
            exportSelectBox.remove(7);
            exportSelectBox.remove(6);
            exportSelectBox.remove(5);
            exportSelectBox.remove(4);
            exportSelectBox.remove(1);
            exportSelectBox.remove(1);
            

            【讨论】:

              【解决方案9】:

              reportviewer 2010 的 Jquery 解决方案: 把它放在包含reportviewer控件的aspx文件中(假设你的reportviewer叫做ReportViewer1)

              <script type="text/javascript">
                  $(document).ready(function () {
                      hideExportOptions();
                  });
              
                  function hideExportOptions() {
                      //Find the menu id by getting the parent of the parent of one of the export links
                      var menuID = $("a[onclick=\"$find('ReportViewer1').exportReport('PDF');\"]").parent().parent().attr("id");
                      if ($("#" + menuID).length > 0) {
                          $("#" + menuID  + " div:nth-child(3)").css('display', 'none');
                      }
                      else {
                          setTimeout("hideExportOptions()", 1000);
                      }
                  }
              
              </script> 
              

              它会等到下拉菜单呈现,然后隐藏选择的选项。通常 setTimeout 只发生一次。您可以通过添加更多的第 n 个子项来隐藏更多/其他,该数字是您要隐藏的选项下拉列表中从 1 开始的位置。

              【讨论】:

                【解决方案10】:
                1. 对“WORDOPENXML”的 Word 选项引用
                2. 对“EXCELOPENXML”的 Excel 选项参考
                3. 对“PDF”的 PDF 选项参考

                问候。

                【讨论】:

                • 这个答案应该有一些周围的描述。不是很清楚。
                【解决方案11】:

                仅在刷新后执行此操作, 像这样:

                ReportViewer1.LocalReport.Refresh();

                                string exportOption = "PDF";
                                RenderingExtension extension = ReportViewer1.LocalReport.ListRenderingExtensions().ToList().Find(x => x.Name.Equals(exportOption, StringComparison.CurrentCultureIgnoreCase));
                                if (extension != null)
                                {
                                    System.Reflection.FieldInfo fieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                                    fieldInfo.SetValue(extension, false);
                                }
                

                在此链接上查看...

                https://www.aspsnippets.com/Articles/ASPNet-RDLC-Local-SSRS-Report-Viewer-Hide-Disable-specific-export-option-Word-Excel-PDF-from-Export-button.aspx

                【讨论】:

                  【解决方案12】:

                  在后面的代码中,显示报表时加载一个隐藏值

                  this.ReportServViewer.ServerReport.Refresh();
                  this.hidReportViewing.Value = "algo";
                  

                  然后使用以下 javascript 设置一个计时器来检查要呈现的导出按钮。当它们被渲染时,移除按钮并清除计时器。

                  <script>
                   var intervalHandler;
                   var maxTries = 10;
                   var currentTries = 0;
                  
                   function removePDFFromReporting() {
                              var clear = false;
                              if (intervalHandler != null) {                 
                                  if ($('#hidReportViewing').val() != '') {                    
                                      var anchor = $("#<%= ReportServViewer.ClientID%>_fixedTable  a:contains('PDF')");
                                      if (anchor.length == 0) {
                                          currentTries = currentTries + 1;
                                          clear = currentTries >= maxTries;
                                      }
                                      else {
                                          anchor.remove();
                                          clear = true;                       
                                      }
                                  }
                              }
                  
                              if (clear) {
                                  $('#hidReportViewing').val('');
                                  clearInterval(intervalHandler);
                                  intervalHandler = null;
                              }
                          }
                  
                  </script>
                  

                  在加载时添加(即$(document).ready())

                  if ($('#hidReportViewing').val() != '')
                   {
                                 intervalHandler = setInterval(removePDFFromReporting, 1500);
                   }
                  

                  【讨论】:

                    【解决方案13】:

                    如果有帮助...隐藏 excel 项 em VB.Net (.Net 3.5) 的代码

                    Private Sub CustomizeRV(ByVal ctrl As ReportViewer)
                    
                        For Each c As Control In ctrl.Controls
                    
                            If c.GetType.ToString = "Microsoft.Reporting.WebForms.ToolbarControl" Then
                    
                                For Each ct In c.Controls
                    
                                    If ct.GetType.ToString = "Microsoft.Reporting.WebForms.ExportGroup" Then
                    
                                        Dim cbo As DropDownList = CType(ct.controls(0), DropDownList)
                    
                                        AddHandler cbo.PreRender, AddressOf cboExportReportViewer_PreRender
                    
                                    End If
                    
                                Next
                    
                            End If
                    
                        Next
                    
                    End Sub
                    
                    Protected Sub cboExportReportViewer_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
                    
                        Dim cbo = CType(sender, DropDownList)
                    
                        For i As Integer = 0 To cbo.Items.Count - 1
                    
                            If cbo.Items(i).Text.ToLower = "excel" Then
                                cbo.Items.Remove(cbo.Items(i))
                                Exit Sub
                            End If
                    
                        Next
                    
                    End Sub
                    

                    ... 并在page_load 事件中调用CustomizeRV(ReportViewer1)

                    【讨论】:

                      【解决方案14】:

                      经过 4 小时的搜索,我找到了解决方案。 我对 marol 的代码做了一些小改动:

                              Control ReportViewerControl = ReportViewer1.FindControl("Ctl01");
                              Control ExportGroupControl = ReportViewerControl.FindControl("Ctl05");
                              DropDownList DropDownControl = (DropDownList)ExportGroupControl.FindControl("Ctl00");
                              DropDownControl.PreRender += new EventHandler(ddList_PreRender);
                      

                      【讨论】:

                        【解决方案15】:

                        对于 ReportViewer >2010,我使用 jQuery 制作的这种方法

                        function HideExtension(ext) {
                                var $reportViewer = $("[id*=ReportViewer1]");
                                var $botons = $reportViewer.find("a");
                                $botons.each(function (index,element) {
                                    if($(element).html()==ext)
                                    {
                                        $(element).parent().css("display", "none");
                                    }
                                });
                            }
                        

                        只需更改您自己的选择器并从$(document).ready(function(){//here})调用函数

                        【讨论】:

                          【解决方案16】:

                          https://stackoverflow.com/a/9192978/1099519 答案的启发,我创建了两个扩展方法。

                          在我的情况下,我使用白名单方法,只启用我想要的格式(所以你需要包括除了 PDF 之外的那些你想要的格式):

                          reportViewer.ServerReport.SetExportFormats("EXCELOPENXML", "EXCEL", "XML", "CSV");
                          

                          扩展方法如下所示(同时支持服务器和本地报告):

                          /// <summary>
                          /// Extension for ReportViewer Control
                          /// </summary>
                          public static class ReportViewerExtensions
                          {
                              private const string VisibleFieldName = "m_isVisible";
                              /// <summary>
                              /// Sets the supported formats on the <see cref="ServerReport"/>
                              /// </summary>
                              /// <param name="serverReport"><see cref="ServerReport"/> instance to set formats on</param>
                              /// <param name="formatNames">Supported formats</param>
                              public static void SetExportFormats(this ServerReport serverReport, params string[] formatNames)
                              {
                                  SetExportFormats(serverReport.ListRenderingExtensions(), formatNames);
                              }
                              /// <summary>
                              /// Sets the supported formats on the <see cref="LocalReport"/>
                              /// </summary>
                              /// <param name="localReport"><see cref="LocalReport"/> instance to set formats on </param>
                              /// <param name="formatNames">Supported formats</param>
                              public static void SetExportFormats(this LocalReport localReport, params string[] formatNames)
                              {
                                  SetExportFormats(localReport.ListRenderingExtensions(), formatNames);
                              }
                          
                              /// <summary>
                              /// Setting the visibility on the <see cref="RenderingExtension"/>
                              /// </summary>
                              /// <param name="renderingExtensions">List of <see cref="RenderingExtension"/></param>
                              /// <param name="formatNames">A list of Formats that should be visible (Case Sensitive)</param>
                              private static void SetExportFormats(RenderingExtension[] renderingExtensions, string[] formatNames)
                              {
                                  FieldInfo fieldInfo;
                                  foreach (RenderingExtension extension in renderingExtensions)
                                  {
                                      if (!formatNames.Contains(extension.Name))
                                      {
                                          fieldInfo = extension.GetType().GetField(VisibleFieldName, BindingFlags.Instance | BindingFlags.NonPublic);
                                          fieldInfo.SetValue(extension, false);
                                      }
                          
                                  }
                              }
                          }
                          

                          【讨论】:

                            【解决方案17】:

                            如果您对使用 jQuery 的快速 javascript 解决方案感兴趣 ..

                            只需将下面的 reportViewer 选择器替换为您的下拉 ID。

                            jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').children().remove(); jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').append("- 选择导出格式-"); jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').append("EXCEL");

                            这会删除所有选项,然后在 EXCEL 中重新添加为唯一选项。

                            【讨论】:

                              【解决方案18】:
                                  //Leave only PDF option, hide everything.
                              $(document).ready(function () {
                                          $("a[title='XML file with report data']").parent().hide();
                                          $("a[title='CSV (comma delimited)']").parent().hide();
                                          $("a[title='IMAGE']").parent().hide();
                                          $("a[title='MHTML']").parent().hide();
                                          $("a[title='Excel']").parent().hide();
                                          $("a[title='Word']").parent().hide();
                                          $("a[title='PowerPoint']").parent().hide();
                                          $("a[title='Data Feed']").parent().hide();
                                          $("a[title='MHTML (web archive)']").parent().hide();
                                          $("a[title='TIFF file']").parent().hide();
                                      });
                              

                              【讨论】:

                                猜你喜欢
                                • 2015-06-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 2018-07-08
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多