在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();                    }    }}


 

原创粉丝点击