Query_SOP_CY.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Net;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. using System.Data;
  9. using System.Web.Script.Serialization;
  10. using Aspose.Cells.Rendering;
  11. using Aspose.Cells;
  12. using System.Drawing.Imaging;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicMethod;
  16. using UAS_MES_NEW.PublicForm;
  17. using Aspose.Pdf.Devices;
  18. using Aspose.Pdf;
  19. using DevExpress.XtraExport;
  20. namespace UAS_MES_NEW.Query
  21. {
  22. public partial class Query_SOP_CY : Form
  23. {
  24. DataHelper dh = new DataHelper();
  25. AutoSizeFormClass asc = new AutoSizeFormClass();
  26. string usercode;
  27. string sourcecode;
  28. DataTable Dbfind;
  29. public Query_SOP_CY()
  30. {
  31. InitializeComponent();
  32. }
  33. private void Form3_Load(object sender, EventArgs e)
  34. {
  35. asc.controllInitializeSize(this);
  36. CheckForIllegalCrossThreadCalls = false;
  37. ps_prodcode.TableName = "productsop";
  38. ps_prodcode.SelectField = "ps_prodcode # SOP编号";
  39. ps_prodcode.FormName = Name;
  40. ps_prodcode.DBTitle = "SOP查询";
  41. ps_prodcode.SetValueField = new string[] { "ps_prodcode" };
  42. ps_prodcode.Condition = "";
  43. ps_prodcode.DbChange += nr_rule_DBChange;
  44. li_code.TableName = "line";
  45. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  46. li_code.FormName = Name;
  47. li_code.DBTitle = "线别查询";
  48. li_code.SetValueField = new string[] { "li_code" };
  49. li_code.Condition = "";
  50. li_code.DbChange += nr_rule_DBChange;
  51. SystemInf.dh = dh;
  52. }
  53. private void nr_rule_DBChange(object sender, EventArgs e)
  54. {
  55. Dbfind = ps_prodcode.ReturnData;
  56. BaseUtil.SetFormValue(this.Controls, Dbfind);
  57. }
  58. void uploadfile()
  59. {
  60. if (FilePath.Text == "")
  61. {
  62. MessageBox.Show("文件不能为空!");
  63. return;
  64. }
  65. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".xls", "").Replace(".xlsx", "");
  66. pr_code_Leave(new object(), new EventArgs());
  67. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  68. if (dt.Rows.Count == 0)
  69. {
  70. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  71. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserCode + "','在录入','ENTERING')", "insert");
  72. }
  73. else
  74. {
  75. string ps_id = dt.Rows[0]["ps_id"].ToString();
  76. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + ps_id + "'", "delete");
  77. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  78. }
  79. Workbook workbook = new Workbook(FilePath.Text);
  80. workbook.CalculateFormula(true);
  81. List<Worksheet> list = new List<Worksheet>();
  82. List<string> filename = new List<string>();
  83. dh.ExecuteSql("update ProductSOP set ps_attachsop='' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  84. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  85. try
  86. {
  87. for (int i = 0; i < workbook.Worksheets.Count; i++)
  88. {
  89. OperatResult.AppendText("正在解析Sheet" + workbook.Worksheets[i].Name + "\n");
  90. Worksheet she = workbook.Worksheets[i];
  91. if (JPG.Checked)
  92. {
  93. try
  94. {
  95. DataHelper dh = new DataHelper();
  96. if (!Directory.Exists(Application.StartupPath + @"\" + ps_prodcode.Text))
  97. {
  98. Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
  99. }
  100. string sheetname = she.Name;
  101. sheetname = sheetname.Replace("01", "1");
  102. sheetname = sheetname.Replace("02", "2");
  103. sheetname = sheetname.Replace("03", "3");
  104. sheetname = sheetname.Replace("04", "4");
  105. sheetname = sheetname.Replace("05", "5");
  106. sheetname = sheetname.Replace("06", "6");
  107. sheetname = sheetname.Replace("07", "7");
  108. sheetname = sheetname.Replace("08", "8");
  109. sheetname = sheetname.Replace("09", "9");
  110. ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
  111. imgOptions.ImageFormat = ImageFormat.Png;
  112. imgOptions.OnePagePerSheet = true;
  113. imgOptions.CheckWorkbookDefaultFont = false;
  114. imgOptions.DefaultFont = "宋体";
  115. imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
  116. imgOptions.SetDesiredSize(1565, 1043);
  117. SheetRender sr = new SheetRender((Aspose.Cells.Worksheet)she, imgOptions);
  118. sr.ToImage(0, Application.StartupPath + @"\" + ps_prodcode.Text + @"\" + sheetname + ".jpg");
  119. //// 创建一个图片
  120. OperatResult.AppendText("解析图片【" + sheetname + ".jpg】" + "\n");
  121. Dictionary<string, object> dic = new Dictionary<string, object>();
  122. dic.Add("em_name", "管理员");
  123. dic.Add("em_code", "ADMIN");
  124. dic.Add("caller", "ProductSOP");
  125. OperatResult.AppendText("上传文件【" + sheetname + ".jpg" + "】\n");
  126. string fp_id = UploadFilesToRemoteUrl("http://192.168.0.85:8099/mes/MEScommon/uploadFiles.action?_noc=1", Application.StartupPath + @"\" + ps_prodcode.Text + @"\" + sheetname + ".jpg", dic);
  127. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  128. dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + sheetname + ".jpg" + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
  129. }
  130. catch (Exception ex)
  131. {
  132. OperatResult.AppendText(ex.Message + ex.StackTrace);
  133. }
  134. filename.Add(she.Name + ".jpg");
  135. }
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. OperatResult.AppendText(ex.Message + "\n");
  141. }
  142. }
  143. /// <summary>
  144. /// 请求上传图片到阿里云
  145. /// </summary>
  146. /// <param name="url">上传地址</param>
  147. /// <param name="filepath">本地文件路径</param>
  148. /// <param name="dic">上传的数据信息</param>
  149. /// <returns></returns>
  150. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  151. {
  152. try
  153. {
  154. ServicePointManager.DefaultConnectionLimit = 50;
  155. string boundary = DateTime.Now.Ticks.ToString("x");
  156. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  157. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  158. request.Method = "POST";
  159. request.Timeout = 10 * 10000;
  160. request.ContentType = "multipart/form-data; boundary=" + boundary;
  161. Stream rs = request.GetRequestStream();
  162. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  163. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  164. if (dic != null)
  165. {
  166. foreach (string key in dic.Keys)
  167. {
  168. rs.Write(boundarybytes, 0, boundarybytes.Length);
  169. string formitem = string.Format(formdataTemplate, key, dic[key]);
  170. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  171. rs.Write(formitembytes, 0, formitembytes.Length);
  172. }
  173. }
  174. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  175. {
  176. rs.Write(boundarybytes, 0, boundarybytes.Length);
  177. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  178. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  179. rs.Write(headerbytes, 0, headerbytes.Length);
  180. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  181. {
  182. var buffer = new byte[1024];
  183. var bytesRead = 0;
  184. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  185. {
  186. rs.Write(buffer, 0, bytesRead);
  187. }
  188. }
  189. var cr = Encoding.UTF8.GetBytes("\r\n");
  190. rs.Write(cr, 0, cr.Length);
  191. }
  192. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  193. var response = request.GetResponse() as HttpWebResponse;
  194. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  195. string Content = newReader.ReadToEnd();
  196. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  197. List<Dictionary<string, object>> dic2 = null;
  198. dic1 = BaseUtil.ToDictionary(Content);
  199. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  200. string fp_id = "";
  201. if (dic2[0]["filepath"] != null)
  202. {
  203. fp_id = dic2[0]["filepath"].ToString();
  204. }
  205. if (response.StatusCode == HttpStatusCode.OK)
  206. {
  207. return fp_id;
  208. }
  209. }
  210. catch (Exception e)
  211. {
  212. LogicHandler.DoCommandLog("SOP", User.UserCode, "", "", sourcecode, "上传SOP", "上传失败", ps_prodcode.Text, "");
  213. Console.WriteLine(e.Message + e.StackTrace);
  214. }
  215. return "";
  216. }
  217. private void ChooseFile_Click(object sender, EventArgs e)
  218. {
  219. ChoosePath.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx";
  220. DialogResult result = ChoosePath.ShowDialog();
  221. if (result == DialogResult.OK)
  222. {
  223. FilePath.Text = ChoosePath.FileName;
  224. }
  225. }
  226. private void UploadSOP_Click(object sender, EventArgs e)
  227. {
  228. Thread thread = new Thread(uploadfile);
  229. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  230. stw.StartPosition = FormStartPosition.CenterScreen;
  231. stw.ShowDialog();
  232. if (JPG.Checked)
  233. {
  234. LogicHandler.DoCommandLog("SOP", User.UserCode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", ps_prodcode.Text, "");
  235. }
  236. else
  237. {
  238. LogicHandler.DoCommandLog("SOP", User.UserCode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", ps_prodcode.Text, "");
  239. }
  240. }
  241. private void OperatResult_TextChanged(object sender, EventArgs e)
  242. {
  243. //OperatResult.SelectionStart = Text.Length;
  244. OperatResult.ScrollToCaret();
  245. }
  246. private void SendSop_Click(object sender, EventArgs e)
  247. {
  248. try
  249. {
  250. if (li_code.Text == "" || ps_prodcode.Text == "")
  251. {
  252. MessageBox.Show("线别和SOP文档不能为空");
  253. return;
  254. }
  255. if (SendDGV.Rows.Count > 0)
  256. {
  257. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  258. JavaScriptSerializer jss = new JavaScriptSerializer();
  259. Dictionary<string, object> map1 = new Dictionary<string, object>();
  260. Dictionary<string, object> map = new Dictionary<string, object>();
  261. string path;
  262. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  263. for (int i = 0; i < SendDGV.Rows.Count; i++)
  264. {
  265. if (SendDGV.Rows[i].Cells["CheckColumn"].FormattedValue.ToString().ToUpper() == "TRUE")
  266. {
  267. Dictionary<string, string> dic1 = new Dictionary<string, string>();
  268. path = SendDGV.Rows[i].Cells["fp_path"].Value.ToString();
  269. path = encryptBASE64(path.Replace("/app/uas/webapps/postattach", pathroot)).Replace("\\s*|\r|\n|\t", "");
  270. dic1.Add("path", path);
  271. dic1.Add("filename", SendDGV.Rows[i].Cells["fp_name"].Value.ToString().Replace(" ", "").Replace("(", "").Replace(")", ""));
  272. dic1.Add("ps_prodcode", ps_prodcode.Text);
  273. listr.Add(dic1);
  274. dh.ExecuteSql("insert into soprelease(SR_ID, SR_LINECODE, SR_PATH, SR_STATUS, SR_MACHINETYPE, SR_FILENAME,sr_ifkeyset)" +
  275. "values(soprelease_seq.nextval,'" + li_code.Text + "','" + path + "',0,'" + ps_prodcode.Text + "','" + SendDGV.Rows[i].Cells["fp_name"].Value.ToString().Replace(" ", "").Replace("(", "").Replace(")", "") + "','" + SendDGV.Rows[i].Cells["psd_iskey"].Value + "') ", "insert");
  276. }
  277. }
  278. LogicHandler.DoCommandLog("SOP", User.UserCode, "", "", sourcecode, "广播SOP", "广播成功", ps_prodcode.Text, "");
  279. MessageBox.Show("文档[" + ps_prodcode.Text + "]下放成功");
  280. }
  281. else
  282. {
  283. MessageBox.Show("产品编号" + ps_prodcode.Text + "未维护SOP文档");
  284. LogicHandler.DoCommandLog("SOP", User.UserCode, "", "", sourcecode, "广播SOP", "广播失败,未维护SOP文档", ps_prodcode.Text, "");
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. LogicHandler.DoCommandLog("SOP", User.UserCode, "", "", sourcecode, "广播SOP", "广播失败", ps_prodcode.Text, "");
  290. Console.WriteLine(ex.Message + ex.StackTrace);
  291. }
  292. }
  293. public string encryptBASE64(string key)
  294. {
  295. Console.WriteLine(key);
  296. byte[] bytes = Encoding.Default.GetBytes(key);
  297. return Convert.ToBase64String(bytes);
  298. }
  299. public static string EncodeBase64(string code_type, string code)
  300. {
  301. string encode = "";
  302. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  303. try
  304. {
  305. encode = Convert.ToBase64String(bytes);
  306. }
  307. catch
  308. {
  309. encode = code;
  310. }
  311. return encode;
  312. }
  313. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  314. {
  315. if (e.KeyCode == Keys.Enter)
  316. {
  317. LoadPrCode();
  318. }
  319. }
  320. private void LoadPrCode()
  321. {
  322. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "'", "select");
  323. if (dt.Rows.Count > 0)
  324. {
  325. string ps_id = dt.Rows[0]["ps_id"].ToString();
  326. dt = (DataTable)dh.ExecuteSql("select fp_name,fp_path,'' issend,psd_iskey from ProductSOPdetail left join " +
  327. "filepath on FP_ID=substr(PSD_ATTACH,instr(PSD_ATTACH,';')+1) where psd_psid=" + ps_id, "select");
  328. if (dt.Rows.Count > 0)
  329. {
  330. SendDGV.DataSource = dt;
  331. }
  332. }
  333. }
  334. private void pr_code_Leave(object sender, EventArgs e)
  335. {
  336. //LoadPrCode();
  337. }
  338. bool AllChecked = false;
  339. private void ChooseALL_Click(object sender, EventArgs e)
  340. {
  341. if (AllChecked)
  342. {
  343. foreach (DataGridViewRow dr in SendDGV.Rows)
  344. dr.Cells[0].Value = false;
  345. AllChecked = false;
  346. }
  347. else
  348. {
  349. foreach (DataGridViewRow dr in SendDGV.Rows)
  350. dr.Cells[0].Value = true;
  351. AllChecked = true;
  352. }
  353. }
  354. private void ps_prodcode_UserControlTextChanged(object sender, EventArgs e)
  355. {
  356. LoadPrCode();
  357. }
  358. private void GetSOP_Click(object sender, EventArgs e)
  359. {
  360. LoadPrCode();
  361. }
  362. private void button1_Click(object sender, EventArgs e)
  363. {
  364. using (Document pdfDocument = new Document("C:\\Users\\callm\\Desktop\\客户资料\\次元\\SOP测试文件\\EM-959-NM16ASH PVT组装SOP20260512.pdf"))
  365. {
  366. // 2. 设置转换参数
  367. Resolution resolution = new Resolution(300); // 设置输出分辨率为 300 DPI[reference:6]
  368. PngDevice pngDevice = new PngDevice(resolution);
  369. // 可以启用字体提示来提升文本清晰度[reference:7]
  370. // pngDevice.RenderingOptions = new RenderingOptions() { UseFontHinting = true };
  371. // 3. 遍历每一页并进行转换
  372. for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
  373. {
  374. if (!Directory.Exists(Application.StartupPath + @"\" + "111"))
  375. {
  376. Directory.CreateDirectory(Application.StartupPath + @"\" + "111");
  377. }
  378. string imagePath = Path.Combine(Application.StartupPath + @"\" + "111", $"image_{pageCount}.png");
  379. using (FileStream imageStream = new FileStream(imagePath, FileMode.Create))
  380. {
  381. pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);
  382. }
  383. }
  384. }
  385. }
  386. private void button2_Click(object sender, EventArgs e)
  387. {
  388. Workbook workbook = new Workbook("C:\\Users\\callm\\Desktop\\客户资料\\次元\\SOP测试文件\\巴拿马组装SOP--测试.xlsx");
  389. ImageOrPrintOptions options = new ImageOrPrintOptions
  390. {
  391. ImageFormat = ImageFormat.Jpeg,
  392. OnePagePerSheet = true,
  393. HorizontalResolution = 150,
  394. VerticalResolution = 150
  395. };
  396. // 遍历所有工作表
  397. for (int sheetIndex = 0; sheetIndex < workbook.Worksheets.Count; sheetIndex++)
  398. {
  399. Worksheet sheet = workbook.Worksheets[sheetIndex];
  400. SheetRender renderer = new SheetRender(sheet, options);
  401. string fileName = $"sheet_{sheetIndex + 1}.jpg";
  402. renderer.ToImage(0, Application.StartupPath + @"\111\" + fileName + ".png");
  403. Console.WriteLine($"已保存: {fileName}");
  404. }
  405. }
  406. }
  407. }