ExcelPrinter.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text;
  7. using System.Xml;
  8. namespace PrintSolution
  9. {
  10. public class ExcelPrinter : PrinterBase
  11. {
  12. private string xmlResourcePath = string.Empty;
  13. public string XmlResourcePath
  14. {
  15. get { return xmlResourcePath; }
  16. set { xmlResourcePath = value.Contains(":") ? value : AppDomain.CurrentDomain.BaseDirectory + value; }
  17. }
  18. private ArrayList paramList = new ArrayList();
  19. public ArrayList ParamList
  20. {
  21. get { return paramList; }
  22. set { paramList = value; }
  23. }
  24. private PictureInfo picInfo = new PictureInfo();
  25. public PictureInfo PicInfo
  26. {
  27. get { return picInfo; }
  28. set { picInfo = value; }
  29. }
  30. /// <summary>
  31. /// 打印方向设置 default0纵向 1横向
  32. /// </summary>
  33. public int Orientation
  34. {
  35. get { return base.PageSetup.Orientation; }
  36. set { base.PageSetup.Orientation = value; }
  37. }
  38. /// <summary>
  39. ///纸张设置
  40. /// </summary>
  41. public new PageSetup PageSetup
  42. {
  43. get { return base.PageSetup; }
  44. set { base.PageSetup = value; }
  45. }
  46. public ExcelPrinter()
  47. {
  48. }
  49. public ExcelPrinter(string xmlResourcePath)
  50. {
  51. if (!xmlResourcePath.Contains(":"))
  52. {
  53. xmlResourcePath = AppDomain.CurrentDomain.BaseDirectory + xmlResourcePath;
  54. }
  55. this.xmlResourcePath = xmlResourcePath;
  56. }
  57. public ExcelPrinter(string xmlResourcePath, ArrayList paramList)
  58. {
  59. if (!xmlResourcePath.Contains(":"))
  60. {
  61. xmlResourcePath = AppDomain.CurrentDomain.BaseDirectory + xmlResourcePath;
  62. }
  63. this.xmlResourcePath = xmlResourcePath;
  64. this.paramList = paramList;
  65. }
  66. public void printExcel()
  67. {
  68. try
  69. {
  70. string excelPath = AppDomain.CurrentDomain.BaseDirectory + "\\tmp.xls";
  71. if (createTmpFile(this.xmlResourcePath, excelPath))
  72. {
  73. System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
  74. string printerName = printDoc.PrinterSettings.PrinterName;
  75. //设置自定义纸张
  76. if (base._PageSetup.IsCustom)
  77. {
  78. printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("原料标签",base._PageSetup.Width,base._PageSetup.Length);
  79. }
  80. this.printExcel(excelPath, printerName);
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. throw e;
  86. }
  87. }
  88. public void printExcel(string xmlResourceStr)
  89. {
  90. try
  91. {
  92. string excelPath = AppDomain.CurrentDomain.BaseDirectory + "\\tmp.xls";
  93. try
  94. {
  95. StreamWriter writer = new StreamWriter(excelPath, false, Encoding.UTF8);
  96. writer.Write(xmlResourceStr);
  97. writer.Close();
  98. System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
  99. string printerName = printDoc.PrinterSettings.PrinterName;
  100. //设置自定义纸张
  101. if (base._PageSetup.IsCustom)
  102. {
  103. printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("原料标签", base._PageSetup.Width, base._PageSetup.Length);
  104. }
  105. this.printExcel(excelPath, printerName);
  106. }
  107. catch (Exception e)
  108. {
  109. }
  110. }
  111. catch (Exception e)
  112. {
  113. throw e;
  114. }
  115. }
  116. private bool createTmpFile(string inPath, string outPath)
  117. {
  118. bool flag = true;
  119. StreamReader reader = null;
  120. StreamWriter writer = null;
  121. try
  122. {
  123. string bufferStr = string.Empty;
  124. int index = 0;
  125. int count = paramList.Count;
  126. if (string.IsNullOrEmpty(inPath))
  127. {
  128. return false;
  129. }
  130. XmlDocument doc = new XmlDocument();
  131. doc.Load(inPath);
  132. foreach (XmlNode n in doc.GetElementsByTagName("Data"))
  133. {
  134. if (n.InnerText.Contains(DATE_REPLACE_STR))
  135. {
  136. bufferStr = index < count ? paramList[index++].ToString() : "";
  137. n.InnerText = n.InnerText.Replace(DATE_REPLACE_STR,bufferStr);
  138. }
  139. }
  140. //bufferStr = doc.OuterXml;
  141. //reader = new StreamReader(inPath, Encoding.UTF8);
  142. writer = new StreamWriter(outPath, false, Encoding.UTF8);
  143. writer.Write(doc.OuterXml);
  144. //while (!string.IsNullOrEmpty(bufferStr = reader.ReadLine()))
  145. //{
  146. // while (bufferStr.Contains(">" + DATE_REPLACE_STR + "<"))
  147. // {
  148. // bufferStr = bufferStr.Replace(DATE_REPLACE_STR, "");
  149. // }
  150. // writer.WriteLine(bufferStr);
  151. //}
  152. }
  153. catch (Exception e)
  154. {
  155. flag = false;
  156. throw e;
  157. }
  158. finally
  159. {
  160. if (reader != null)
  161. {
  162. reader.Close();
  163. }
  164. if (writer != null)
  165. {
  166. writer.Close();
  167. }
  168. }
  169. return flag;
  170. }
  171. protected override void paintPicByExcel(Microsoft.Office.Interop.Excel._Worksheet ws)
  172. {
  173. if (!string.IsNullOrEmpty(PicInfo.PicPath))
  174. {
  175. Microsoft.Office.Interop.Excel.Pictures pics = (Microsoft.Office.Interop.Excel.Pictures)ws.Pictures(missing);
  176. Microsoft.Office.Interop.Excel.Picture pic = pics.Insert(picInfo.PicPath, missing);
  177. pic.Left = picInfo.PicLeft;
  178. pic.Top = picInfo.PicTop;
  179. pic.Width = picInfo.PicWidth;
  180. pic.Height = picInfo.PicHeight;
  181. }
  182. }
  183. protected override void paintPicByEt(ET._Worksheet ws)
  184. {
  185. if (!string.IsNullOrEmpty(PicInfo.PicPath))
  186. {
  187. ET.Pictures pics = (ET.Pictures)ws.Pictures(missing);
  188. pics.Insert(picInfo.PicPath, missing);
  189. }
  190. }
  191. }
  192. }