代码之家  ›  专栏  ›  技术社区  ›  Adam Robinson

如何在不显示表单的情况下打印ReportViewer的报告

  •  14
  • Adam Robinson  · 技术社区  · 16 年前

    虽然我意识到我可以在屏幕外显示表单并将其隐藏,以及许多其他形式的WinForms黑客魔法,但我宁愿坚持禅宗之路并正确完成这项工作。我有一个SSRS本地报告(因此没有服务器),我想让用户选择查看或打印(换句话说,我不想强迫他们查看或打印)。不幸的是,当我试图将ReportViewer控件作为我在代码中显式创建的组件(当然是在using()块中)打印时,或者当我试图实例化我的查看器窗体并在不显示它的情况下打印时,ReportViewer控件会抱怨它的“状态”。

    4 回复  |  直到 16 年前
        1
  •  22
  •   Brian Hartman    16 年前

    我在我的博客上发布了一个这样做的示例: http://blogs.msdn.com/brianhartman/archive/2009/02/27/manually-printing-a-report.aspx

    LocalReport对象可以独立于ReportViewer控件进行实例化,并直接在附加到该博客文章的示例代码中使用。或者,即使您没有首先在UI中显示报告,也可以传入ReportViewer.LocalReport。

        2
  •  1
  •   Mozy    16 年前

    http://scruffylookingcatherder.com/archive/2007/12/07/printing-reporting-services-2005-reports.aspx

    一点说明:它使用SSRSWeb服务将报告呈现为EMF图像,然后将图像发送到打印机。

        4
  •  0
  •   Robert Mars_win    10 年前
    Private Sub btnReceipt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceipt.Click
    
    
        My.Forms.FormA5.ReportViewer.LocalReport.DataSources.Clear()
        Dim cmd = New SqlClient.SqlCommand("Select * from V_Sale where InvoiceNo=" & Me.txtInvoice.Text, cn)
        Dim dr = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(dr)
        dr.Close()
        Dim rpt As New ReportViewer
        rpt.LocalReport.DataSources.Clear()
        rpt.LocalReport.DataSources.Add(New ReportDataSource("posds_receipt", dt))
        rpt.LocalReport.ReportEmbeddedResource = "POSsystem.receipt.rdlc"
        rpt.SetDisplayMode(DisplayMode.PrintLayout)
        rpt.ZoomMode = ZoomMode.FullPage
    
        Dim printDialog1 As PrintDialog = New PrintDialog
        printDialog1.Document = PrintDocument1
        Dim result As DialogResult = printDialog1.ShowDialog
        If (result = DialogResult.OK) Then
            PrintDocument1.Print()
        End If
    
    End Sub