using Aspose.Cells;
using Aspose.Cells.Rendering;
using iWareCommon.Utils;
using System;
using System.Threading;
using WindowsFormsApplication1;
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, double leftMargin, double topMargin)
{
//获取模板
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 = leftMargin;//偏移量
// pageSetup.RightMargin = 0.1;
// pageSetup.BottomMargin = 0.3;
pageSetup.TopMargin = topMargin;
//pageSetup.PaperSize = PaperSizeType.;//设置纸张大小
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;
}
}
}