schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
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
65
66
67
68
69
70
 
 
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;
        /// <summary>
        /// 打印指定文件
        /// </summary>
        /// <param name="path">要打印的路径</param>
      
        /// <returns>成功返回true</returns>
        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;
        }
    }
}