22
schangxiang@126.com
2024-12-03 90344bc24b7828091281f069e46d802642e7e0d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
 
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;
        /// <summary>
        /// 打印指定文件
        /// </summary>
        /// <param name="path">要打印的路径</param>
 
        /// <returns>成功返回true</returns>
        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;
        }
    }
}