当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《打印RDLC报告而不显示ReportViewer控件》经验,为你挑选了1个好方法。

我想知道是否可以DataGridView直接向rdlc报告发送/打印数据而不将其绑定到ReportViewer控件.

关于将dgv数据绑定到报表查看器控件有很多线程.我不想使用报表查看器控件创建另一个表单,但是使用现有表单DataGridView和打印按钮上的数据来发送数据以进行RDLC报告和打印.

可能吗?
谢谢



1> Reza Aghaei..:

打印不显示"打印"对话框

作为选项,您可以使用LocalReport对象和CreateStreamCallback回调函数以编程方式打印RDLC报告.这是一个完整的msdn演练:

演练:无需预览即可打印本地报告

打印显示"打印"对话框

作为另一个选项,将一个ReportVieweron表格并将Visible控件的属性设置为false,然后将数据传递给报表,当RenderingComplete事件触发时,调用PrintDialog:

ReportViewer.PrintDialog方法


对于第一个选项(没有显示打印对话框的打印),我创建了一个Print扩展方法,您可以通过这种方式轻松使用它:

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 =
            $@"
                EMF
                {pageSettings.PaperSize.Width * 100}in
                {pageSettings.PaperSize.Height * 100}in
                {pageSettings.Margins.Top * 100}in
                {pageSettings.Margins.Left * 100}in
                {pageSettings.Margins.Right * 100}in
                {pageSettings.Margins.Bottom * 100}in
            ";

        Warning[] warnings;
        var streams = new List();
        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();
        }
    }
}

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有