using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Core.LZMes.Client.UIK.Tools { class PrintUtil { public static bool printExcel(string fileName, string printerName) { object missing = System.Reflection.Missing.Value; try { if (isExcelInstalled()) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(fileName); //excel.Visible = true; Microsoft.Office.Interop.Excel._Worksheet ws = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Worksheets["Sheet1"]; ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape; ws.PrintOut(1, 2, 1, false, printerName, false, false, missing);//"HP LaserJet P1505" workbook.Saved = true; workbook.Close(missing, missing, missing); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(ws); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); } else { ET.Application et = new ET.Application(); ET._Workbook ewb = et.Workbooks.Add(fileName); //et.Visible = true; ET._Worksheet ews = (ET._Worksheet)ewb.Worksheets["Sheet1"]; ews.PageSetup.Orientation = ET.XlPageOrientation.xlLandscape;//"EPSON LQ-1600KIII" ews.PrintOut(1, 1, 1, false, printerName, false, false, missing, false, 1, 1, 0, 0, false, ET.ETPaperTray.etPrinterDefaultBin, false, ET.ETPaperOrder.etPrinterRepeat); ewb.Saved = true; ewb.Close(missing, missing, missing); et.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(ews); System.Runtime.InteropServices.Marshal.ReleaseComObject(et); } return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return false; } } private static bool isExcelInstalled() { Type type = Type.GetTypeFromProgID("Excel.Application"); return type != null; } } }