You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中RDLC报表批量打印不同标题的参数设置问题

RDLC报表一次性打印多份不同标题报表的问题解决

你当前的代码连续调用original()和copy1(),但ReportViewer.RefreshReport()是异步执行的——第二个方法会立即覆盖第一个方法设置的参数、数据源和报表配置,所以最终只显示最后一次设置的COPY1报表。

以下是两种可行的解决方案:

方案一:单报表多页分组实现(适合预览+打印)

通过在数据源中添加标识字段,将多份报表的数据合并到一个DataTable,然后在RDLC报表中设置分组,让每份报表单独占一页。

代码修改

Private Sub PrintMultipleReports()
    ' 创建包含所有报表数据的DataTable,新增Header字段区分原始/副本
    Dim table As New DataTable()
    table.Columns.Add("Header", GetType(String))
    table.Columns.Add("value", GetType(String))
    table.Columns.Add("price", GetType(String))
    table.Columns.Add("quantity", GetType(Double))
    
    ' 添加原始报表数据
    table.Rows.Add("ORIGINAL", "test1", "10", "20")
    table.Rows.Add("ORIGINAL", "test2", "10", "20")
    ' 添加副本报表数据
    table.Rows.Add("COPY1", "test1", "10", "20")
    table.Rows.Add("COPY1", "test2", "10", "20")
    
    Dim reportDataSource1 = New ReportDataSource("DataSet1", table)
    Me.ReportViewer1.LocalReport.DataSources.Clear()
    Me.ReportViewer1.LocalReport.DataSources.Add(reportDataSource1)
    Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "Datatable_in_ReportViewer.Report1.rdlc"
    
    Me.ReportViewer1.RefreshReport()
End Sub

RDLC报表设计修改

  1. 在报表中添加分组,分组依据选择=Fields!Header.Value
  2. 打开分组属性,勾选在组的每个实例之间分页
  3. 将标题文本框的绑定值改为=Fields!Header.Value

方案二:生成独立报表流后合并打印(适合直接打印)

通过LocalReport.Render()方法分别生成每份报表的打印流(EMF格式适合打印),然后依次打印这些流。

代码修改

Imports System.Drawing.Printing
Imports System.IO
Imports Microsoft.Reporting.WinForms

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PrintMultipleReportsDirectly()
End Sub

Private Sub PrintMultipleReportsDirectly()
    ' 生成两份报表的打印流
    Dim originalBytes = GenerateReportBytes("ORIGINAL")
    Dim copy1Bytes = GenerateReportBytes("COPY1")
    
    ' 依次打印两份报表
    PrintReport(originalBytes)
    PrintReport(copy1Bytes)
End Sub

Private Function GenerateReportBytes(headerText As String) As Byte()
    Dim table As New DataTable()
    table.Columns.Add("value", GetType(String))
    table.Columns.Add("price", GetType(String))
    table.Columns.Add("quantity", GetType(Double))
    table.Rows.Add("test1", "10", "20")
    table.Rows.Add("test2", "10", "20")
    
    ' 创建独立的LocalReport实例,避免互相覆盖
    Dim localReport As New LocalReport()
    localReport.ReportEmbeddedResource = "Datatable_in_ReportViewer.Report1.rdlc"
    localReport.DataSources.Add(New ReportDataSource("DataSet1", table))
    
    ' 设置标题参数
    Dim parameters As New ReportParameterCollection()
    parameters.Add(New ReportParameter("paramHeader", headerText))
    localReport.SetParameters(parameters)
    
    ' 渲染为EMF格式(适合打印的矢量格式)
    Dim warnings As Warning() = Nothing
    Dim streamIds As String() = Nothing
    Dim mimeType, encoding, filenameExt As String
    
    Return localReport.Render("EMF", Nothing, mimeType, encoding, filenameExt, streamIds, warnings)
End Sub

Private Sub PrintReport(reportBytes As Byte())
    Using ms As New MemoryStream(reportBytes)
        Using printDoc As New PrintDocument()
            Dim pageIdx As Integer = 0
            Dim metafiles As New List(Of Metafile)()
            
            ' 解析EMF流为可打印的Metafile对象
            While ms.Position < ms.Length
                metafiles.Add(New Metafile(ms))
            End While
            
            ' 绑定打印事件
            AddHandler printDoc.PrintPage, Sub(sender, e)
                If pageIdx < metafiles.Count Then
                    e.Graphics.DrawImage(metafiles(pageIdx), e.PageBounds)
                    pageIdx += 1
                    e.HasMorePages = pageIdx < metafiles.Count
                Else
                    e.HasMorePages = False
                End If
            End Sub
            
            printDoc.Print()
        End Using
    End Using
End Sub

内容的提问来源于stack exchange,提问作者user22579796

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 16:35:35