转 http://blog.163.com/xu_shuhao/blog/static/52577487201072284619646/
本文和大家讲下C#不使用ReportViewer直接打印Report文件(rdlc)的实现代码,主要讲如何不使用报表预览控件(ReportViewer),直接将报表的内容在打印机上打印出来。
一般情况下,我们设计好报表后,在程序运行的时候,会将其加载到ReprotViewer控件中进行预览并打印,但有些情况下,我们不想预览报表的内容,而直接将其在打印机上打印出来,又该怎么做呢?要想实现直接打印的功能,我们需要用到.Net提供的2个对象,LocalReport和PrintDocument。LocalReport对象负责加载一个报表文件生成实际的报表并将报表按照知道的格式输出,PrintDocument对象负责将LocalReport的输出内容发送到打印机打印。具体的实现步骤如下:
Step 1: 声明一个LocalReport对象并加载一个报表文件(假定我们已经设计好一个报表文件,名称为PrintMe.rdlc)。
1: LocalReport report = new LocalReport();
2: //设置需要打印的报表的文件名称。
3: report.ReportPath = @"c:\PrintMe.rdlc";
4: //创建要打印的数据源
5: ReportDataSource source = new ReportDataSource(SourceTalbe.TableName, SourceTalbe);
6: report.DataSources.Add(source);
7: //刷新报表中的需要呈现的数据
8: report.Refresh();
Step 2: 将报表的内容输出为指定格式的数据流。
1: string deviceInfo =
2: "<DeviceInfo>" +
3: " <OutputFormat>EMF</OutputFormat>" +
4: " <PageWidth>8.5in</PageWidth>" +
5: " <PageHeight>11in</PageHeight>" +
6: " <MarginTop>0.25in</MarginTop>" +
7: " <MarginLeft>0.25in</MarginLeft>" +
8: " <MarginRight>0.25in</MarginRight>" +
9: " <MarginBottom>0.25in</MarginBottom>" +
10: "</DeviceInfo>";
11: Warning[] warnings;
12: //将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
13: report.Render("Image", deviceInfo, CreateStream, out warnings);
这里使用LocalReport对象的Render方法,将报表的内容输出到指定的数据流(Stream)中。Render方法的第一个参数指定输出的流的格式,这里指定为Image格式(图形格式);第二个参数为一个XML格式的字符串,用来描述输出格式的详细信息;第三个参数是一个回调函数(CreateStreamCallback委托类型),你需要为这个参数声明一个函数,Render方法会将报表的内容输出到这个函数返回的Stream对象的实例中,这个函数看上去类似下面的声明
1: //声明一个Stream对象的列表用来保存报表的输出数据
2: //LocalReport对象的Render方法会将报表按页输出为多个Stream对象。
3: private List<Stream> m_streams;
4: //用来提供Stream对象的函数,用于LocalReport对象的Render方法的第三个参数。
5: private Stream CreateStream(string name, string fileNameExtension,
6: Encoding encoding, string mimeType, bool willSeek)
7: {
8: //如果需要将报表输出的数据保存为文件,请使用FileStream对象。
9: Stream stream = new MemoryStream();
10: m_streams.Add(stream);
11: return stream;
12: }
你可以使用这个函数的参数执行更多的操作,具体内容请参考CreateStreamCallback委托。
第四个参数用来输出报表处理过程中产生的警告信息。
Step 3: 使用PrintDocument对象执行打印操作。
1: //用来记录当前打印到第几页了
2: private int m_currentPageIndex;
3:
4: private void Print()
5: {
6: m_currentPageIndex = 0;
7:
8: if (m_streams == null || m_streams.Count == 0)
9: return;
10: //声明PrintDocument对象用于数据的打印
11: PrintDocument printDoc = new PrintDocument();
12: //指定需要使用的打印机的名称,使用空字符串""来指定默认打印机
13: printDoc.PrinterSettings.PrinterName = "";
14: //判断指定的打印机是否可用
15: if (!printDoc.PrinterSettings.IsValid)
16: {
17: MessageBox.Show("Can't find printer");
18: return;
19: }
20: //声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。
21: printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
22: //执行打印操作,Print方法将触发PrintPage事件。
23: printDoc.Print();
24: }
具体的PrintPage事件处理程序如下:
1: private void PrintPage(object sender, PrintPageEventArgs ev)
2: {
3: //Metafile对象用来保存EMF或WMF格式的图形,
4: //我们在前面将报表的内容输出为EMF图形格式的数据流。
m_streams[m_currentPageIndex].Position = 0;
5: Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
6: //指定是否横向打印
7: ev.PageSettings.Landscape = false;
8: //这里的Graphics对象实际指向了打印机
9: ev.Graphics.DrawImage(pageImage, 0, 0);
10: m_streams[m_currentPageIndex].Close();
11: m_currentPageIndex++;
12: //设置是否需要继续打印
13: ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
14: }
到此,我们的报表数据就已经打印出来了,在这个过程中,你可以根据需要添加自己的打印逻辑。
获取本地的打印机信息请参考PrinterSettings。
分享到:
相关推荐
ReportViewer 有些网络从网官下载慢,可从此处下载: Microsoft RDLC Report Designer
RDLC报表部署时服务器缺少DLL,错误提示: “An error occurred during local report processing. The definition of the report 'Main Report' is invalid. An unexpected error occurred in Report Processing. ...
//有了ADO.NET,可把读取来的数据直接加载到数据集中 ds.Tables[0].Load(read); read.Close(); Con.Close(); //为查看器提供本地报表数据 CrystalReport1 cry = new CrystalReport1(); cry.SetDataSource...
RDLC前身是RDL,嵌套在sql中的一个ReportBuilder工具。 微软vs以前用水晶报表,慢慢开始推广自己的RDL、RDLC格式。 微软做了两种设计器:嵌入for visual studio的、独立于程序运行的。 另外还有一个第三方的设计器...
在没有rdlc为后缀的报表文件和没有xsd为后缀的DataSet数据源的情况下,依据从数据库中获取的数据生成RDLC报表。 Visual Studio 的最新版本是2022,在以住的版本中微软都会有一个 Microsoft RDLC Report Designer ...
该压缩包为vs2017环境下对rdlc报表编辑所需的reportviewer插件,安装顺序依次为reportviewer.exe、reportviewLP.exe、 reportviewer.msi。
基于ReportViewer+C#实现的动态绑定RDLC 的数据报表程序代码。
vs2017 RDLC报表插件安装
使用RDLC报表简单显示虚拟人物的消费报表。
this.reportViewer1.LocalReport.ReportEmbeddedResource = "报表test.Report1.rdlc"; reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting....
2.创建 Report1.rdlc 文件,将上一步创建的 DataSet 关联进来。 3.根据自己的需求设计报表文件,可以设置传入参数。 4.在窗体上添加 ReportViewer ,调用 SetReportViewerData<T> 函数即可。 /// /// 显示报表文件...
数据库备份直接还原即可运行,使用reportviewer,根据查询条件生成报表,传递参数。包括linq处理数据的一些逻辑。适合小白查看,文件:Window1.xaml;Report2.rdlc;包含一个datetimepicker,可设置只选择年月。
一种是直接使用 visual studio 2010 版本, 安装后自动带有 报表 另一种是在visual studio 2022 版本 安装插件, Report Viewer 查看器来实现
未能加载文件或程序集“Microsoft.ReportViewer.ProcessingObjectModel, Version=8.0.0.0/9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。” 现在提供...
ReportViewer发布到服务器后如果少这几个文件会出错!
基于C#的WinForm打印程序演示...添加一个报表文件 Report1.rdlc。 在左边设置框【数据集】右键,添加数据集. 【新建】-【对象】选中Student数据源 5.设置打印页面,绑定数据到页面上 6.给步骤2的控件 ReportViewer
ReportViewer2010、ReportViewer(10.0)microsoft report viewer 2010简称为reportviewer2010,官方全称为microsoft report viewer 2010 redistributable package,包含了用于查看使用Microsoft报告技术设计的报告的...
节点rdlc Node.JS 绑定到 .NET 的 ReportViewer(仅限 Windows) 使用此模块,您可以运行 RDLC 报告并使用 javascript 数据填充它们。 强烈建议您阅读并使用来构建您的报告设计(RDLC 文件) 网上有很多关于如何创建...
MICROSOFT® REPORT VIEWER 2015 RUNTIME(rdlc),最新版
RDLCPrinter是RDLC LocalReport的库。 您可以通过示例使用SQL Server数据集。 使用RDLCPrinter类,您可以将报告导出为PDF,Word或PNG图像...该程序包中包含WPF预览控件以及一些有用的控件! 只有英语才需要使用...