ReportViewer - 隐藏PDF导出

时间:2009-09-30 18:30:20

标签: asp.net reporting-services reportviewer

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

18 个答案:

答案 0 :(得分:27)

我有完全相同的问题并使用以下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");

答案 1 :(得分:10)

这是禁用导出选项的方法,只需将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"/>

答案 2 :(得分:10)

这个简单的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();  
 });

答案 3 :(得分:5)

使用上面的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”,但无法找到启用“导出”文本按钮的方法。 :-(

答案 4 :(得分:3)

我遇到了同样的问题。我可以在报表呈现时隐藏不需要的导出选项,但在钻取报表的情况下它并不起作用。以下代码使用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");
    }

答案 5 :(得分:2)

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”&amp; PDF选项在“导出控件”下拉列表中可见。我试过在我的Drillthrough()事件的第一个语句中调用你的方法(就像我在Page load方法中使用的那样)。

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

答案 6 :(得分:2)

我设法通过一些修补来禁用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 :(得分:2)

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仅出现一次。您可以通过添加更多nth-childs来隐藏更多/其他人,该数字是您要隐藏的选项下拉列表中从1开始的位置。

答案 8 :(得分:2)

我已经设法在客户端使用页面底部的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 :(得分:2)

  1. 对Word选项引用“WORDOPENXML”
  2. 要Excel选项引用“EXCELOPENXML”
  3. PDF格式选项参考“PDF”
  4. 问候。

答案 10 :(得分:2)

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

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

答案 11 :(得分:1)

如果有帮助......代码隐藏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

答案 12 :(得分:1)

在代码隐藏中,在显示报告时加载隐藏值

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>

在on load add(即$(document).ready())

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

答案 13 :(得分:1)

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

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

答案 14 :(得分:1)

对于ReportViewer&gt; 2010,我使用这个用jQuery做的方法

def create(self, validated_data):
    obj = OriginalModel.objects.create(**validated_data)
    obj.save(foo=validated_data['foo'])
    return obj

只需更改自己的选择器,然后从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"); } }); }

调用该功能

答案 15 :(得分:0)

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

只需使用您的下拉ID替换下面的reportViewer选择器。

  

的jQuery( '#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00')的儿童()除去()。;           jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00')。append(“ - Select export format - ”);           jQuery的( '#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00')附加( “EXCEL”);

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

答案 16 :(得分:0)

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

在我的情况下,我仅通过启用所需的格式来使用白名单方法(因此,除了PDF之外,您还需要包含所需的格式):

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

扩展方法如下(同时支持Server-和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);
            }

        }
    }
}

答案 17 :(得分:0)

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