Query_SOP_CY.cs 20 KB

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