| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- using System.IO;
- using System.Text;
- using System.Xml;
- namespace PrintSolution
- {
- public class ExcelPrinter : PrinterBase
- {
- private string xmlResourcePath = string.Empty;
- public string XmlResourcePath
- {
- get { return xmlResourcePath; }
- set { xmlResourcePath = value.Contains(":") ? value : AppDomain.CurrentDomain.BaseDirectory + value; }
- }
- private ArrayList paramList = new ArrayList();
- public ArrayList ParamList
- {
- get { return paramList; }
- set { paramList = value; }
- }
- private PictureInfo picInfo = new PictureInfo();
- public PictureInfo PicInfo
- {
- get { return picInfo; }
- set { picInfo = value; }
- }
- /// <summary>
- /// 打印方向设置 default0纵向 1横向
- /// </summary>
- public int Orientation
- {
- get { return base.PageSetup.Orientation; }
- set { base.PageSetup.Orientation = value; }
- }
- /// <summary>
- ///纸张设置
- /// </summary>
- public new PageSetup PageSetup
- {
- get { return base.PageSetup; }
- set { base.PageSetup = value; }
- }
- public ExcelPrinter()
- {
-
- }
- public ExcelPrinter(string xmlResourcePath)
- {
- if (!xmlResourcePath.Contains(":"))
- {
- xmlResourcePath = AppDomain.CurrentDomain.BaseDirectory + xmlResourcePath;
- }
- this.xmlResourcePath = xmlResourcePath;
- }
- public ExcelPrinter(string xmlResourcePath, ArrayList paramList)
- {
- if (!xmlResourcePath.Contains(":"))
- {
- xmlResourcePath = AppDomain.CurrentDomain.BaseDirectory + xmlResourcePath;
- }
- this.xmlResourcePath = xmlResourcePath;
- this.paramList = paramList;
- }
- public void printExcel()
- {
- try
- {
- string excelPath = AppDomain.CurrentDomain.BaseDirectory + "\\tmp.xls";
- if (createTmpFile(this.xmlResourcePath, excelPath))
- {
- System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
- string printerName = printDoc.PrinterSettings.PrinterName;
- //设置自定义纸张
- if (base._PageSetup.IsCustom)
- {
- printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("原料标签",base._PageSetup.Width,base._PageSetup.Length);
- }
- this.printExcel(excelPath, printerName);
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- public void printExcel(string xmlResourceStr)
- {
- try
- {
- string excelPath = AppDomain.CurrentDomain.BaseDirectory + "\\tmp.xls";
- try
- {
- StreamWriter writer = new StreamWriter(excelPath, false, Encoding.UTF8);
- writer.Write(xmlResourceStr);
- writer.Close();
- System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
- string printerName = printDoc.PrinterSettings.PrinterName;
- //设置自定义纸张
- if (base._PageSetup.IsCustom)
- {
- printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("原料标签", base._PageSetup.Width, base._PageSetup.Length);
- }
- this.printExcel(excelPath, printerName);
- }
- catch (Exception e)
- {
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- private bool createTmpFile(string inPath, string outPath)
- {
- bool flag = true;
- StreamReader reader = null;
- StreamWriter writer = null;
- try
- {
- string bufferStr = string.Empty;
- int index = 0;
- int count = paramList.Count;
- if (string.IsNullOrEmpty(inPath))
- {
- return false;
- }
- XmlDocument doc = new XmlDocument();
- doc.Load(inPath);
- foreach (XmlNode n in doc.GetElementsByTagName("Data"))
- {
- if (n.InnerText.Contains(DATE_REPLACE_STR))
- {
- bufferStr = index < count ? paramList[index++].ToString() : "";
- n.InnerText = n.InnerText.Replace(DATE_REPLACE_STR,bufferStr);
- }
- }
- //bufferStr = doc.OuterXml;
- //reader = new StreamReader(inPath, Encoding.UTF8);
- writer = new StreamWriter(outPath, false, Encoding.UTF8);
- writer.Write(doc.OuterXml);
- //while (!string.IsNullOrEmpty(bufferStr = reader.ReadLine()))
- //{
- // while (bufferStr.Contains(">" + DATE_REPLACE_STR + "<"))
- // {
- // bufferStr = bufferStr.Replace(DATE_REPLACE_STR, "");
- // }
- // writer.WriteLine(bufferStr);
- //}
- }
- catch (Exception e)
- {
- flag = false;
- throw e;
- }
- finally
- {
- if (reader != null)
- {
- reader.Close();
- }
- if (writer != null)
- {
- writer.Close();
- }
- }
- return flag;
- }
- protected override void paintPicByExcel(Microsoft.Office.Interop.Excel._Worksheet ws)
- {
- if (!string.IsNullOrEmpty(PicInfo.PicPath))
- {
- Microsoft.Office.Interop.Excel.Pictures pics = (Microsoft.Office.Interop.Excel.Pictures)ws.Pictures(missing);
- Microsoft.Office.Interop.Excel.Picture pic = pics.Insert(picInfo.PicPath, missing);
- pic.Left = picInfo.PicLeft;
- pic.Top = picInfo.PicTop;
- pic.Width = picInfo.PicWidth;
- pic.Height = picInfo.PicHeight;
- }
- }
- protected override void paintPicByEt(ET._Worksheet ws)
- {
- if (!string.IsNullOrEmpty(PicInfo.PicPath))
- {
- ET.Pictures pics = (ET.Pictures)ws.Pictures(missing);
- pics.Insert(picInfo.PicPath, missing);
- }
- }
- }
- }
|