views:

164

answers:

2

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.

A: 

Please try this:

Dim instance As ReportViewer

instance.ShowExportButton = false

HTH

Raja
Thanks for the response. However, this code will disable both Excel AND 'pdf' export. I want to be able to export only pdf files. I want to hide the button that refers to Excel export, and not the one using the pdf option...
tmarouda
+1  A: 

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
Ira Rainey
I don't know if it works, I'll try it soon, but you deserve to be voted as the answer to my question. Thanks for the effort.
tmarouda
No probs. Let me know if you have trouble with it, as it works fine for me here.
Ira Rainey