c# – 在没有预览的情况下打印ReportViewer

c# – 在没有预览的情况下打印ReportViewer,第1张

概述我正在使用Visual Studio 2010 C# Windows窗体应用程序 MySql 我有一个100%正常工作的报表查看器.报告查看器充满了我的数据库的数据,它显示我点击按钮打印并打印…但是,我的客户不想点击这个按钮,他想自动打印.当我调用ReportViewer时,它自己打印而无需单击按钮来执行此 *** 作.谁能告诉我我是怎么做到的? 我从工具箱中尝试了reportviewer1.print和 我正在使用Visual Studio 2010 C# Windows窗体应用程序 MySql
我有一个100%正常工作的报表查看器.报告查看器充满了我的数据库的数据,它显示我点击按钮打印并打印…但是,我的客户不想点击这个按钮,他想自动打印.当我调用ReportVIEwer时,它自己打印而无需单击按钮来执行此 *** 作.谁能告诉我我是怎么做到的?

我从工具箱中尝试了reportvIEwer1.print和Printdocument.但我不知道如何正确使用这些.

谢谢你的关注!

解决方法 我有同样的问题,这是我使用的代码,工作就像一个魅力!
using System;using System.IO;using System.Text;using System.Globalization;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Printing;using Microsoft.Reporting.WinForms;using System.Collections.Generic;using System.Collections.Specialized;using System.Diagnostics;using System.ComponentModel;using System.Data;using System.linq;using System.Threading.Tasks;using System.windows.Forms;namespace NewLabelPrinter{    /// <summary>    /// The ReportPrintdocument will print all of the pages of a ServerReport or LocalReport.    /// The pages are rendered when the print document is constructed.  Once constructed,/// call Print() on this class to begin printing.    /// </summary>    class AutoprintCls : Printdocument    {        private PageSettings m_pageSettings;        private int m_currentPage;        private List<Stream> m_pages = new List<Stream>();        public AutoprintCls(ServerReport serverReport)            : this((Report)serverReport)        {            RenderAllServerReportPages(serverReport);        }        public AutoprintCls(LocalReport localReport)            : this((Report)localReport)        {            RenderAllLocalReportPages(localReport);        }        private AutoprintCls(Report report)        {            // Set the page settings to the default defined in the report            ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();            // The page settings object will use the default printer unless            // PageSettings.PrinterSettings is changed.  This assumes there            // is a default printer.            m_pageSettings = new PageSettings();            m_pageSettings.PaperSize = reportPageSettings.PaperSize;            m_pageSettings.margins = reportPageSettings.margins;        }        protected overrIDe voID dispose(bool disposing)        {            base.dispose(disposing);            if (disposing)            {                foreach (Stream s in m_pages)                {                    s.dispose();                }                m_pages.Clear();            }        }        protected overrIDe voID OnBeginPrint(PrintEventArgs e)        {            base.OnBeginPrint(e);            m_currentPage = 0;        }        protected overrIDe voID OnPrintPage(PrintPageEventArgs e)        {            base.OnPrintPage(e);            Stream pagetoPrint = m_pages[m_currentPage];            pagetoPrint.position = 0;            // Load each page into a Metafile to draw it.            using (Metafile pageMetafile = new Metafile(pagetoPrint))            {                Rectangle adjustedRect = new Rectangle(                        e.PageBounds.left - (int)e.PageSettings.HardmarginX,e.PageBounds.top - (int)e.PageSettings.HardmarginY,e.PageBounds.WIDth,e.PageBounds.Height);                // Draw a white background for the report                e.Graphics.FillRectangle(Brushes.White,adjustedRect);                // Draw the report content                e.Graphics.DrawImage(pageMetafile,adjustedRect);                // Prepare for next page.  Make sure we haven't hit the end.                m_currentPage++;                e.HasMorePages = m_currentPage < m_pages.Count;            }        }        protected overrIDe voID OnqueryPageSettings(queryPageSettingsEventArgs e)        {            e.PageSettings = (PageSettings)m_pageSettings.Clone();        }        private voID RenderAllServerReportPages(ServerReport serverReport)        {            try            {                string deviceInfo = CreateEMFDeviceInfo();                // Generating Image renderer pages one at a time can be expensive.  In order                // to generate page 2,the server would need to recalculate page 1 and throw it                // away.  Using PersistStreams causes the server to generate all the pages in                // the background but return as soon as page 1 is complete.                nameValueCollection firstPageParameters = new nameValueCollection();                firstPageParameters.Add("rs:PersistStreams","True");                // GetNextStream returns the next page in the sequence from the background process                // started by PersistStreams.                nameValueCollection nonFirstPageParameters = new nameValueCollection();                nonFirstPageParameters.Add("rs:GetNextStream","True");                string mimeType;                string fileExtension;                Stream pageStream = serverReport.Render("IMAGE",deviceInfo,firstPageParameters,out mimeType,out fileExtension);                // The server returns an empty stream when moving beyond the last page.                while (pageStream.Length > 0)                {                    m_pages.Add(pageStream);                    pageStream = serverReport.Render("IMAGE",nonFirstPageParameters,out fileExtension);                }            }            catch (Exception e)            {                MessageBox.Show("possible missing information ::  " + e);            }        }        private voID RenderAllLocalReportPages(LocalReport localReport)        {            try            {                string deviceInfo = CreateEMFDeviceInfo();                Warning[] warnings;                localReport.Render("IMAGE",LocalReportCreateStreamCallback,out warnings);            }            catch (Exception e)            {                MessageBox.Show("error :: " + e);            }        }        private Stream LocalReportCreateStreamCallback(            string name,string extension,EnCoding enCoding,string mimeType,bool willSeek)        {            MemoryStream stream = new MemoryStream();            m_pages.Add(stream);            return stream;        }        private string CreateEMFDeviceInfo()        {            PaperSize paperSize = m_pageSettings.PaperSize;            margins margins = m_pageSettings.margins;            // The device info string defines the page range to print as well as the size of the page.            // A start and end page of 0 means generate all pages.            return string.Format(                CultureInfo.InvariantCulture,"<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><margintop>{0}</margintop><marginleft>{1}</marginleft><marginRight>{2}</marginRight><marginBottom>{3}</marginBottom><PageHeight>{4}</PageHeight><PageWIDth>{5}</PageWIDth></DeviceInfo>",ToInches(margins.top),ToInches(margins.left),ToInches(margins.Right),ToInches(margins.Bottom),ToInches(paperSize.Height),ToInches(paperSize.WIDth));        }        private static string ToInches(int hundrethsOfInch)        {            double inches = hundrethsOfInch / 100.0;            return inches.ToString(CultureInfo.InvariantCulture) + "in";        }    }}

这个课程的设置非常适合您的需要,然后您需要做的就是:

private voID Autoprint()    {        AutoprintCls autoprintme = new AutoprintCls(reportVIEwer1.LocalReport);        autoprintme.Print();    }

嘿presto它打印.只需将此附加到代码中的方法(可能在报告载入之后.)和您的设置很好!

选项:(未经测试)

如发现这打印到默认打印机,要更改打印机,您可以执行以下 *** 作:

if (printDialog.ShowDialog() == DialogResult.OK) {    m_pageSettings .PrinterSettings.Printername = printDialog.PrinterSettings.Printername;}

没有经过测试,因为我不再有任何源代码来测试这个

总结

以上是内存溢出为你收集整理的c# – 在没有预览的情况下打印ReportViewer全部内容,希望文章能够帮你解决c# – 在没有预览的情况下打印ReportViewer所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1249629.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存