Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在不预览的情况下打印ReportViewer_C#_Mysql_Reportviewer - Fatal编程技术网

C# 在不预览的情况下打印ReportViewer

C# 在不预览的情况下打印ReportViewer,c#,mysql,reportviewer,C#,Mysql,Reportviewer,我使用的是Visual Studio 2010 C#Windows窗体应用程序+MySql 我有一个100%工作的报告查看器。reportviewer充满了我数据库的数据,它显示出来,我点击按钮打印,它打印。。。但是,我的客户不想点击这个按钮,他想自动打印。当我调用ReportViewer时,它会自动打印,无需单击按钮即可完成打印。谁能告诉我我是怎么做到的 我尝试了reportviewer1.print和工具箱中的PrintDocument。但我不知道如何正确使用这些 谢谢大家的关注 这就是我们

我使用的是Visual Studio 2010 C#Windows窗体应用程序+MySql 我有一个100%工作的报告查看器。reportviewer充满了我数据库的数据,它显示出来,我点击按钮打印,它打印。。。但是,我的客户不想点击这个按钮,他想自动打印。当我调用ReportViewer时,它会自动打印,无需单击按钮即可完成打印。谁能告诉我我是怎么做到的

我尝试了reportviewer1.print和工具箱中的PrintDocument。但我不知道如何正确使用这些


谢谢大家的关注

这就是我们使用Crystal Reports的方式

            ReportDocument rd = new ReportDocument();
            // Insert code to run the report here

            // This gets the user's default printer to print to.
            PrintDialog prt = new PrintDialog();
            rd.PrintOptions.PrinterName = prt.PrinterSettings.PrinterName; 
            // This does the printing.
            rd.PrintToPrinter(copies, true, 1, 1000); 

我认为对你来说,与
PrintOptions.PrinterName
相当的是
ReportViewer.PrinterSettings
,但我怀疑你真正需要的是与
PrintToPrinter()
,这在我的简要介绍中没有看到。

如果我的Crystal Report答案对你不起作用,你也可以试试。同样,我还没有测试过它,也不能确定它是否有效,但它看起来像是一种完全不同的方法,可能会有效。如果没有,那我就帮不了什么忙了,不幸的是。

我也有同样的问题,这是我使用的代码,工作起来很有魅力

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", deviceInfo, nonFirstPageParameters, out mimeType, 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", deviceInfo, 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";
        }
    }
}
嘿,普雷斯托,它打印出来了。只需将其附加到代码中的一个方法(可能在加载报告之后),就可以很好地完成您的设置

选项:(未测试)

当打印到默认打印机时,要更改打印机,可以执行以下操作:

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

没有测试,因为我不再有任何源代码来测试这一点

这是使用CrystalReports吗?这是我所知道的唯一的报表查看器,但这并不意味着它是唯一的。不,它的报表查看器(只是工具箱中的“报表”)你能给我一个机会,告诉我怎么做吗?也许没什么不同=)真的谢谢你回答我Bobson=)我现在就试试,我不希望打印对话框自动打印,我现在就试试,尽快给出反馈。我没有“报告文件”;s唯一没有关于“打印”选项的ReportDataSource;s@alannaidon-对于您来说,它应该是
ReportViewer
,而不是
ReportDocument
,但想法是一样的。如果不起作用,试试我的另一个答案。我想这对你的问题更准确。没关系,波森。我用SendKey尝试了一些东西,按下了ReportViewer自动创建的“打印”按钮。你知道吗?@alannaidon-我想我从来没有用过SendKey。不过,这听起来是一个很好的简单解决方案。请随意添加它作为一个新的答案,并接受它,如果你得到它的工作。这里提供的链接工作得很好,完全封装在一个类中,所以你不必添加任何方法到你的代码,只需调用这个类。嗨,有可能选择打印机在你的代码从打印对话框?是的,我相信使用打印对话框将适合你的需要,看一看是的,但我不知道如何为您的代码提供printdialog…我尝试m_pageSettings.PrinterSettings.PrinterName=PrintDialog1.PrinterSettings.PrinterName。。。但仍然是默认打印机…因此您创建了一个新的printDialog-“printDialog pdiag=new printDialog();“类似于pdiag.PrinterSettings.PrinterName=“myPrinterName”;事实上我会更新我的回答你好。即使将打印机设置为其他名称,默认情况下仍会打印。你能帮忙吗?
if (printDialog.ShowDialog() == DialogResult.OK) 
{
    m_pageSettings .PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
}