using LabelManager2; using System; using System.Data; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicForm; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Query { public partial class Query_SN : Form { DataHelper dh = SystemInf.dh; ApplicationClass lbl; Thread InitPrint; Document doc; public Query_SN() { InitializeComponent(); } private void Export_Click(object sender, EventArgs e) { ExcelExport(""); } //选择导出Excel时是选择导出数据的还是模板 private void ExcelExport(string DataOrTemplet) { //Data表示导出数据 //Templet表示导出模板 MD5 md5 = MD5.Create(); folderBrowserDialog1.Description = "选择导出的路径"; DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { string FolderPath = folderBrowserDialog1.SelectedPath; ExcelHandler eh = new ExcelHandler(); DataTable dt = new DataTable(); dt.Columns.Add("工单号"); dt.Columns.Add("序列号"); dt.Columns.Add("MAC"); dt.Columns.Add("BT"); dt.Columns.Add("拓展栏位1"); dt.Columns.Add("拓展栏位2"); dt.Columns.Add("拓展栏位3"); dt.Columns.Add("备注"); string prefix = BeginMac.Text.Substring(0, 8); string suffix = BeginMac.Text.Substring(8, 4); int Number = Int32.Parse(suffix, System.Globalization.NumberStyles.HexNumber); for (decimal i = currentSerial.Value; i < currentSerial.Value + int.Parse(num.Text); i++) { DataRow dr = dt.NewRow(); dr["工单号"] = ma_code.Text; dr["MAC"] = prefix + Number.ToString("X"); Number = Number + 1; dr["BT"] = prefix + Number.ToString("X"); Number = Number + 1; string serial = pr_code.Text + color.Text + GetDate(date.Value.ToString("yyyy"), date.Value.ToString("MM"), date.Value.ToString("dd")) + factory.Text + status.Text + lpad(int.Parse(seriallength.Text), (i + 1).ToString()); string encode = MD5Encoding(serial + ")G$d"); dr["序列号"] = serial + encode.Substring(encode.Length - 2); dt.Rows.Add(dr); } eh.ExportExcel(dt, FolderPath); } } private void Query_SpecialReport_Load(object sender, EventArgs e) { InitPrint = new Thread(InPrint); SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序"); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.lab"); } private void InPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(); } catch (Exception) { MessageBox.Show("未正确安装CodeSoft软件"); } } private static string MD5Encoding(string rawPass) { // 创建MD5类的默认实例:MD5CryptoServiceProvider MD5 md5 = MD5.Create(); byte[] bs = Encoding.UTF8.GetBytes(rawPass); byte[] hs = md5.ComputeHash(bs); StringBuilder stb = new StringBuilder(); foreach (byte b in hs) { // 以十六进制格式格式化 stb.Append(b.ToString("x2")); } return stb.ToString().ToUpper(); } private static string lpad(int length, string number) { while (number.Length < length) { number = "0" + number; } number = number.Substring(number.Length - length, length); return number; } private string GetDate(string year, string month, string day) { string returndate = ""; switch (year) { case "2020": returndate = "C"; break; case "2021": returndate = "D"; break; case "2022": returndate = "E"; break; case "2023": returndate = "F"; break; case "2024": returndate = "G"; break; case "2025": returndate = "H"; break; case "2026": returndate = "I"; break; case "2027": returndate = "J"; break; case "2028": returndate = "K"; break; case "2029": returndate = "L"; break; case "2030": returndate = "M"; break; case "2031": returndate = "N"; break; case "2032": returndate = "O"; break; case "2033": returndate = "P"; break; case "2034": returndate = "Q"; break; default: break; } switch (month) { case "01": returndate += "1"; break; case "02": returndate += "2"; break; case "03": returndate += "3"; break; case "04": returndate += "4"; break; case "05": returndate += "5"; break; case "06": returndate += "6"; break; case "07": returndate += "7"; break; case "08": returndate += "8"; break; case "09": returndate += "9"; break; case "10": returndate += "A"; break; case "11": returndate += "B"; break; case "12": returndate += "C"; break; default: break; } switch (day) { case "01": returndate += "1"; break; case "02": returndate += "2"; break; case "03": returndate += "3"; break; case "04": returndate += "4"; break; case "05": returndate += "5"; break; case "06": returndate += "6"; break; case "07": returndate += "7"; break; case "08": returndate += "8"; break; case "09": returndate += "9"; break; case "10": returndate += "A"; break; case "11": returndate += "B"; break; case "12": returndate += "C"; break; case "13": returndate += "D"; break; case "14": returndate += "E"; break; case "15": returndate += "F"; break; case "16": returndate += "G"; break; case "17": returndate += "H"; break; case "18": returndate += "I"; break; case "19": returndate += "J"; break; case "20": returndate += "K"; break; case "21": returndate += "L"; break; case "22": returndate += "M"; break; case "23": returndate += "N"; break; case "24": returndate += "O"; break; case "25": returndate += "P"; break; case "26": returndate += "Q"; break; case "27": returndate += "R"; break; case "28": returndate += "S"; break; case "29": returndate += "T"; break; case "30": returndate += "U"; break; case "31": returndate += "V"; break; default: break; } return returndate; } private void Print_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("工单号"); dt.Columns.Add("序列号"); dt.Columns.Add("MAC"); dt.Columns.Add("BT"); dt.Columns.Add("拓展栏位1"); dt.Columns.Add("拓展栏位2"); dt.Columns.Add("拓展栏位3"); dt.Columns.Add("备注"); string prefix = BeginMac.Text.Substring(0, 8); string suffix = BeginMac.Text.Substring(8, 4); int Number = Int32.Parse(suffix, System.Globalization.NumberStyles.HexNumber); for (decimal i = currentSerial.Value; i < currentSerial.Value + int.Parse(num.Text); i++) { DataRow dr = dt.NewRow(); dr["工单号"] = ma_code.Text; dr["MAC"] = prefix + Number.ToString("X"); Number = Number + 1; dr["BT"] = prefix + Number.ToString("X"); Number = Number + 1; string serial = pr_code.Text + color.Text + GetDate(date.Value.ToString("yyyy"), date.Value.ToString("MM"), date.Value.ToString("dd")) + factory.Text + status.Text + lpad(int.Parse(seriallength.Text), (i + 1).ToString()); string encode = MD5Encoding(serial + ")G$d"); dr["序列号"] = serial + encode.Substring(encode.Length - 2); dt.Rows.Add(dr); for (int j = 0; j < doc.Variables.FormVariables.Count; j++) { if (doc.Variables.FormVariables.Item(j + 1).Name.ToUpper() == "SN") { doc.Variables.FormVariables.Item(j + 1).Value = (serial + encode.Substring(encode.Length - 2)); } } doc.Printer.SwitchTo(Printer.Text); doc.PrintDocument(1); } } } }