Query_SOP_CY.cs 21 KB

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