schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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 iWareCC.Common;
using iWareCommon.Utils;
using iWareSql.MyDbContext;
using iWareTestForm.Model;
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
 
namespace autuPrint.printService
{
    public class CreatExcelForPOUpdate
    {
        /// <summary>
        /// //线程锁防止并发
        /// </summary>
        private static Mutex single = new Mutex();
        /// <summary>
        /// 生成打印数据源
        /// </summary>
        /// <param name="img">条形码</param>
        ///   <param name="label">标签参数</param>
        /// <returns>成功返回true</returns>
        public static bool creatEXcel(List<AnalysisNoFinishedPOResult> dataList, ref string realFilePath, ref string readFolderPath, ref string errMsg)
        {
            if (single.WaitOne())//增加线程锁防止双击并发
            {
 
                try
                {
                    //获取模板文件
                    readFolderPath = SystemValueUtil.TEMPORARY_MAILWARNING_DIR;
                    realFilePath = SystemValueUtil.TEMPORARY_MAILWARNING_DIR + @"\SAP和WMS更新PO收货数报告(" + DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ").xlsx";
                    FileInfo copyFile = new FileInfo(SystemValueUtil.SAP和WMS更新PO收货数报告模板);
                    copyFile.CopyTo(realFilePath, true);
                    FileInfo existingFile = new FileInfo(realFilePath);
                    using (ExcelPackage package = new ExcelPackage(existingFile))
                    {
                        //获取模板内容
                        ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];
                        //行,列
                        //注意:行和列都是从1开始,而不是从0开始!!!
 
                        for (int i = 0; i < (dataList.Count); i++)
                        {
                            worksheet.Cells[i + 2, 1].Value = dataList[i].Type;
                            worksheet.Cells[i + 2, 2].Value = dataList[i].WMS_Type;
                            worksheet.Cells[i + 2, 3].Value = dataList[i].OrderNo;
                            worksheet.Cells[i + 2, 4].Value = dataList[i].LineNumber;
                            worksheet.Cells[i + 2, 5].Value = dataList[i].Material;
                            worksheet.Cells[i + 2, 6].Value = dataList[i].MaterialDescription;
 
                            worksheet.Cells[i + 2, 7].Value = dataList[i].Qty;
                            worksheet.Cells[i + 2, 8].Value = dataList[i].HasReciveQty;
                            worksheet.Cells[i + 2, 9].Value = dataList[i].WMS_Qty;
                            worksheet.Cells[i + 2, 10].Value = dataList[i].WMS_HasReciveQty;
                            worksheet.Cells[i + 2, 11].Value = dataList[i].AnalysisResult;
                            worksheet.Cells[i + 2, 12].Value = dataList[i].Remark;
                            worksheet.Cells[i + 2, 13].Value = dataList[i].Remark2;
                        }
 
 
                        package.Save();//保存
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.WriteErrorLog(LogType.SrmTheadService, "类名: creatExcelPrint 方法名: creatEXcel 生成打印文件出错 \r\n  " + ex.ToString(), ex);
                    //logtxt.txtWrite("类名: creatExcelPrint 方法名: creatEXcel 生成打印文件出错 \r\n  " + ex.ToString(), 2);
                    errMsg = ex.Message;
                    return false;
                }
                finally { single.ReleaseMutex(); }//一轮结束
            }
            return true;
        }
 
 
    }
}