274 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			274 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
| //using System;
 | |
| //using System.Collections.Generic;
 | |
| //using System.Linq;
 | |
| //using System.Text;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.ComponentModel;
 | |
| using System.Data;
 | |
| using System.Drawing;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Windows.Forms;
 | |
| using Microsoft.Reporting.WinForms;
 | |
| using System.IO;
 | |
| using System.Drawing.Printing;
 | |
| using System.Drawing.Imaging;
 | |
| 
 | |
| namespace DeiNiu.wms.Logical
 | |
| {
 | |
|     public class printUtils
 | |
|     {
 | |
| 
 | |
|        public static string printerName = "";
 | |
| 
 | |
|         private IList<Stream> m_streams;
 | |
|         private int m_currentPageIndex;
 | |
|         DataTable dt = new DataTable();
 | |
|         private string _errormessage = "";
 | |
|         private string m_ReportName = string.Empty;
 | |
| 
 | |
| 
 | |
|         public void print_reportImage(DataTable dt, string report_paths, string[] Parameter, string print_type)
 | |
|         {
 | |
|             LocalReport report = new LocalReport();
 | |
|             report.ReportEmbeddedResource = report_paths;
 | |
|             report.EnableExternalImages = true;
 | |
|             //report.ReportEmbeddedResource = "Bll.Report1.rdlc";
 | |
|             if (Parameter != null)
 | |
|             {
 | |
|                 ReportParameter[] Paramet = new ReportParameter[Parameter.Length / 2];
 | |
|                 for (int i = 0; i < Parameter.Length / 2; i++)
 | |
|                 {
 | |
|                     Paramet[i] = new ReportParameter(Parameter[i * 2], Parameter[i * 2 + 1]);
 | |
|                 }
 | |
|                 if (Paramet.Length == 1)
 | |
|                 {
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0] });
 | |
|                 }
 | |
|                 else if (Paramet.Length == 2)
 | |
|                 {
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1] });
 | |
|                 }
 | |
|                 else if (Paramet.Length == 3)
 | |
|                 {
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2] });
 | |
|                 }
 | |
|                 else if (Paramet.Length == 4)
 | |
|                 {
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3] });
 | |
|                 }
 | |
|                 else if (Paramet.Length == 5)
 | |
|                 {
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3], Paramet[4] });
 | |
|                 }
 | |
|                 else if (Paramet.Length == 6)
 | |
|                 {
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3], Paramet[4], Paramet[5] });
 | |
|                 }
 | |
|             }
 | |
|             report.DataSources.Clear();
 | |
|             report.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt));
 | |
|             PPrint(report, print_type);
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public bool PPrint(LocalReport report,string print_type)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 Export(report, print_type);
 | |
|                 m_currentPageIndex = 0;
 | |
|                 if (!NBPrint())
 | |
|                 {
 | |
|                     return false;
 | |
|                 }
 | |
|                 if (m_streams != null)
 | |
|                 {
 | |
|                     foreach (Stream stream in m_streams)
 | |
|                     {
 | |
|                         stream.Close();
 | |
|                         stream.Dispose();
 | |
|                       
 | |
|                     }
 | |
|                     m_streams = null;
 | |
|                 }
 | |
|                 return true;
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _errormessage = "在打印过程中出现异常!";
 | |
|                 return false;
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public bool NBPrint()
 | |
