c# - 打印 RDLC 报告而不显示 ReportViewer 控件

标签 c# winforms report rdlc

我想知道是否可以将 DataGridView 中的数据直接发送/打印到 rdlc 报告,而无需将其绑定(bind)到 ReportViewer 控件。

关于将 dgv 数据绑定(bind)到报告查看器控件的话题有很多。 我不想使用报表查看器控件创建另一个表单,而是使用现有表单和 DataGridView 上的数据和打印按钮将数据发送到 RDLC 报告并打印.

这可能吗?
谢谢

最佳答案

您可以使用 LocalReport 以编程方式打印 RDLC 报告对象和 CreateStreamCallback回调函数。这是一个完整的 Microsoft 文档演练,您可能会发现它很有用:

为了更容易使用,我创建了一个 Print extension method您可以通过这种方式轻松使用它:

this.reportViewer1.LocalReport.Print();

扩展方法如下:

using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;

public static class LocalReportExtensions
{
    public static void Print(this LocalReport report)
    {
        var pageSettings = new PageSettings();
        pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
        pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
        pageSettings.Margins = report.GetDefaultPageSettings().Margins;
        Print(report, pageSettings);
    }

    public static void Print(this LocalReport report, PageSettings pageSettings)
    {
        string deviceInfo =
            $@"<DeviceInfo>
                <OutputFormat>EMF</OutputFormat>
                <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
                <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
                <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
                <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
                <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
                <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
            </DeviceInfo>";

        Warning[] warnings;
        var streams = new List<Stream>();
        var currentPageIndex = 0;

        report.Render("Image", deviceInfo, 
            (name, fileNameExtension, encoding, mimeType, willSeek) => 
            {
                var stream = new MemoryStream();
                streams.Add(stream);
                return stream;
            }, out warnings);

        foreach (Stream stream in streams)
            stream.Position = 0;

        if (streams == null || streams.Count == 0)
            throw new Exception("Error: no stream to print.");

        var printDocument = new PrintDocument();
        printDocument.DefaultPageSettings = pageSettings;
        if (!printDocument.PrinterSettings.IsValid)
            throw new Exception("Error: cannot find the default printer.");
        else
        {
            printDocument.PrintPage += (sender, e) =>
            {
                Metafile pageImage = new Metafile(streams[currentPageIndex]);
                Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);
                e.Graphics.DrawImage(pageImage, adjustedRect);
                currentPageIndex++;
                e.HasMorePages = (currentPageIndex < streams.Count);
                e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
            };
            printDocument.EndPrint += (Sender, e) =>
            {
                if (streams != null)
                {
                    foreach (Stream stream in streams)
                        stream.Close();
                    streams = null;
                }
            };
            printDocument.Print();
        }
    }
}

打印并显示打印对话框

以防万一有人想通过显示打印对话框进行打印,您可以在窗体上放置一个 ReportViewer 并将控件的 Visible 属性设置为 false,然后将数据传递给报告和时间 RenderingComplete事件触发,调用 PrintDialog:

关于c# - 打印 RDLC 报告而不显示 ReportViewer 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727037/

相关文章:

java - 在不同类之间共享 HashMap 和其他一些数据结构

c# - DataGridViewComboBoxColumn 不会在第一次单击时打开下拉列表

c# - 每 30 秒重复一次 ping

powershell - 将最新的窗口更新导出到Powershell中的CSV

c# - 使用 FileStream.Seek

c# - TreeView 的正则表达式

android - 为什么我从 Market 收到 java.lang.UnsatisfiedLinkError 报告

documentation - 验证用户输入是否属于功能需求?

c# - 膨胀 TextView 时出现空引用错误

c# - DataGridViewCell 编辑模式下的 Ctrl + c 复制整行