When using the SSRS ReportViewer control in ASP.NET, by default you have the option to export to Excel or PDF. Recently I had an application where the users wanted to limit the export options to PDF only. Thanks to this blog post, I found it was very easy to accomplish. First, call the procedure from your report's PreRender event:
Protected Sub ReportViewer1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ReportViewer1.PreRender
' Disable EXCEL format Call DisableFormat(Me.ReportViewer1, "Excel")
End Sub
The code for the disabling an export format is as follows:
Protected Sub DisableFormat(ByRef viewer As ReportViewer, ByVal formatName As String)
Const Flags As System.Reflection.BindingFlags = System.Reflection.BindingFlags.NonPublic + System.Reflection.BindingFlags.Public + System.Reflection.BindingFlags.Instance Dim m_previewService As System.Reflection.FieldInfo = viewer.LocalReport.GetType().GetField("m_previewService", Flags)
Dim ListRenderingExtensions As System.Reflection.MethodInfo = m_previewService.FieldType.GetMethod("ListRenderingExtensions", Flags) Dim previewServiceInstance As Object = m_previewService.GetValue(viewer.LocalReport)
Dim extensions As IList = ListRenderingExtensions.Invoke(previewServiceInstance, Nothing) Dim name As System.Reflection.PropertyInfo = extensions(0).GetType().GetProperty("Name", Flags)
Dim extension As Object For Each extension In extensions
If (String.Compare(name.GetValue(extension, Nothing).ToString(), formatName, True) = 0) Then Dim m_isVisible As System.Reflection.FieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.NonPublic + System.Reflection.BindingFlags.Instance)
Dim m_isExposedExternally As System.Reflection.FieldInfo = extension.GetType().GetField("m_isExposedExternally", System.Reflection.BindingFlags.NonPublic + System.Reflection.BindingFlags.Instance) m_isVisible.SetValue(extension, False) m_isExposedExternally.SetValue(extension, False)
Exit For
End If Next extension
End Sub
The above code shows how to disable a format. To enable a specific format, simply change False in the the below two lines to True.
m_isVisible.SetValue(extension,
False)
m_isExposedExternally.SetValue(extension,
False)
You could easily take the above procedure and add a boolean value so that the procedure could be used to enable or disable a given export format.