报表之导出设置(七)

  ReportView控件默认的给我们展现了3种导出样式,分别是Excel、Pdf、Word。

  这三种样式基本可以满足导出要求。假设需要导出图片或其他类型的文件,将怎样做?

  其实ReportView提供了5种导出样式,只显式的展示了3种。另外两种是RPL呈现器与TIF文件,即RPL文件与IMAGE文件

  下面需要做的即通过反射找到报表对象,显式的展示另外两种导出样式即可。 

View Code
 public const string ReportViewer_RenderFormat_HTML = "RPL";
        public const string ReportViewer_RenderFormat_Excel = "Excel";
        public const string ReportViewer_RenderFormat_RGDI = "WORD";
        public const string ReportViewer_RenderFormat_IMAGE = "IMAGE";
        public const string ReportViewer_RenderFormat_PDF = "PDF";

        public static void EnableFormat(ReportViewer viewer, string formatName)
        {
            const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;

            FieldInfo m_previewService = viewer.LocalReport.GetType().GetField
           (
               "m_previewService",
              Flags
            );

            MethodInfo ListRenderingExtensions = m_previewService.FieldType.GetMethod
            (
               "ListRenderingExtensions",
                Flags
            );

            object previewServiceInstance = m_previewService.GetValue(viewer.LocalReport);

            IList extensions = ListRenderingExtensions.Invoke(previewServiceInstance, null) as IList;

            PropertyInfo name = extensions[0].GetType().GetProperty("Name", Flags);

            foreach (object extension in extensions)
            {
                if (string.Compare(name.GetValue(extension, null).ToString(), formatName, true) == 0)
                {
                    FieldInfo m_isVisible = extension.GetType().GetField("m_isVisible", BindingFlags.NonPublic | BindingFlags.Instance);
                    FieldInfo m_isExposedExternally = extension.GetType().GetField("m_isExposedExternally", BindingFlags.NonPublic | BindingFlags.Instance);
                    m_isVisible.SetValue(extension, true);
                    m_isExposedExternally.SetValue(extension, true);
                    break;
                }
            }
        }

  (代码较为简单,不再解读)

你可能感兴趣的:(导出)