【问题标题】:How to print an SSRS (SQL Server Report Service) Report in VB.NET如何在 VB.NET 中打印 SSRS(SQL Server 报表服务)报表
【发布时间】:2016-06-06 18:04:10
【问题描述】:

如何通过 VB.NET 打印 SSRS 报告而不需要一些不必要的复杂代码?

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

【问题讨论】:

    标签: vb.net reporting-services printing


    【解决方案1】:

    看,只是凡人!

    我目前正在将一些报表从 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多