UploadMakePlan.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Text;
  6. using NPOI.SS.UserModel;
  7. using NPOI.XSSF.UserModel;
  8. using Oracle.ManagedDataAccess.Client;
  9. namespace FileWatcher
  10. {
  11. public partial class UploadMakePlan : Form
  12. {
  13. string connectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.3.7)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  14. StringBuilder sql = new StringBuilder();
  15. DataHelper dh;
  16. string imaster;
  17. public UploadMakePlan()
  18. {
  19. InitializeComponent();
  20. StartPosition = FormStartPosition.CenterScreen;
  21. }
  22. string fieltype = "";
  23. string ftpulr = "";
  24. private void Form1_Load(object sender, EventArgs e)
  25. {
  26. dh = new DataHelper(connectionString);
  27. }
  28. private void Upload_Click(object sender, EventArgs e)
  29. {
  30. DateTime today = DateTime.Value;
  31. string todayDate = today.ToString("MMdd");
  32. string code = dh.GetSerialNumberByCaller("MakePlan");
  33. string id = "";
  34. DataTable dt = (DataTable)dh.ExecuteSql("select mp_id from MakePlan where to_char(mp_begintime,'yyyymmdd')='" + today.ToString("yyyyMMdd") + "'", "select");
  35. if (dt.Rows.Count == 0)
  36. {
  37. id = dh.GetSEQ("MakePlan_seq");
  38. dh.ExecuteSql("insert into MakePlan(MP_ID, MP_CODE, MP_KIND, MP_INDATE, MP_STATUS, MP_STATUSCODE, MP_BEGINTIME)" +
  39. "values(" + id + ",'" + code + "','日计划',sysdate,'在录入','ENTERING',to_date('" + today.ToString("yyyyMMdd") + "','yyyymmdd'))", "insert");
  40. }
  41. else
  42. {
  43. id = dt.Rows[0]["mp_id"].ToString();
  44. dh.ExecuteSql("delete from MakePlandetail where mpd_mpid=" + id, "delete");
  45. }
  46. using (FileStream file = new FileStream(FilePath.Text, FileMode.Open, FileAccess.Read))
  47. {
  48. XSSFWorkbook workbook = new XSSFWorkbook(file);
  49. int detno = 1;
  50. for (int i = 0; i < workbook.NumberOfSheets; i++)
  51. {
  52. ISheet sheet = workbook.GetSheetAt(i);
  53. string sheetName = sheet.SheetName;
  54. if (sheetName.Contains(todayDate))
  55. {
  56. OperateResult.AppendText(sheetName);
  57. OperateResult.AppendText($"Processing sheet: {sheetName}\n");
  58. //Console.WriteLine($"Processing sheet: {sheetName}");
  59. // 获取标题行(第4行)
  60. IRow headerRow = sheet.GetRow(3); // 第4行索引为3
  61. if (headerRow == null)
  62. {
  63. Console.WriteLine("Header row not found.");
  64. continue;
  65. }
  66. // 获取标题列的索引
  67. int wccodeIndex = -1;
  68. int orderdetnoIndex = -1;
  69. int ordercodeIndex = -1;
  70. int planqtyIndex = -1;
  71. int remarkIndex = -1;
  72. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  73. {
  74. ICell cell = headerRow.GetCell(colIndex);
  75. if (cell != null)
  76. {
  77. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  78. if (headerValue == "工作中心")
  79. {
  80. wccodeIndex = colIndex;
  81. }
  82. else if (headerValue == "订单序号")
  83. {
  84. orderdetnoIndex = colIndex;
  85. }
  86. else if (headerValue == "订单编号")
  87. {
  88. ordercodeIndex = colIndex;
  89. }
  90. else if (headerValue == "计划数量")
  91. {
  92. planqtyIndex = colIndex;
  93. }
  94. else if (headerValue.Contains("备注") &&!headerValue .Contains( "生产异常备注"))
  95. {
  96. remarkIndex = colIndex;
  97. }
  98. }
  99. }
  100. if (wccodeIndex == -1 || orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1)
  101. {
  102. Console.WriteLine("Required columns not found in the header.");
  103. continue;
  104. }
  105. using (OracleConnection conn = new OracleConnection(connectionString))
  106. {
  107. conn.Open();
  108. for (int rowIndex = 4; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  109. {
  110. IRow row = sheet.GetRow(rowIndex);
  111. if (row != null)
  112. {
  113. string wccode = row.GetCell(wccodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue.ToString();
  114. if (wccode == "")
  115. {
  116. continue;
  117. }
  118. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  119. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  120. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  121. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  122. if (!string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  123. {
  124. string insertQuery = "INSERT INTO MakePlanDetail (mpd_mpid,mpd_detno,mpd_id,mpd_wccode, mpd_orderdetno, mpd_ordercode,mpd_outqty,mpd_remark) VALUES (" + id + "," + detno + ",MakePlanDetail_seq.nextval,:1, :2, :3,:4,:5)";
  125. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  126. {
  127. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  128. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  129. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  130. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  131. cmd.Parameters.Add(new OracleParameter(":5", remark));
  132. cmd.ExecuteNonQuery();
  133. }
  134. }
  135. }
  136. detno = detno + 1;
  137. }
  138. }
  139. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  140. OperateResult.AppendText("计划上传成功");
  141. }
  142. }
  143. }
  144. }
  145. private void ChooseFile_Click(object sender, EventArgs e)
  146. {
  147. DialogResult result;
  148. result = ImportExcel1.ShowDialog();
  149. if (result == DialogResult.OK)
  150. {
  151. FilePath.Text = ImportExcel1.FileName;
  152. }
  153. }
  154. }
  155. }