如何在VB.NET中打印SSRS(SQL Server报表服务)报表

时间:2016-06-06 18:04:11

标签: vb.net reporting-services printing

如何在没有一些不必要的复杂代码的情况下通过VB.NET打印SSRS报告?

也许是我可以导入到我的程序中的类的形式?

1 个答案:

答案 0 :(得分:0)

看到凡人! < - 只是凡人

我目前正在将某些报告从Crystal Reports迁移到SSRS。 CR可以做的一件事就是SSRS在做的事情就是打印。经过大量的阅读后,我总是后悔自己决定建议转向SSRS。我终于偶然发现了一个可接受的解决方案。我不确定它在大规模打印方面的表现如何,我可能会在以后改进它。

Imports System.Drawing.Printing
Imports System.Drawing.Imaging
Imports System.IO
Imports Microsoft.Reporting.WinForms
Public Class PrintSSRS
    Private pages As New List(Of Metafile)
    Private pageIndex As Integer = 0

    Public Sub Print_Report(ReportServerUrl As String, ReportPath As String, Parameters() As ReportParameter, Optional LandScapeView As Boolean = False, Optional ShowPrintDialog As Boolean = False)
        Dim Report As New ReportViewer

        With Report
            .Visible = False
            .ProcessingMode = ProcessingMode.Remote
            .ServerReport.ReportPath = ReportPath
            .ServerReport.ReportServerUrl = New Uri(ReportServerUrl)
        End With

        Dim doc As New Printing.PrintDocument()
        doc = New Printing.PrintDocument()

        Dim DeviceInfo As New System.Text.StringBuilder
        If LandScapeView Then
            doc.DefaultPageSettings.Landscape = True
            AddHandler doc.PrintPage, AddressOf PrintPageHandlerLandScapeView
            DeviceInfo.AppendLine("<DeviceInfo>")
            DeviceInfo.AppendLine("    <OutputFormat>emf</OutputFormat>")
            DeviceInfo.AppendLine("    <PageWidth>11in</PageWidth>")
            DeviceInfo.AppendLine("    <PageHeight>8.5in</PageHeight>")
            DeviceInfo.AppendLine("    <MarginTop>0.25in</MarginTop>")
            DeviceInfo.AppendLine("    <MarginLeft>0.25in</MarginLeft>")
            DeviceInfo.AppendLine("    <MarginRight>0.25in</MarginRight>")
            DeviceInfo.AppendLine("    <MarginBottom>0.25in</MarginBottom>")
            DeviceInfo.AppendLine("</DeviceInfo>")
        Else
            AddHandler doc.PrintPage, AddressOf PrintPageHandlerPortraitView
            DeviceInfo.AppendLine("<DeviceInfo>")
            DeviceInfo.AppendLine("    <OutputFormat>emf</OutputFormat>")
            DeviceInfo.AppendLine("    <PageWidth>8.5in</PageWidth>")
            DeviceInfo.AppendLine("    <PageHeight>11in</PageHeight>")
            DeviceInfo.AppendLine("    <MarginTop>0.25in</MarginTop>")
            DeviceInfo.AppendLine("    <MarginLeft>0.25in</MarginLeft>")
            DeviceInfo.AppendLine("    <MarginRight>0.25in</MarginRight>")
            DeviceInfo.AppendLine("    <MarginBottom>0.25in</MarginBottom>")
            DeviceInfo.AppendLine("</DeviceInfo>")
        End If

        If ShowPrintDialog Then
            Dim dialog As New PrintDialog()
            dialog.Document = doc
            Dim print As DialogResult
            print = dialog.ShowDialog()
            doc.PrinterSettings = dialog.PrinterSettings
        End If

        Dim warnings() As Microsoft.Reporting.WinForms.Warning
        Dim streamids() As String
        Dim mimeType, encoding, filenameExtension As String
        mimeType = "" : encoding = "" : filenameExtension = ""

        Dim data() As Byte
        Report.ServerReport.SetParameters(Parameters)
        data = Report.ServerReport.Render("Image", DeviceInfo.ToString, mimeType, encoding, filenameExtension, streamids, warnings)
        pages.Add(New Metafile(New MemoryStream(data)))

        For Each pageName As String In streamids
            data = Report.ServerReport.RenderStream("Image", pageName, DeviceInfo.ToString, mimeType, encoding)
            pages.Add(New Metafile(New MemoryStream(data)))
        Next

        doc.Print()
        Report.RefreshReport()

        pages.Clear()
        pageIndex = 0
        doc.Dispose()
    End Sub

    Private Sub PrintPageHandlerPortraitView(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        Dim page As Metafile = pages(pageIndex)
        pageIndex += 1
        Dim pWidth As Integer = 827
        Dim pHeight As Integer = 1100

        e.Graphics.DrawImage(page, 0, 0, pWidth, pHeight)
        e.HasMorePages = pageIndex < pages.Count
    End Sub

    Private Sub PrintPageHandlerLandScapeView(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        Dim page As Metafile = pages(pageIndex)
        pageIndex += 1
        Dim pWidth As Integer = 1100
        Dim pHeight As Integer = 827

        e.Graphics.DrawImage(page, 0, 0, pWidth, pHeight)
        e.HasMorePages = pageIndex < pages.Count
    End Sub
End Class

我不确定如何让AddressOf进入主子网站。它看起来像一个While循环,但我无法让e正常工作。

可能还有很大的改进空间。

原始来源 http://rani-irsan.blogspot.com/2015/04/ssrs-direct-printing-to-printer.html