Disable Excel export option when I create a report via ReportViewer

How to disable Excel export option when creating a report through ReportViewer in winforms application?

Addition: In particular, I want to hide the toolbar button that refers to the Excel output / export task, and not the one that handles the pdf export option.

+3
source share
4 answers

While at first glance this seems easy, export options are difficult to obtain. You can get the Reportviewer toolbar by simply doing the following:

Dim myToolStrip As ToolStrip = DirectCast(ReportViewer1.Controls.Find("toolStrip1", True)(0), ToolStrip)

... .Items, , , DropDownItems .

, - . , :

//Hide the default export button
ReportViewer1.ShowExportButton = False

//Define a new button
Dim newExportButton As New ToolStripButton("Export PDF", Nothing, AddressOf Me.ExportPDF, "newExport")

//And add it to the toolstrip
DirectCast(ReportViewer1.Controls.Find("toolStrip1", True)(0), ToolStrip).Items.Add(newExportButton)

:

Private Sub ExportPDF()

    Dim warnings As Microsoft.Reporting.WinForms.Warning()
    Dim streamids As String()
    Dim mimeType As String = ""
    Dim encoding As String = ""
    Dim extension As String = ""

    Dim bytes As Byte() = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

    Dim fs As New IO.FileStream("C:\export.pdf", IO.FileMode.Create)
    fs.Write(bytes, 0, bytes.Length)
    fs.Close()
    fs.Dispose()

End Sub
+2

, ( LINQ ):

    private void DisableReportViewerExportExcel()
    {
        var toolStrip = this.ReportViewer.Controls.Find("toolStrip1", true)[0] as ToolStrip;

        if (toolStrip != null)
            foreach (var dropDownButton in toolStrip.Items.OfType<ToolStripDropDownButton>())
                dropDownButton.DropDownOpened += new EventHandler(dropDownButton_DropDownOpened);
    }

    void dropDownButton_DropDownOpened(object sender, EventArgs e)
    {
        if (sender is ToolStripDropDownButton)
        {
            var ddList = sender as ToolStripDropDownButton;
            foreach (var item in ddList.DropDownItems.OfType<ToolStripDropDownItem>())
                if (item.Text.Contains("Excel"))
                    item.Enabled = false;
        }
    }
+2
//Call This function from Page_Load Event
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); 
        } 
    }
}

//Dropdown prerender event
//You can hide any option from ReportViewer( Excel,PDF,Image )   
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;
                }
            }
        }
    }
}
+1

, :

Dim instance As ReportViewer

instance.ShowExportButton = false

-3

Source: https://habr.com/ru/post/1741815/


All Articles