打印RDLC报告而不显示ReportViewer控件 [英] Print RDLC Report without showing ReportViewer Control
问题描述
我想知道是否可以将 DataGridView
中的数据直接发送/打印到rdlc报表而不将其绑定到 ReportViewer
控制。
I was wondering if is possible to send/print data from DataGridView
directly to rdlc report without binding it to ReportViewer
control.
有很多关于将dgv数据绑定到报表查看器控件的线程。
我不想创建带有报表查看器控件的其他表单,但是将现有表单与 DataGridView
上的数据和打印按钮一起使用,以将数据发送到 RDLC
报告并打印。
There are many threads about binding dgv data to report viewer control.
I don't want to create another form with report viewer control, but use existing form with data on DataGridView
and on print button to send the data to RDLC
report and print it.
有可能吗?
谢谢
Is it possible?
Thanks
推荐答案
您可以使用 LocalReport
对象和 CreateStreamCallback
回调函数。这是一个完整的Microsoft文档演练,您可能会觉得有用:
You can print an RDLC report programmatically by using LocalReport
object and CreateStreamCallback
callback function. Here is a complete Microsoft docs walkthrough which you may find useful:
- Walkthrough: Printing a Local Report without Preview
为了易于使用,我创建了打印扩展方法,您可以通过以下方式轻松使用它:
To make it easier to use, I've created a Print extension method which you can easily use it this way:
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
并设置<$控件的c $ c> Visible 属性设置为false,然后将数据传递到报表,并在 RenderingComplete
事件触发,调用 PrintDialog
:
Just in case someone wants to print with showing Print dialog, you can put a ReportViewer
on form and set the Visible
property of the control to false then pass data to the report and when the RenderingComplete
event fired, call PrintDialog
:
- ReportViewer.PrintDialog Method
这篇关于打印RDLC报告而不显示ReportViewer控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!