333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Drawing.Printing;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using System.IO;
using WIP_common;
using WIP_DAL;
using System.Collections.Specialized;
using Newtonsoft.Json;
 
namespace WIP_Print
{
    public static class PrintHelper
    {
        public static string appPath;
        public static Dictionary<string, string> printDevKeyDic;
        public static string defaultPrint;
        public static PrintDocument fPrintDocument = new PrintDocument();
        public static string connStr;
 
        /// <summary>
        /// 打印Excel
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="printName"></param>
        ///  <param name="orientation">打印方向,默认纵向</param>
        public static void PrintExcel(byte[] bs, string printName, PageOrientationType orientation)
        {
            string printArea = "";//默认
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream(bs);
                Workbook workbook = new Workbook(ms);
                Worksheet worksheet = workbook.Worksheets[0];
                //打印设置
                PageSetup pageSetup = worksheet.PageSetup;
                pageSetup.CenterHorizontally = true;//水平居中
                pageSetup.CenterVertically = false;//不需要垂直居中
                if (printArea != "")
                {
                    //pageSetup.PrintArea = "A1:I22";
                }
 
                pageSetup.Orientation =orientation;
                // pageSetup.Orientation = PageOrientationType.Landscape;//横向
                // pageSetup.Orientation = PageOrientationType.Portrait;//纵向
 
                ImageOrPrintOptions options = new ImageOrPrintOptions();
                System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
 
                defaultPrint = fPrintDocument.PrinterSettings.PrinterName;
                //defaultPrint = @"\\prtsvr\MP 3054 PCL 6 jszx";
                //new LogHelper().AddLog("默认打印机名称是:"+defaultPrint);
 
                SheetRender sr = new SheetRender(worksheet, options);
                if (printName != "")
                {
                    sr.ToPrinter(printName);
                }
                else
                {
                    sr.ToPrinter(defaultPrint);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
 
                GC.Collect(); //垃圾回收机制
                if (ms != null)
                    ms.Close();
            }
        }
    }
}