我通过ReportViewer生成报告时禁用Excel导出选项
作者:互联网
在Winforms应用程序中通过ReportViewer生成报告时,如何禁用Excel导出选项?
加成:
特别是,我想隐藏引用Excel输出/导出任务的工具栏按钮,而不是处理pdf导出选项的工具栏按钮.
解决方法:
从表面上看,这似乎很容易,但是很难掌握出口选择权.您只需执行以下操作即可获取reportviewer的工具栏:
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
标签:reportviewer,reporting,vb-net,c,winforms 来源: https://codeday.me/bug/20191024/1918074.html