在没有预览的情况下打印ServerReport [英] Printing ServerReport without Preview

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

问题描述

我有一个SQLServer 2005 Reporting Services ServerReport,它已通过ReportViewer控件部署并由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.

我我一直在寻找抛出MSDN的方法,但是仅引用本地报告:

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

http://msdn.microsoft.com/zh-cn/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/b ryanke / 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天全站免登陆