C# 如何在不显示表单的情况下打印 ReportViewer 的报告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/731049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print a ReportViewer's report without showing a form
提问by Adam Robinson
While I realize that I could just show the form off-screen and hide it, along with many other forms of WinForms hackish wizardry, I'd rather stick with the zen path and get this done right. I have a SSRS local report (so no server) that I want to give the user the option of either viewing or printing (in other words, I don't want to force them to view to print). Unfortunately, the ReportViewer control complains about its "state" when I try to print it either as a component I'm creating explicitly in my code (inside a using() block, of course) or if I try to instantiate my viewer form and just print without ever showing it.
虽然我意识到我可以在屏幕外显示表单并将其隐藏,以及许多其他形式的 WinForms hackish 魔法,但我宁愿坚持禅道并正确完成这项工作。我有一个 SSRS 本地报告(所以没有服务器),我想让用户选择查看或打印(换句话说,我不想强迫他们查看打印)。不幸的是,当我尝试将 ReportViewer 控件打印为我在代码中显式创建的组件(当然在 using() 块中)或者如果我尝试实例化我的查看器表单和只是打印而不显示它。
Is there a means to do this that will sit well with me, or should I just show it off-screen and move on with my life?
有没有一种方法可以让我很满意,或者我应该在屏幕外展示它并继续我的生活?
采纳答案by Brian Hartman
I have a sample that does this posted on my blog here: http://blogs.msdn.com/brianhartman/archive/2009/02/27/manually-printing-a-report.aspx
我在我的博客上发布了一个示例:http: //blogs.msdn.com/brianhartman/archive/2009/02/27/manually-printing-a-report.aspx
The LocalReport object can be instantiated independently of the ReportViewer control and used directly in the sample code attached to that blog post. Or you can pass in ReportViewer.LocalReport even if you don't first display the report in the UI.
LocalReport 对象可以独立于 ReportViewer 控件进行实例化,并直接在该博客文章所附的示例代码中使用。或者您可以传入 ReportViewer.LocalReport 即使您没有首先在 UI 中显示报告。
回答by Mozy
Check this out and see if it helps...http://scruffylookingcatherder.com/archive/2007/12/07/printing-reporting-services-2005-reports.aspx
检查一下,看看它是否有帮助... http://scruffylookingcatherder.com/archive/2007/12/07/printing-reporting-services-2005-reports.aspx
A little explanation: It's using the SSRS web service to render the report to an EMF image then send the image to the printer.
一点解释:它使用 SSRS Web 服务将报告呈现为 EMF 图像,然后将图像发送到打印机。
回答by Ramesh Rao
Please see the below link very useful for you http://social.msdn.microsoft.com/Forums/en-US/9f52d79d-5baf-4e84-97d5-7dbba6623b89/printing-without-the-reportviewer
请参阅以下对您非常有用的链接 http://social.msdn.microsoft.com/Forums/en-US/9f52d79d-5baf-4e84-97d5-7dbba6623b89/printing-without-the-reportviewer
回答by Pheakdey Sout
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