gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:21:06 25 4
gpt4 key购买 nike

我想知道是否可以将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com