ReportViewer - Hide PDF Export

前端 未结 18 1648
暖寄归人
暖寄归人 2020-12-13 10:40

I make use of a ReportView component in a VB.Net 2005 app. How can I disable the PDF export functionality, only keeping the MS Excel format?

18条回答
  •  暖寄归人
    2020-12-13 11:14

    Inspired by the answer https://stackoverflow.com/a/9192978/1099519 I created two extension methods.

    In my case I use a whitelist approach by only enabling the formats I want (So you would need to include those you want except PDF):

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

    The Extension Methods look like this (supporting both Server- and LocalReports):

    /// 
    /// Extension for ReportViewer Control
    /// 
    public static class ReportViewerExtensions
    {
        private const string VisibleFieldName = "m_isVisible";
        /// 
        /// Sets the supported formats on the 
        /// 
        ///  instance to set formats on
        /// Supported formats
        public static void SetExportFormats(this ServerReport serverReport, params string[] formatNames)
        {
            SetExportFormats(serverReport.ListRenderingExtensions(), formatNames);
        }
        /// 
        /// Sets the supported formats on the 
        /// 
        ///  instance to set formats on 
        /// Supported formats
        public static void SetExportFormats(this LocalReport localReport, params string[] formatNames)
        {
            SetExportFormats(localReport.ListRenderingExtensions(), formatNames);
        }
    
        /// 
        /// Setting the visibility on the 
        /// 
        /// List of 
        /// A list of Formats that should be visible (Case Sensitive)
        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);
                }
    
            }
        }
    }
    

提交回复
热议问题