using Aspose.Cells; using Aspose.Cells.Rendering; using iWareCommon.Utils; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace autuPrint.printService { public static class print { private static Mutex single = new Mutex(); static Form1 form = Program.f; /// /// 打印指定文件 /// /// 要打印的路径 /// 成功返回true public static bool printExcel(string path) { //获取模板 if (single.WaitOne())//增加线程锁防止双击并发 { try { Workbook workbook = new Workbook(path); /// Cells cells = worksheet.Cells;//获取所有单元格 Worksheet worksheet = workbook.Worksheets[0];//获取该Excel文档的第一个工作表 PageSetup pageSetup = worksheet.PageSetup; pageSetup.Orientation = PageOrientationType.Portrait;//纵向打印 pageSetup.LeftMargin = form.textBox1.Text.Trim() == "" ? 1 : Convert.ToDouble(form.textBox1.Text.Trim());//偏移量 // pageSetup.RightMargin = 0.1; // pageSetup.BottomMargin = 0.3; pageSetup.TopMargin = form.textBox2.Text.Trim() == "" ? 1 : Convert.ToDouble(form.textBox2.Text.Trim()); ; // pageSetup.PaperSize = PaperSizeType.Paper10x11;//设置纸张大小 Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions(); SheetRender sr = new SheetRender(worksheet, options); //找到默认打印机 System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings(); string strPrinterName = printSettings.PrinterName; sr.ToPrinter(strPrinterName);//开始打印 } catch (Exception ex) { Log4NetHelper.WriteErrorLog(LogType.Print,"类名: print 方法名: printExcel 打印文件出错 \r\n " + ex.ToString(), ex); return false; } finally { single.ReleaseMutex(); }//一轮结束 } return true; } } }