的ReportViewer - 隐藏PDF导出(ReportViewer - Hide PDF E

2019-07-20 07:16发布

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

Answer 1:

我有完全相同的问题,并使用下面的C#方法来解决,发现这里 !

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");


Answer 2:

这是你如何禁用导出选项,只是除所有Excel的那些标记为false。
*不要忘了重新启动Reporting Services服务。

文件:安装路径\ 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"/>


Answer 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();  
 });


Answer 4:

使用Jon的码以上作为参考,我设法隐藏“创先争优”在运行时程序。 不过,我不是一个好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”,但无法找到解决办法,使“导出”按钮文本。 :-(



Answer 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");
    }


Answer 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无形。

该解决方案将是肯定的工作,我刚拍完它的工作。

如果你有任何问题,让我知道。



Answer 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()事件“创先争优”和PDF选项在出口越来越明显控制下拉。 我致电我的第一个语句的方法试图Drillthrough()事件(像我在页面加载方法使用)。

请让我知道,我如何隐藏在Excel中的选项Drillthrough()的ReportViewer的事件。



Answer 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);


Answer 9:

2010年的ReportViewer jQuery的解决方案:将这个在aspx文件包含ReportViewer控件(假设你的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位数字隐藏更多/人。



Answer 10:

  1. 到Word选项参考“WORDOPENXML”
  2. 到Excel选项参考“EXCELOPENXML”
  3. 要PDF选项参考“PDF”

问候。



Answer 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



Answer 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>

在负载增加(即$(文件)。就绪())

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


Answer 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

...并把一个呼叫CustomizeRV(ReportViewer1)page_load事件



Answer 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);


Answer 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})



Answer 16:

如果您有兴趣使用jQuery快速JavaScript解决方案..

只需更换下面与您的下拉ID中的ReportViewer选择。

。jQuery的( '#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00')儿童()删除(); 。jQuery的( '#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00')追加( “ - 选择导出格式 - ”); jQuery的( '#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00')附加( “EXCEL”)。

这将删除所有的选项,然后在Excel添加回作为唯一的选择。



Answer 17:

通过回答启发https://stackoverflow.com/a/9192978/1099519我创建了两个扩展方法。

在我来说,我使用白名单的方式只允许我想要的(所以你需要包括那些你想除了PDF)的格式:

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

扩展方法是这样的(同时支持服务器端和LocalReports):

/// <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);
            }

        }
    }
}


文章来源: ReportViewer - Hide PDF Export