PrinterBase.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace PrintSolution
  6. {
  7. public class PrinterBase
  8. {
  9. protected object missing = System.Reflection.Missing.Value;
  10. protected const string DATE_REPLACE_STR = "?";
  11. protected PageSetup _PageSetup = new PageSetup();//纸张设置
  12. internal PageSetup PageSetup
  13. {
  14. get { return _PageSetup; }
  15. set { _PageSetup = value; }
  16. }
  17. protected bool printExcel(string excelPath, string printerName)
  18. {
  19. bool flag = true;
  20. try
  21. {
  22. if (CheckExcelInstalled.isExcelInstalled())
  23. {
  24. Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
  25. Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(excelPath);
  26. //excel.Visible = true;
  27. Microsoft.Office.Interop.Excel._Worksheet ws = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Worksheets["Sheet1"];
  28. try
  29. {
  30. paintPicByExcel(ws);
  31. //打印方向
  32. if (_PageSetup.Orientation == 1)
  33. {
  34. ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
  35. }
  36. //边距
  37. if (_PageSetup.TopMargin != 0)
  38. ws.PageSetup.TopMargin = _PageSetup.TopMargin;
  39. if (_PageSetup.BottomMargin != 0)
  40. ws.PageSetup.BottomMargin = _PageSetup.BottomMargin;
  41. if (_PageSetup.LeftMargin != 0)
  42. ws.PageSetup.LeftMargin = _PageSetup.LeftMargin;
  43. if (_PageSetup.RightMargin != 0)
  44. ws.PageSetup.RightMargin = _PageSetup.RightMargin;
  45. ws.PrintOut(1, 2, 1, false, printerName, false, false, missing);
  46. workbook.Saved = true;
  47. }
  48. catch (Exception e)
  49. {
  50. flag = false;
  51. }
  52. finally
  53. {
  54. workbook.Close(missing, missing, missing);
  55. excel.Quit();
  56. System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
  57. System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
  58. System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
  59. System.GC.Collect();
  60. System.GC.WaitForPendingFinalizers();
  61. }
  62. }
  63. else
  64. {
  65. ET.Application et = new ET.Application();
  66. ET._Workbook ewb = et.Workbooks.Add(excelPath);
  67. //et.Visible = true;
  68. ET._Worksheet ews = (ET._Worksheet)ewb.Worksheets["Sheet1"];
  69. try
  70. {
  71. paintPicByEt(ews);
  72. if (_PageSetup.Orientation == 1)
  73. {
  74. ews.PageSetup.Orientation = ET.XlPageOrientation.xlLandscape;
  75. }
  76. //边距
  77. if (_PageSetup.TopMargin != 0)
  78. ews.PageSetup.TopMargin = _PageSetup.TopMargin;
  79. if (_PageSetup.BottomMargin != 0)
  80. ews.PageSetup.BottomMargin = _PageSetup.BottomMargin;
  81. if (_PageSetup.LeftMargin != 0)
  82. ews.PageSetup.LeftMargin = _PageSetup.LeftMargin;
  83. if (_PageSetup.RightMargin != 0)
  84. ews.PageSetup.RightMargin = _PageSetup.RightMargin;
  85. ews.PrintOut(1, 1, 1, false, printerName, false, false, missing, false, 1, 1, 0, 0, false, ET.ETPaperTray.etPrinterDefaultBin, false, ET.ETPaperOrder.etPrinterRepeat);
  86. ewb.Saved = true;
  87. }
  88. catch (Exception e)
  89. {
  90. flag = false;
  91. }
  92. finally
  93. {
  94. ewb.Close(missing, missing, missing);
  95. et.Quit();
  96. System.Runtime.InteropServices.Marshal.ReleaseComObject(ews);
  97. System.Runtime.InteropServices.Marshal.ReleaseComObject(ewb);
  98. System.Runtime.InteropServices.Marshal.ReleaseComObject(et);
  99. System.GC.Collect();
  100. System.GC.WaitForPendingFinalizers();
  101. }
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. Console.WriteLine(ex.ToString());
  107. flag = false;
  108. }
  109. return flag;
  110. }
  111. protected virtual void paintPicByExcel(Microsoft.Office.Interop.Excel._Worksheet ws)
  112. {
  113. }
  114. protected virtual void paintPicByEt(ET._Worksheet ws)
  115. {
  116. }
  117. }
  118. }