|         {
 | |
|             if (m_streams == null || m_streams.Count == 0)
 | |
|             {
 | |
|                 _errormessage = "在打印过程中出现异常!";
 | |
|                 return false;
 | |
|             }
 | |
|             PrintDocument printDoc = new PrintDocument();
 | |
|             //printDoc.PrinterSettings.
 | |
|             printDoc.DocumentName = "世纪清鸿测试报表";
 | |
|             //
 | |
|             //const string printerName = @"\\127.0.0.1\ZDesigner GT800 (EPL) (副本 1)";
 | |
|         //    const string printerName = @"\\127.0.0.1\ZDesigner GT800 (EPL) (副本 2)";
 | |
|             //const string printerName = @"\\127.0.0.1\ZDesigner GT800 (EPL) (副本 3)";
 | |
| 
 | |
| 
 | |
|             const string printerName = "ZDesigner GT800 (EPL)"; //@"\\127.0.0.1\Microsoft XPS Document Writer";
 | |
|             // \\127.0.0.1\ZDesigner GT800 (EPL) (副本 1)
 | |
|            // MainForm.code_print_name = "\\\\127.0.0.1\\ZDesigner GT800 (EPL) (副本 6)";
 | |
|             //if (Bll.print_code.printerName != "")
 | |
|             //{
 | |
|             //    printDoc.PrinterSettings.PrinterName = Bll.print_code.printerName;//printerName;//MainForm.code_print_name;
 | |
|             //}
 | |
|             printDoc.PrinterSettings.PrinterName = printerName;
 | |
|             //printDoc.PrinterSettings.PrinterName = @"\\192.168.1.152\Microsoft XPS Document Writer";
 | |
|             //printDoc.pri
 | |
|             if (!printDoc.PrinterSettings.IsValid)
 | |
|             {
 | |
|                 _errormessage = "请添加默认打印机!";
 | |
|                 return false;
 | |
|             }
 | |
| 
 | |
|             printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
 | |
|             printDoc.Print();
 | |
|             return true;
 | |
|         }
 | |
|         public void PrintPage(object sender, PrintPageEventArgs ev)
 | |
|         {
 | |
|             Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
 | |
|             //ev.PageSettings.Landscape = false;
 | |
|             ev.Graphics.DrawImage(pageImage, ev.PageBounds);
 | |
|             m_currentPageIndex++;
 | |
|             ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
 | |
|         }
 | |
|         public void Export(LocalReport report,string print_type)
 | |
|         {
 | |
|             string deviceInfo = "";
 | |
|             if (print_type == "")
 | |
|             {
 | |
|                 deviceInfo =
 | |
|                       "<DeviceInfo>" +
 | |
|                     "  <OutputFormat>EMF</OutputFormat>" +
 | |
|                     "  <PageWidth>21.0cm</PageWidth>" +
 | |
|                     "  <PageHeight>29.7.0cm</PageHeight>" +
 | |
|                     "  <MarginTop>0.5cm</MarginTop>" +
 | |
|                     "  <MarginLeft>0.5cm</MarginLeft>" +
 | |
|                     "  <MarginRight>0.5cm</MarginRight>" +
 | |
|                     "  <MarginBottom>0.5cm</MarginBottom>" +
 | |
|                     "</DeviceInfo>";
 | |
|             }
 | |
|             else if (print_type == "A4")
 | |
|             {
 | |
|                 deviceInfo =
 | |
|                       "<DeviceInfo>" +
 | |
|                     "  <OutputFormat>EMF</OutputFormat>" +
 | |
|                     "  <PageWidth>21.0cm</PageWidth>" +
 | |
|                     "  <PageHeight>29.7.0cm</PageHeight>" +
 | |
|                     "  <MarginTop>0.5cm</MarginTop>" +
 | |
|                     "  <MarginLeft>0.5cm</MarginLeft>" +
 | |
|                     "  <MarginRight>0.5cm</MarginRight>" +
 | |
|                     "  <MarginBottom>0.5cm</MarginBottom>" +
 | |
|                     "</DeviceInfo>";
 | |
|             }
 | |
|             else if (print_type == "code")
 | |
|             {
 | |
|                 deviceInfo =
 | |
|                                      "<DeviceInfo>" +
 | |
|                                    "  <OutputFormat>EMF</OutputFormat>" +
 | |
|                                    "  <PageWidth>10.0cm</PageWidth>" +
 | |
|                                    "  <PageHeight>5.0cm</PageHeight>" +
 | |
|                                    "  <MarginTop>0.0cm</MarginTop>" +
 | |
|                                    "  <MarginLeft>0.0cm</MarginLeft>" +
 | |
|                                    "  <MarginRight>0.0cm</MarginRight>" +
 | |
|                                    "  <MarginBottom>0.0cm</MarginBottom>" +
 | |
|                                    "</DeviceInfo>";
 | |
|             }
 | |
| 
 | |
|             Warning[] warnings;
 | |
|             m_streams = new List<Stream>();
 | |
|             report.Render("Image", deviceInfo, CreateStream, out warnings);
 | |
|             //report.Render("EXCEL", deviceInfo, CreateStream, out warnings);
 | |
|             foreach (Stream stream in m_streams)
 | |
|             {
 | |
|                 stream.Position = 0;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|  
 | |
| 
 | |
|         private Stream CreateStream(string name, string fileNameExtension,
 | |
|       Encoding encoding, string mimeType, bool willSeek)
 | |
|         {
 | |
|             Stream stream = new FileStream(name + "." + fileNameExtension,
 | |
|          FileMode.Create);
 | |
|             try
 | |
|             {
 | |
|               
 | |
|                 m_streams.Add(stream);
 | |
|               
 | |
|             }
 | |
|             catch (Exception)
 | |
|             {
 | |
|                 
 | |
|               
 | |
|             }
 | |
|             return stream;
 | |
|         }
 | |
|         public void print_report(DataTable dt, string report_paths, string[] Parameter,string print_type)
 | |
|         {
 | |
|             LocalReport report = new LocalReport();
 | |
|             report.ReportEmbeddedResource = report_paths;
 | |
|             if (Parameter != null)
 | |
|             {
 | |
|                 ReportParameter[] Paramet = new ReportParameter[Parameter.Length / 2];
 | |
|                 for (int i = 0; i < Parameter.Length / 2; i++)
 | |
|                 {
 | |
|                     Paramet[i] = new ReportParameter(Parameter[i * 2], Parameter[i * 2 + 1]);
 | |
|                 }
 | |
|                 if (Paramet.Length == 1)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0] });
 | |
|                 else if (Paramet.Length == 2)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1] });
 | |
