WinForm+VS2005下ReportViewer的自动打印原码
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;
using System.Windows.Forms; using ControlLib;
using System.Drawing.Printing;
using Microsoft.Reporting.WinForms; using System.IO;
using System.Drawing.Imaging;
namespace ReportLib {
public partial class CrystalForm : Form {
private string _reportname = \; private DataSet _datasource = null; private string _autoprint = \; private IList
public CrystalForm(string reportname,DataSet datasource,string p1) {
_reportname = reportname; _datasource = datasource; _autoprint = p1;
InitializeComponent(); }
private void CrystalForm_Load(object sender, EventArgs e) {
this.reportViewer1.LocalReport.ReportEmbeddedResource = _reportname; this.reportViewer1.LocalReport.DataSources.Clear(); if (_datasource != null) {
foreach (DataTable table in _datasource.Tables) {
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(table.TableName, table));
} }
this.reportViewer1.RefreshReport(); if(_autoprint==\) {
PPrint(); } }
#region 报表自打印 public void PPrint() {
try {
LocalReport report = this.reportViewer1.LocalReport; Export(report);
m_currentPageIndex = 0; NBPrint();
if (m_streams != null) {
foreach (Stream stream in m_streams) stream.Close(); m_streams = null; } }
catch (Exception ex) {
MessageBox.Show(\在打印过程中出现异常! \ + ex.Message.ToString()); } }
private void Export(LocalReport report) {
//7.5in 3.66in 0 0 0 0 当前设置为A4纵向 string deviceInfo = \ +
\
m_streams = new List
report.Render(\, deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams) {
stream.Position = 0; } }
private void NBPrint() {
if (m_streams == null || m_streams.Count == 0) return;
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid) {
string msg = String.Format(\, \默认打印机!\); MessageBox.Show(msg, \找不到默认打印机\); return; }
printDoc.PrintPage += new PrintPageEventHandler(PrintPage); printDoc.Print(); this.Close(); }
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) {
string filenameext = DateTime.Now.Year.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); Stream stream = new FileStream(name + \ + fileNameExtension, FileMode.Create); m_streams.Add(stream); return stream; }
private void PrintPage(object sender, PrintPageEventArgs ev) {
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); ev.Graphics.DrawImage(pageImage, ev.PageBounds); m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count); }
#endregion } }
ReportViewer显示 rdlc报表,对应一个dataset.Xsd接受数据集的对应表。 打印部分参考http://blog.csdn.net/jbdren/archive/2010/05/05/5558288.aspx