2012-10-04 164 views
6

我正在使用Visual Studio 2010 C#Windows窗體應用程序+ MySql 我有一個工作100%的報告查看器。 reportviewer充滿了我的數據庫的數據,它顯示出我點擊按鈕打印和打印...但是,我的客戶端不想點擊這個按鈕,他想自動打印。當我調用ReportViewer時,它自己打印,無需點擊按鈕即可完成此操作。有誰能告訴我我是怎麼做到的?打印ReportViewer沒有預覽

我嘗試了reportviewer1.print和來自工具箱的PrintDocument。但我不知道如何正確使用這些。

感謝關注!

+0

這是使用CrystalReports?這是我知道的唯一報告查看器,但這並不意味着它是唯一的報告查看器。 – Bobson

+0

不,它的ReportViewer(只是工具箱中的報告)你能給我一個鏡頭,告訴我怎麼做?也許它沒有那麼不同=) –

回答

0

這就是我們如何用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(),我不,我簡單介紹一下看看。

+0

真的非常感謝你回答我Bobson =) 我現在就試試,我不想讓PrintDialog自動打印,我現在就嘗試並儘快給出反饋。 我沒有「ReportDocument」;唯一的ReportDataSource沒有.options關於'print'; s –

+0

@alannaidon - 對於你來說,它會是'ReportViewer'而不是'ReportDocument',但這個想法是一樣的。如果它不起作用,請嘗試我的其他答案。我懷疑你的問題更準確。 – Bobson

1

如果我的Crystal Report答案不適合您,您還可以嘗試this page。再一次,我沒有測試它,並且不能確定它是否有效,但它看起來像是一種完全不同的方法,可能有效。如果不是,那麼不幸的是,我不會有任何幫助。

+0

這就好了Bobson。我嘗試了一下SendKey,KeyPressing到按鈕「打印」,這是使用ReportViewer自動創建的。你知道這件事嗎? –

+0

@alannaidon - 我不認爲我曾經使用過SendKey。這聽起來像一個很好的簡單的解決方案,但。隨意添加它作爲一個新的答案,並接受它,如果你得到它的工作。 – Bobson

+0

這裏提供的鏈接工作得很好,完全被封裝在一個類中,所以你不必爲你的代碼添加任何方法,只需要調用這個類。 – MrWuf

7

我只是有同樣的問題,這是我使用的代碼,像一個魅力工作!

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"; 
    } 
} 

}

此類具有建立完善爲你所需要的,那麼所有你需要做的是:

private void AutoPrint() 
    { 
     AutoPrintCls autoprintme = new AutoPrintCls(reportViewer1.LocalReport); 
     autoprintme.Print(); 
    } 

和變戲法它打印。只需將它附加到代碼中的一個方法(也許在報告加載之後),並且您的設置很好!

選項:(未測試)

由於看準了這打印出到默認打印機,以更改打印機,你可以做到以下幾點:

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

但正如我不再有任何未測試源代碼來測試這個出

+0

嗨,可以從printdialog的代碼中選擇打印機? – Kate

+0

是的,我相信使用printdialog會適合你的需要,看看http://msdn.microsoft.com/en-us/library/cfkddyc2(v=vs.110).aspx – lemunk

+0

是的,但我不知道我怎麼可以給你的代碼printdialog ...我嘗試m_pageSettings.PrinterSettings.PrinterName = PrintDialog1.PrinterSettings.PrinterName; ...但仍然默認打印機... – Kate