2012-10-04 246 views
6

Visual Studio 2010 C#Windowsフォームアプリケーション+ MySqlを使用しています レポートビューアが100%稼働しています。レポートビューアはデータベースのデータで満たされています。印刷するボタンをクリックすると印刷されますが、クライアントはこのボタンをクリックしたくないので、自動的に印刷します。 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"はありません; 'print'についての.optionsを持たないReportDataSource; –

+0

@alannaidon - あなたのためには、 'ReportDocument'ではなく' ReportViewer'でしょうが、アイデアは同じです。それが動作しない場合は、私の他の答えを試してください。私はそれがあなたの問題に対してより正確だと思う。 – Bobson

1

Crystal Reportの回答がうまくいかない場合は、this pageもお試しください。繰り返しますが、私はそれをテストしておらず、動作するかどうかは確かではありませんが、全く異なるアプローチのように見えます。もしそうでなければ、私は残念なことに助けになることはありません。

+0

これはOk Bobsonです。私はSendKeyで何かを試しました。Keyは、ReportViewerで自動的に作成される "Print"ボタンに押されました。あなたはそれについて何か知っていますか? –

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

とちょっとプレストそれが印刷されます。これをコード内のAメソッドに接続してください(おそらく、レポートの読み込みの後に)、あなたの設定はうまくいきます!

オプション:(テストしていない)

たように、これはあなたが次のことを行うことができ、プリンタを変更するには、デフォルトのプリンタに出力し発見:

私はもはやいずれかを持っていないようのにテストされていません
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

関連する問題