|                 else if (Paramet.Length == 3)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2] });
 | |
|                 else if (Paramet.Length == 4)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3] });
 | |
|                 else if (Paramet.Length == 5)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3], Paramet[4] });
 | |
|                 else if (Paramet.Length == 6)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3], Paramet[4], Paramet[5] });
 | |
|                 else if (Paramet.Length == 7)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0], Paramet[1], Paramet[2], Paramet[3], Paramet[4], Paramet[5], Paramet[6] });
 | |
|                 else if (Paramet.Length == 33)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0],Paramet[1],Paramet[2],Paramet[3],Paramet[4],Paramet[5], 
 | |
|                                                                             Paramet[6],Paramet[7],Paramet[8],Paramet[9],Paramet[10],
 | |
|                                                                             Paramet[11],Paramet[12],Paramet[13],Paramet[14],Paramet[15],
 | |
|                                                                             Paramet[16],Paramet[17],Paramet[18],Paramet[19],Paramet[20],
 | |
|                                                                             Paramet[21],Paramet[22],Paramet[23],Paramet[24],Paramet[25],
 | |
|                                                                             Paramet[26],Paramet[27],Paramet[28],Paramet[29],Paramet[30],Paramet[31],//Paramet[32],
 | |
|                                                                             Paramet[32]});
 | |
|                 //else if (Paramet.Length == 13)
 | |
|                 //    report.SetParameters(new ReportParameter[] { Paramet[0],Paramet[1],Paramet[2],Paramet[3],Paramet[4],Paramet[5], 
 | |
|                 //                                                            Paramet[6],Paramet[7],Paramet[8],Paramet[9],Paramet[10],
 | |
|                 //                                                            Paramet[11],Paramet[12]});
 | |
| 
 | |
|                 else if (Paramet.Length == 10)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0],Paramet[1],Paramet[2],Paramet[3],Paramet[4],Paramet[5], 
 | |
|                                                                             Paramet[6],Paramet[7],Paramet[8],Paramet[9]});
 | |
|                 else if (Paramet.Length == 11)
 | |
|                     report.SetParameters(new ReportParameter[] { Paramet[0],Paramet[1],Paramet[2],Paramet[3],Paramet[4],Paramet[5], 
 | |
|                                                                             Paramet[6],Paramet[7],Paramet[8],Paramet[9],Paramet[10]});
 | |
|             }
 | |
|             report.DataSources.Clear();
 | |
|             report.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt));
 | |
|             PPrint(report,print_type);
 | |
|         }
 | |
|     }
 | |
| }
 |