虽然从表面上看这似乎很容易,但导出选项却很难掌握。您只需执行以下操作即可获取报告查看器的工具条:
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