通过 ReportViewer 生成报告时禁用 Excel 导出选项

发布于 2024-08-29 14:17:56 字数 145 浏览 4 评论 0原文

当我在 winforms 应用程序中通过 ReportViewer 生成报告时,如何禁用 Excel 导出选项?

添加: 特别是,我想隐藏引用 Excel 输出/导出任务的工具栏按钮,而不是隐藏处理 pdf 导出选项的按钮。

How can I disable the Excel export option when I generate a report, via the ReportViewer, in my winforms application?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掀纱窥君容 2024-09-05 14:17:56

这将完成海报要求的内容(使用一点 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;
        }
    }

This will accomplish what the poster asked (uses a little LINQ for selecting objects of certain types from collections):

    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;
        }
    }
摘星┃星的人 2024-09-05 14:17:56

虽然从表面上看这似乎很容易,但导出选项很难掌握。您可以通过简单地执行以下操作来获取报表查看器的工具条:

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

While on the face of it this seems easy, the export options are difficult to get hold of. You can get the toolstrip of the reportviewer by simply doing this:

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

...and you can iterate through the .Items collection to do what you like with the buttons, however the DropDownItems collection for the export button always appears empty.

So the easy solution is to get rid of the default export button and add your own with just the functionality you need. So in your form constructor:

//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)

Then all you need to do is to take care of the actual exporting:

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
梦里寻她 2024-09-05 14:17:56
//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;
                }
            }
        }
    }
}
//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;
                }
            }
        }
    }
}
不忘初心 2024-09-05 14:17:56

请尝试这个:

Dim instance As ReportViewer

instance.ShowExportButton = false

HTH

Please try this:

Dim instance As ReportViewer

instance.ShowExportButton = false

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文