using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
namespace RDLCReport
{
public partial class PubPrintViewRdlcN : Form
{
public PubPrintViewRdlcN()
{
InitializeComponent();
}
private int m_currentPageIndex;
private IList<Stream> m_streams;
private System.Windows.Forms.BindingSource vBindingSource;//表BindingSource
private DataTable LoadSalesData()
{
SqlConnection conn = new SqlConnection("server ='JOWEN//FLERPDATA';database = 'DB_ZFTERP';uid=zjw;pwd=");
SqlCommand cmd = new SqlCommand("exec [PCB_H010003] '201002'",conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet dataSet = new DataSet();
sda.Fill(dataSet, "salses");
vBindingSource = new BindingSource();
vBindingSource.DataSource = dataSet;
vBindingSource.DataMember = "salses";
vBindingSource.Filter = " 产品类别 = '纸类' ";
DataView view = new DataView(dataSet.Tables[0]);
view.RowFilter = " 产品类别 = '纸类' ";
DataTable newTable = view.ToTable("FilteredTable");
dataSet.ReadXml(@"d:/My Reports/data.xml");
//return dataSet.Tables[0];
return newTable;
}
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
//Stream stream = new FileStream(@"d:/My Reports/" + name +
Stream stream = new FileStream(@"c:/" + name +
"." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report)
{
//pageSetupDialog1.PrinterSettings.ToString();
//MessageBox.Show(pageSetupDialog1.PrinterSettings.ToString());
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>42cm</PageWidth>" +
" <PageHeight>29.7cm</PageHeight>" +
" <MarginTop>0.3cm</MarginTop>" +
" <MarginLeft>0.3cm</MarginLeft>" +
" <MarginRight>0.3cm</MarginRight>" +
" <MarginBottom>0.3cm</MarginBottom>" +
"</DeviceInfo>";
//string deviceInfo =
// "<DeviceInfo>" +
// " <OutputFormat>EMF</OutputFormat>" +
// " <PageWidth>"+pageSetupDialog1.PageSettings.PaperSize.Width*0.01+"in</PageWidth>" +
// " <PageHeight>" + pageSetupDialog1.PageSettings.PaperSize.Height * 0.01 + "in</PageHeight>" +
// " <MarginTop>0.05in</MarginTop>" +
// " <MarginLeft>0.05in</MarginLeft>" +
// " <MarginRight>0.05in</MarginRight>" +
// " <MarginBottom>0.05in</MarginBottom>" +
// "</DeviceInfo>";
//"<DeviceInfo>" +
//" <OutputFormat>EMF</OutputFormat>" +
//" <PageWidth>8.5in</PageWidth>" +
//" <PageHeight>11in</PageHeight>" +
//" <MarginTop>0.25in</MarginTop>" +
//" <MarginLeft>0.25in</MarginLeft>" +
//" <MarginRight>0.25in</MarginRight>" +
//" <MarginBottom>0.25in</MarginBottom>" +
//"</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
//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);
// //if (m_currentPageIndex == m_streams.Count) m_currentPageIndex = 1;
//}
//private void Print()
//{
// //const string printerName = "Microsoft Office Document Image Writer";
// const string printerName = "Samsung ML-2850 PCL6";
// if (m_streams == null || m_streams.Count == 0)
// return;
// PrintDocument printDoc = new PrintDocument();
// //printDoc = new PrintDocument();
// printDoc.PrinterSettings.PrinterName = printerName;
// if (!printDoc.PrinterSettings.IsValid)
// {
// string msg = String.Format("Can't find printer /"{0}/".", printerName);
// MessageBox.Show(msg, "Print Error");
// return;
// }
// printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
// printDialog1.Document = printDoc;
// printDialog1.ShowDialog();
// pageSetupDialog1.Document = printDoc;
// pageSetupDialog1.ShowDialog();
// //printPreviewDialog1.Document = printDoc;
// //printPreviewDialog1.ShowDialog();
// printDoc.Print();
//}
//private void Run()
//{
// LocalReport report = new LocalReport();
// //report = new LocalReport();
// report.ReportPath = @"c:/My Reports/Report.rdlc";
// report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
// Export(report);
// m_currentPageIndex = 0;
// Print();
//}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
if (printDialog1.PrinterSettings.PrintRange == PrintRange.AllPages)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
e.Graphics.DrawImage(pageImage, e.PageBounds);
m_currentPageIndex++;
e.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
else if (printDialog1.PrinterSettings.PrintRange == PrintRange.CurrentPage)
{
Metafile pageImage = new Metafile(m_streams[printPreviewControl1.StartPage]);
e.Graphics.DrawImage(pageImage, e.PageBounds);
m_currentPageIndex = printPreviewControl1.StartPage;
//m_currentPageIndex++;
//e.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
else if (printDialog1.PrinterSettings.PrintRange == PrintRange.SomePages)
{
if (m_currentPageIndex == 0) { m_currentPageIndex = printDialog1.PrinterSettings.FromPage-1; }
if (m_currentPageIndex >= printDialog1.PrinterSettings.FromPage-1 && m_currentPageIndex <= printDialog1.PrinterSettings.ToPage-1)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
e.Graphics.DrawImage(pageImage, e.PageBounds);
m_currentPageIndex++;
if (m_currentPageIndex >= printDialog1.PrinterSettings.FromPage - 1 && m_currentPageIndex <= printDialog1.PrinterSettings.ToPage - 1)
{
e.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
}
}
}
catch (Exception)
{
//throw;
}
}
private void Form1_Load(object sender, EventArgs e)
{
m_currentPageIndex = 0;
LocalReport report = new LocalReport();
report.ReportPath = @"d:/My Reports/Report.rdlc";
report.DataSources.Add(new ReportDataSource("DB_ZFTERPDataSetZJW_H010003", LoadSalesData()));
LoadSalesData();
//vBindingSource.SupportsFiltering = true;
//vBindingSource.Filter = "产品类别 = '纸类' ";
// vBindingSource.Remove("产品类别 = '纸类'");
//vBindingSource.Sort = "产品类别";
//report.DataSources.Add(new ReportDataSource("DB_ZFTERPDataSetZJW_H010003", vBindingSource));
Export(report);
//const string printerName = "Samsung ML-2850 PCL6";
//if (m_streams == null || m_streams.Count == 0)
// return;
//printDocument1.PrinterSettings.PrinterName = printerName;
//if (!printDocument1.PrinterSettings.IsValid)
//{
// string msg = String.Format("Can't find printer /"{0}/".", printerName);
// MessageBox.Show(msg, "Print Error");
// return;
//}
//printDialog1.Document = printDocument1;
//printDialog1.ShowDialog();
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
printPreviewControl1.Document = printDocument1;
printPreviewControl1.Show();
printPreviewControl1.Zoom = 1;
printPreviewControl1.StartPage = 0;
tsl1.Text = "第" + "1" + "页,共" + m_streams.Count.ToString() + "页";
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
m_currentPageIndex = 0;
LocalReport report = new LocalReport();
//report = new LocalReport();
report.ReportPath = @"d:/My Reports/Report.rdlc";
report.DataSources.Add(new ReportDataSource("DB_ZFTERPDataSetZJW_H010003", LoadSalesData()));
Export(report);
//const string printerName = "Samsung ML-2850 PCL6";
//if (m_streams == null || m_streams.Count == 0)
// return;
//printDocument1.PrinterSettings.PrinterName = printerName;
//if (!printDocument1.PrinterSettings.IsValid)
//{
// string msg = String.Format("Can't find printer /"{0}/".", printerName);
// MessageBox.Show(msg, "Print Error");
// return;
//}
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
printDialog1.Document = printDocument1;
//printDialog1.ShowDialog();
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom = 0.25;
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom = 0.5;
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom = 0.75;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom = 1;
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom = 1.25;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom = 1.5;
}
//第一页
private void toolStripButton4_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage = 0;
tsl1.Text = "第" + "1" + "页,共" + m_streams.Count.ToString() + "页";
//printPreviewControl1.Columns = 3;
//printPreviewControl1.Refresh();
}
//最后一页
private void toolStripButton7_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage = m_streams.Count - 1;
tsl1.Text = "第" + m_streams.Count.ToString() + "页,共" + m_streams.Count.ToString() + "页";
printPreviewControl1.Refresh();
}
//前一页
private void toolStripButton5_Click(object sender, EventArgs e)
{
if (printPreviewControl1.StartPage != 0)
{
printPreviewControl1.StartPage = printPreviewControl1.StartPage - 1;
tsl1.Text = "第" + (printPreviewControl1.StartPage + 1).ToString() + "页,共" + m_streams.Count.ToString() + "页";
}
}
//后一页
private void toolStripButton6_Click(object sender, EventArgs e)
{
//if (printPreviewControl1.StartPage != m_currentPageIndex-1)
//{
try
{
printPreviewControl1.StartPage = printPreviewControl1.StartPage + 1;
tsl1.Text = "第" + (printPreviewControl1.StartPage + 1).ToString() + "页,共" + m_streams.Count.ToString() + "页";
}
catch (Exception)
{
//throw;
}
//}
}
private void PubPrintViewRdlcN_FormClosing(object sender, FormClosingEventArgs e)
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.Close();
}
//public void Dispose()
//{
// if (m_streams != null)
// {
// foreach (Stream stream in m_streams)
// stream.Close();
// m_streams = null;
// }
//}
}
}