Query_SN.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using LabelManager2;
  2. using System;
  3. using System.Data;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using UAS_MES_NEW.DataOperate;
  9. using UAS_MES_NEW.Entity;
  10. using UAS_MES_NEW.PublicForm;
  11. using UAS_MES_NEW.PublicMethod;
  12. namespace UAS_MES_NEW.Query
  13. {
  14. public partial class Query_SN : Form
  15. {
  16. DataHelper dh = SystemInf.dh;
  17. ApplicationClass lbl;
  18. Thread InitPrint;
  19. Document doc;
  20. public Query_SN()
  21. {
  22. InitializeComponent();
  23. }
  24. private void Export_Click(object sender, EventArgs e)
  25. {
  26. ExcelExport("");
  27. }
  28. //选择导出Excel时是选择导出数据的还是模板
  29. private void ExcelExport(string DataOrTemplet)
  30. {
  31. //Data表示导出数据
  32. //Templet表示导出模板
  33. MD5 md5 = MD5.Create();
  34. folderBrowserDialog1.Description = "选择导出的路径";
  35. DialogResult result = folderBrowserDialog1.ShowDialog();
  36. if (result == DialogResult.OK)
  37. {
  38. string FolderPath = folderBrowserDialog1.SelectedPath;
  39. ExcelHandler eh = new ExcelHandler();
  40. DataTable dt = new DataTable();
  41. dt.Columns.Add("工单号");
  42. dt.Columns.Add("序列号");
  43. dt.Columns.Add("MAC");
  44. dt.Columns.Add("BT");
  45. dt.Columns.Add("拓展栏位1");
  46. dt.Columns.Add("拓展栏位2");
  47. dt.Columns.Add("拓展栏位3");
  48. dt.Columns.Add("备注");
  49. for (decimal i = currentSerial.Value; i < currentSerial.Value + int.Parse(num.Text); i++)
  50. {
  51. DataRow dr = dt.NewRow();
  52. dr["工单号"] = ma_code.Text;
  53. string serial = pr_code.Text
  54. + GetDate(date.Value.ToString("yyyy"), date.Value.ToString("MM"), date.Value.ToString("dd"))
  55. + factory.Text
  56. + status.Text
  57. + lpad(int.Parse(seriallength.Text), (i + 1).ToString());
  58. //string encode = MD5Encoding(serial + ")G$d");
  59. dr["序列号"] = serial;
  60. dt.Rows.Add(dr);
  61. }
  62. eh.ExportExcel(dt, FolderPath);
  63. }
  64. }
  65. private void Query_SpecialReport_Load(object sender, EventArgs e)
  66. {
  67. InitPrint = new Thread(InPrint);
  68. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  69. BaseUtil.SetFormCenter(stw);
  70. stw.ShowDialog();
  71. doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.lab");
  72. }
  73. private void InPrint()
  74. {
  75. try
  76. {
  77. lbl = new ApplicationClass();
  78. BaseUtil.WriteLbl();
  79. }
  80. catch (Exception)
  81. {
  82. MessageBox.Show("未正确安装CodeSoft软件");
  83. }
  84. }
  85. private static string MD5Encoding(string rawPass)
  86. {
  87. // 创建MD5类的默认实例:MD5CryptoServiceProvider
  88. MD5 md5 = MD5.Create();
  89. byte[] bs = Encoding.UTF8.GetBytes(rawPass);
  90. byte[] hs = md5.ComputeHash(bs);
  91. StringBuilder stb = new StringBuilder();
  92. foreach (byte b in hs)
  93. {
  94. // 以十六进制格式格式化
  95. stb.Append(b.ToString("x2"));
  96. }
  97. return stb.ToString().ToUpper();
  98. }
  99. private static string lpad(int length, string number)
  100. {
  101. while (number.Length < length)
  102. {
  103. number = "0" + number;
  104. }
  105. number = number.Substring(number.Length - length, length);
  106. return number;
  107. }
  108. private string GetDate(string year, string month, string day)
  109. {
  110. string returndate = "";
  111. switch (year)
  112. {
  113. case "2020":
  114. returndate = "C";
  115. break;
  116. case "2021":
  117. returndate = "D";
  118. break;
  119. case "2022":
  120. returndate = "E";
  121. break;
  122. case "2023":
  123. returndate = "F";
  124. break;
  125. case "2024":
  126. returndate = "G";
  127. break;
  128. case "2025":
  129. returndate = "H";
  130. break;
  131. case "2026":
  132. returndate = "I";
  133. break;
  134. case "2027":
  135. returndate = "J";
  136. break;
  137. case "2028":
  138. returndate = "K";
  139. break;
  140. case "2029":
  141. returndate = "L";
  142. break;
  143. case "2030":
  144. returndate = "M";
  145. break;
  146. case "2031":
  147. returndate = "N";
  148. break;
  149. case "2032":
  150. returndate = "O";
  151. break;
  152. case "2033":
  153. returndate = "P";
  154. break;
  155. case "2034":
  156. returndate = "Q";
  157. break;
  158. default:
  159. break;
  160. }
  161. switch (month)
  162. {
  163. case "01":
  164. returndate += "1";
  165. break;
  166. case "02":
  167. returndate += "2";
  168. break;
  169. case "03":
  170. returndate += "3";
  171. break;
  172. case "04":
  173. returndate += "4";
  174. break;
  175. case "05":
  176. returndate += "5";
  177. break;
  178. case "06":
  179. returndate += "6";
  180. break;
  181. case "07":
  182. returndate += "7";
  183. break;
  184. case "08":
  185. returndate += "8";
  186. break;
  187. case "09":
  188. returndate += "9";
  189. break;
  190. case "10":
  191. returndate += "A";
  192. break;
  193. case "11":
  194. returndate += "B";
  195. break;
  196. case "12":
  197. returndate += "C";
  198. break;
  199. default:
  200. break;
  201. }
  202. switch (day)
  203. {
  204. case "01":
  205. returndate += "1";
  206. break;
  207. case "02":
  208. returndate += "2";
  209. break;
  210. case "03":
  211. returndate += "3";
  212. break;
  213. case "04":
  214. returndate += "4";
  215. break;
  216. case "05":
  217. returndate += "5";
  218. break;
  219. case "06":
  220. returndate += "6";
  221. break;
  222. case "07":
  223. returndate += "7";
  224. break;
  225. case "08":
  226. returndate += "8";
  227. break;
  228. case "09":
  229. returndate += "9";
  230. break;
  231. case "10":
  232. returndate += "A";
  233. break;
  234. case "11":
  235. returndate += "B";
  236. break;
  237. case "12":
  238. returndate += "C";
  239. break;
  240. case "13":
  241. returndate += "D";
  242. break;
  243. case "14":
  244. returndate += "E";
  245. break;
  246. case "15":
  247. returndate += "F";
  248. break;
  249. case "16":
  250. returndate += "G";
  251. break;
  252. case "17":
  253. returndate += "H";
  254. break;
  255. case "18":
  256. returndate += "I";
  257. break;
  258. case "19":
  259. returndate += "J";
  260. break;
  261. case "20":
  262. returndate += "K";
  263. break;
  264. case "21":
  265. returndate += "L";
  266. break;
  267. case "22":
  268. returndate += "M";
  269. break;
  270. case "23":
  271. returndate += "N";
  272. break;
  273. case "24":
  274. returndate += "O";
  275. break;
  276. case "25":
  277. returndate += "P";
  278. break;
  279. case "26":
  280. returndate += "Q";
  281. break;
  282. case "27":
  283. returndate += "R";
  284. break;
  285. case "28":
  286. returndate += "S";
  287. break;
  288. case "29":
  289. returndate += "T";
  290. break;
  291. case "30":
  292. returndate += "U";
  293. break;
  294. case "31":
  295. returndate += "V";
  296. break;
  297. default:
  298. break;
  299. }
  300. return returndate;
  301. }
  302. private void Print_Click(object sender, EventArgs e)
  303. {
  304. DataTable dt = new DataTable();
  305. dt.Columns.Add("工单号");
  306. dt.Columns.Add("序列号");
  307. dt.Columns.Add("MAC");
  308. dt.Columns.Add("BT");
  309. dt.Columns.Add("拓展栏位1");
  310. dt.Columns.Add("拓展栏位2");
  311. dt.Columns.Add("拓展栏位3");
  312. dt.Columns.Add("备注");
  313. for (decimal i = currentSerial.Value; i < currentSerial.Value + int.Parse(num.Text); i++)
  314. {
  315. DataRow dr = dt.NewRow();
  316. dr["工单号"] = ma_code.Text;
  317. string serial = pr_code.Text
  318. + GetDate(date.Value.ToString("yyyy"), date.Value.ToString("MM"), date.Value.ToString("dd"))
  319. + factory.Text
  320. + status.Text
  321. + lpad(int.Parse(seriallength.Text), (i + 1).ToString());
  322. dr["序列号"] = serial;
  323. dt.Rows.Add(dr);
  324. for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
  325. {
  326. if (doc.Variables.FormVariables.Item(j + 1).Name.ToUpper() == "SN")
  327. {
  328. doc.Variables.FormVariables.Item(j + 1).Value = (serial);
  329. }
  330. }
  331. doc.Printer.SwitchTo(Printer.Text);
  332. doc.PrintDocument(1);
  333. }
  334. }
  335. }
  336. }