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

虽然我意识到我可以在屏幕外显示表单并隐藏它,以及许多其他forms的WinForms hackish魔法,但我宁愿坚持使用zen路径并完成正确的操作。 我有一个SSRS本地报告(所以没有服务器),我想给用户选择查看或打印(换句话说,我不想强​​迫他们查看打印)。 不幸的是,当我尝试将其作为一个组件打印时,ReportViewer控件会抱怨它的“状态”我在我的代码中显式创建(当然在using()块内部)或者如果我尝试实例化我的查看器forms和只是打印而不显示它。

有没有办法做到这一点,能与我好好相处,还是我应该把它展示在屏幕外并继续我的生活?

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

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

检查一下,看看它是否有帮助… http://scruffylookingcatherder.com/archive/2007/12/07/printing-reporting-services-2005-reports.aspx

一点解释:它使用SSRS Web服务将报告呈现为EMF图像,然后将图像发送到打印机。

请参阅以下链接对您非常有用http://social.msdn.microsoft.com/Forums/en-US/9f52d79d-5baf-4e84-97d5-7dbba6623b89/printing-without-the-reportviewer

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