不带预览打印 ServerReport [英] Printing ServerReport without Preview

查看:15
本文介绍了不带预览打印 ServerReport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 ReportViewer 控件部署了一个 SQLServer 2005 Reporting Services ServerReport,并经常被我的 Winforms 应用程序 (Framework 2.0) 使用.

I have a SQLServer 2005 Reporting Services ServerReport deployed and frequently used by my Winforms app (Framework 2.0) via the ReportViewer control.

我需要的是从应用程序的一种形式中提供一个一键式打印按钮,该按钮仅触发打印对话框,而无需打开 ReportViewer.

What I need is to provide a one-click print button from one of the forms of the app that triggers only the print dialog, without bringing up the ReportViewer.

我一直在尝试将报告呈现为字节数组,但我无法通过.

I've been experimenting with rendering the report to a byte array, but I can't get pass that.

此报告有多个页面,所以我不知道适用于本地报告的图像"呈现是否也适用于服务器报告.

This report has multiple pages, so i don't know if the "Image" rendering that works for local reports it's going to work also for server reports.

我一直在寻找 throw MSDN,但只引用了本地报告:

I've been looking throw MSDN but there is only reference to local reports:

http://msdn.microsoft.com/en-us/library/ms252091(VS.80).aspx

我在服务器端报告中获得的少量信息是使用对 ReportServer Web 服务的 Web 引用,而我不想要那样.

And the little information that I can get on server side reports are using Web references to the ReportServer web service, and I don't want that.

http://blogs.msdn.com/bryanke/articles/71491.aspx

有没有办法使用打印对话框打印服务器报告,而不向用户显示报告查看器(我不介意它是否在幕后)?

Is there any way to print a server report, using the print dialog, without showing the report viewer to the user (I don't mind if it's behind the scenes)?

推荐答案

好的,终于想通了.

那篇博文几乎包含了我需要的所有内容,但我将在这里发布完整的答案以供参考.

That blog post has almost everything that I needed, but I'm going to post the full answer here for references.

我最终在幕后使用了报表查看器对象,但只是为了方便,因为它不是必需的.

I ended up using the report viewer object behind the scenes, but only for convenience, since it's not required.

第一步是询问用户打印机设置:

The first step is asking the user for the printer settings:

Dim doc As New Printing.PrintDocument()
AddHandler doc.PrintPage, AddressOf PrintPageHandler
Dim dialog As New PrintDialog()
dialog.Document = doc
Dim print As DialogResult
print = dialog.ShowDialog()
doc.PrinterSettings = dialog.PrinterSettings

有了这个,我们继续配置我们的报告调用:修改此字符串,您可以在任何纸张尺寸和任何方向上打印(横向切换高度和宽度),但报告本身必须配置为相同的页面布局.

Having that, we proceed to configure our report call: Modifying this string, you can get to print on any paper size and any orientation (switching height and width for landscape), but the report itself must be configured in the same page layout.

Dim deviceInfo As String = _
"<DeviceInfo>" + _
"<OutputFormat>emf</OutputFormat>" + _
"  <PageWidth>8.5in</PageWidth>" + _
"  <PageHeight>11in</PageHeight>" + _
"  <MarginTop>0.25in</MarginTop>" + _
"  <MarginLeft>0.25in</MarginLeft>" + _
"  <MarginRight>0.25in</MarginRight>" + _
"  <MarginBottom>0.25in</MarginBottom>" + _
"</DeviceInfo>"

Dim warnings() As Warning
Dim streamids() As String
Dim mimeType, encoding, filenameExtension, path As String
mimeType = "" : encoding = "" : filenameExtension = ""

最后,我们呈现报告及其所有页面.

Finally, we render the report with all its pages.

请注意,如果报表只有一页,则从不使用 renderStream 方法.

Note that if the report has only one page, the renderStream method is never used.

rpt_control 是报表查看器控件,之前配置过,针对的是服务器报表.

rpt_control is the report viewer control, previously configured and aiming at a server report.

请注意,在此代码中,我们将页面添加到列表中.这个列表是一个全局变量,因为它在 PrintPageHandler 方法中是需要的.

Note allso that in this code we add pages to a list. This list is a global variable, since it's needed in the PrintPageHandler method.

Dim data() As Byte
rpt_control.ServerReport.SetParameters(_parametros)
data = rpt_control.ServerReport.Render("Image", deviceInfo, mimeType, encoding, filenameExtension, streamids, warnings)
pages.Add(New Metafile(New MemoryStream(data)))

For Each pageName As String In streamids
    data = rpt_control.ServerReport.RenderStream("Image", pageName, deviceInfo, mimeType, encoding)
    pages.Add(New Metafile(New MemoryStream(data)))
Next
doc.Print()

到现在为止,我们根本没有做任何打印,这实际上是由我们之前引用的 PrintPageHandler 方法处理的.

Until now, we haven't done any printing at all, this is actually handled by the PrintPageHandler method that we referenced earlier.

Dim pages As New List(Of Metafile)
Dim pageIndex As Integer = 0
Private Sub PrintPageHandler(ByVal sender As Object, ByVal e As PrintPageEventArgs)
    Dim page As Metafile = pages(pageIndex)
    pageIndex += 1
    e.Graphics.DrawImage(page, 0, 0, page.Width, page.Height)
    e.HasMorePages = pageIndex < pages.Count
End Sub

这篇关于不带预览打印 ServerReport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