PrintUtil.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Core.LZMes.Client.UIK.Tools
  6. {
  7. class PrintUtil
  8. {
  9. public static bool printExcel(string fileName, string printerName)
  10. {
  11. object missing = System.Reflection.Missing.Value;
  12. try
  13. {
  14. if (isExcelInstalled())
  15. {
  16. Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
  17. Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(fileName);
  18. //excel.Visible = true;
  19. Microsoft.Office.Interop.Excel._Worksheet ws = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Worksheets["Sheet1"];
  20. ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
  21. ws.PrintOut(1, 2, 1, false, printerName, false, false, missing);//"HP LaserJet P1505"
  22. workbook.Saved = true;
  23. workbook.Close(missing, missing, missing);
  24. excel.Quit();
  25. System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
  26. System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
  27. }
  28. else
  29. {
  30. ET.Application et = new ET.Application();
  31. ET._Workbook ewb = et.Workbooks.Add(fileName);
  32. //et.Visible = true;
  33. ET._Worksheet ews = (ET._Worksheet)ewb.Worksheets["Sheet1"];
  34. ews.PageSetup.Orientation = ET.XlPageOrientation.xlLandscape;//"EPSON LQ-1600KIII"
  35. ews.PrintOut(1, 1, 1, false, printerName, false, false, missing, false, 1, 1, 0, 0, false, ET.ETPaperTray.etPrinterDefaultBin, false, ET.ETPaperOrder.etPrinterRepeat);
  36. ewb.Saved = true;
  37. ewb.Close(missing, missing, missing);
  38. et.Quit();
  39. System.Runtime.InteropServices.Marshal.ReleaseComObject(ews);
  40. System.Runtime.InteropServices.Marshal.ReleaseComObject(et);
  41. }
  42. return true;
  43. }
  44. catch (Exception ex)
  45. {
  46. Console.WriteLine(ex.ToString());
  47. return false;
  48. }
  49. }
  50. private static bool isExcelInstalled()
  51. {
  52. Type type = Type.GetTypeFromProgID("Excel.Application");
  53. return type != null;
  54. }
  55. }
  56. }