在vs中后台打印rdlc报表
来源:互联网 发布:pc机装mac 编辑:程序博客网 时间:2024/05/18 01:47
最近的一个项目要求在后台可以打印rdlc报表文件,于是参照微软官方的例子写了一个PrintService。
感觉这个应该蛮常用的,和大家分享下,自己也备忘记一下。
至于怎么构建数据源和画rdlc报表文件这里就不赘述,主要是打印功能的实现。
各方法的作用见代码注释。
using System;using System.Collections;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Printing;using System.IO;using System.Text;using Microsoft.Reporting.WebForms;namespace Domain.Base.Service{ public class PrintService { private int m_currentPageIndex; private IList<Stream> m_streams;//创建打印数据流 private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { Stream stream = new MemoryStream(); m_streams.Add(stream); return stream; } // 打印文件 private void PrintPage(object sender, PrintPageEventArgs ev) { var pageImage = new Metafile(m_streams[m_currentPageIndex]); // 定义打印边界 var adjustedRect = new Rectangle( ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX, ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY, ev.PageBounds.Width, ev.PageBounds.Height); // 确定背景 ev.Graphics.FillRectangle(Brushes.White, adjustedRect); // 画出打印图 ev.Graphics.DrawImage(pageImage, adjustedRect); // 打印下一页 m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); }/// <summary> /// 打印方法 /// </summary> /// <param name="reportTemplatePath">打印报表的模板文件,rdlc文件</param> /// <param name="reportDataList">模板数据源</param> /// <param name="dataSourceName">报表模板的数据源名称</param> /// <param name="fileName">生成打印文件的名称</param> public void Print(string reportTemplatePath, IList reportDataList, string dataSourceName, string fileName = "") {//取得rdlc报表文件 var report = new LocalReport { ReportPath = reportTemplatePath };//绑定rdlc报表数据源 report.DataSources.Add(new ReportDataSource(dataSourceName, reportDataList)); string deviceInfo = @"<DeviceInfo> <OutputFormat>EMF</OutputFormat> <PageWidth>8.5in</PageWidth> <PageHeight>11in</PageHeight> <MarginTop>0.25in</MarginTop> <MarginLeft>0.25in</MarginLeft> <MarginRight>0.25in</MarginRight> <MarginBottom>0.25in</MarginBottom> </DeviceInfo>"; Warning[] warnings; m_streams = new List<Stream>();//打印流 report.Render("Image", deviceInfo, CreateStream, out warnings); foreach (Stream stream in m_streams) stream.Position = 0; PrintDocument printDoc = new PrintDocument(); if (!printDoc.PrinterSettings.IsValid) { throw new Exception("Error: cannot find the default printer."); } printDoc.PrintPage += PrintPage; m_currentPageIndex = 0;//后台打印到打印机 printDoc.Print(); } }}
- 在vs中后台打印rdlc报表
- .Net中 RDLC报表打印类
- 在VS 2005 和 VS 2008 中使用RDLC使用免费报表
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008[转]
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- 在vs2005中的ReportViewer(RDLC报表)中使用直接打印功能,在vs2005使用ReportViewer2008
- rdlc报表连续打印
- rdlc报表打印
- rdlc报表预览打印
- RDLC 报表打印
- 在 MVC 4 中使用RDLC 报表
- 在不预览的情况下打印RDLC报表
- [转]在不预览的情况下打印RDLC报表
- Web项目中使用RDLC报表可以打印吗?
- Web项目中使用RDLC报表可以打印吗?
- RDLC后台自定义报表模板
- RDLC报表打印控件使用说明
- 如何在没有万用表的情况下,辨别光敏与红外发射管
- Faststone不错
- 嫣然一笑苦了情
- SkipList 跳表
- logrotate
- 在vs中后台打印rdlc报表
- C#开发之生成XmlDocument用来返回错误提示信息
- C#里Math.Round()函数问题--5的處理
- linux安装jdk
- 解决Bitmap中的OOM问题
- 简单位操作(NVIDIA)
- java设计模式之策略模式
- 51单片机扩展中断的四种方法
- IE 下 setInterval 的工作问题