Form1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace FileAnalysis
  17. {
  18. public partial class Form1 : Form
  19. {
  20. DataHelper dh = new DataHelper();
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25. private void Form1_Load(object sender, EventArgs e)
  26. {
  27. 提示.ShowBalloonTip(30, "提示", "程序启动", ToolTipIcon.Info);
  28. timer1.Interval = 1000 * 60 * 10;
  29. timer1.Tick += Timer1_Tick;
  30. timer1.Start();
  31. Timer1_Tick(sender, e);
  32. }
  33. private void Timer1_Tick(object sender, EventArgs e)
  34. {
  35. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  36. string IPAddress = "";
  37. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  38. {
  39. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  40. IPAddress = IpEntry.AddressList[i].ToString();
  41. }
  42. StringBuilder sql = new StringBuilder();
  43. sql.Clear();
  44. sql.Append("select * from(select chipcode_,to_char(createdate_,'yymmdd')cer_testdate,substr(ip,0,instr(ip,'/')-1)ip,localdatapath_,");
  45. sql.Append("localpath_ from (select A.chipcode_,createdate_,replace(substr(localdatapath_,0,instr(localdatapath_,'$'))");
  46. sql.Append(",'smb://@','') IP,localdatapath_,replace(replace(replace(substr(localdatapath_,instr(localdatapath_,'$')-1),'$',':'),'/','\\'),'\\\\','\\') ");
  47. sql.Append("localpath_ from datacenter$chip@SZSI_P A left join datacenter$mestemp@SZSI_P B on A.CHIPCODE_=B.chipcode_ ");
  48. sql.Append("where nvl(readstatus_,0)=0) order by createdate_ desc) where ip= '" + IPAddress + "' and rownum<60");
  49. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  50. List<string> insertSQL = new List<string>();
  51. for (int i = 0; i < dt.Rows.Count; i++)
  52. {
  53. FileStream fs = null;
  54. IWorkbook workbook = null;
  55. ISheet sheet = null;
  56. string test = dt.Rows[i]["cer_testdate"].ToString();
  57. string chipcode = dt.Rows[i]["chipcode_"].ToString();
  58. string filepath = dt.Rows[i]["localpath_"].ToString();
  59. //string filepath = @"c:\filewatcher\F20036121#07.xls";
  60. try
  61. {
  62. richTextBox1.AppendText(test + " " + dt.Rows[i]["localpath_"].ToString() + "\n");
  63. //如果文件不存在则调用转换
  64. if (!File.Exists(filepath))
  65. {
  66. filepath = filepath.Replace("xls", "jdf");
  67. if (!File.Exists(filepath))
  68. {
  69. filepath = filepath.Replace("jdf", "njdf");
  70. }
  71. if (filepath.Substring(filepath.LastIndexOf(".") + 1) == "jdf" || filepath.Substring(filepath.LastIndexOf(".") + 1) == "njdf")
  72. {
  73. if (!filepath.Contains("RECYCLE"))
  74. {
  75. string FullName = filepath;
  76. string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  77. string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
  78. if (File.Exists(FullName))
  79. {
  80. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + filepath + "\n");
  81. }
  82. else
  83. {
  84. richTextBox1.AppendText("不存在文件" + FullName + "\n");
  85. }
  86. //不存在同名的文件则进行转换
  87. if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
  88. {
  89. if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf")
  90. {
  91. exec(@"DTS-JDFData2Excel.exe", @"DTS-JDFData2Excel.exe " + FullName);
  92. }
  93. else if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "njdf")
  94. {
  95. exec(@"DTS-Data2Excel.exe", @"DTS-Data2Excel.exe " + FullName);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. //如果文件存在
  102. if (File.Exists(filepath))
  103. {
  104. using (fs = File.OpenRead(filepath))
  105. {
  106. workbook = new HSSFWorkbook(fs);
  107. sheet = workbook.GetSheet("Data");
  108. List<string> itemname = new List<string>();
  109. List<string> detno = new List<string>();
  110. List<string> bias1 = new List<string>();
  111. List<string> bias2 = new List<string>();
  112. List<string> bias3 = new List<string>();
  113. List<string> minlimit = new List<string>();
  114. List<string> maxlimit = new List<string>();
  115. List<string> avg = new List<string>();
  116. List<string> stdev = new List<string>();
  117. List<string> mindata = new List<string>();
  118. List<string> maxdata = new List<string>();
  119. for (int k = 1; k < sheet.GetRow(13).PhysicalNumberOfCells; k++)
  120. {
  121. itemname.Add(sheet.GetRow(13).GetCell(k + 1).StringCellValue.Split(' ')[1]);
  122. bias1.Add(sheet.GetRow(14).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  123. bias2.Add(sheet.GetRow(15).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  124. bias3.Add(sheet.GetRow(16).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  125. minlimit.Add(sheet.GetRow(17).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  126. maxlimit.Add(sheet.GetRow(18).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  127. avg.Add(sheet.GetRow(19).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  128. stdev.Add(sheet.GetRow(20).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  129. mindata.Add(sheet.GetRow(21).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  130. maxdata.Add(sheet.GetRow(22).GetCell(k + 1).StringCellValue.Replace(" ", ""));
  131. detno.Add(k.ToString());
  132. }
  133. //解析前面的范围文件
  134. sql.Clear();
  135. sql.Append("insert into DATACENTER$CHIP_DATAMAIN@SZSI_P(CHIPCODE_,DATE_,ITEM_NAME,DETNO,BIAS1,BIAS2,BIAS3,MINLIMIT,MAXLIMIT,AVG_,STDEV,MIN_,MAX_)");
  136. sql.Append("values('" + chipcode + "',sysdate,:ITEM_NAME,:DETNO,:BIAS1,:BIAS2,:BIAS3,:MINLIMIT,:MAXLIMIT,:AVG_,:STDEV,:MIN_,:MAX_)");
  137. dh.BatchInsert(sql.ToString(), new string[] { "itemname", "detno", "bias1", "bias2", "bias3", "minlimit", "maxlimit", "avg", "stdev", "mindata", "maxdata" }, itemname.ToArray(), detno.ToArray(), bias1.ToArray(), bias2.ToArray(), bias3.ToArray(), minlimit.ToArray(), maxlimit.ToArray(), avg.ToArray(), stdev.ToArray(), mindata.ToArray(), maxdata.ToArray());
  138. for (int j = 24; j <= sheet.LastRowNum; j++)
  139. {
  140. string Filed = "";
  141. string Value = "";
  142. sql.Clear();
  143. sql.Append("insert into RECORD$" + test + "(chipcode,detno,");
  144. for (int k = 1; k < sheet.GetRow(13).PhysicalNumberOfCells; k++)
  145. {
  146. string CellValue = sheet.GetRow(j).GetCell(k + 1).StringCellValue.Replace(" ", "");
  147. Filed += "ITEM" + (k) + ",";
  148. Value += "'" + CellValue + "',";
  149. }
  150. sql.Append(Filed.Substring(0, Filed.Length - 1) + ") values('" + chipcode + "'," + (j - 23) + "," + Value.Substring(0, Value.Length - 1) + ");");
  151. insertSQL.Add(sql.ToString());
  152. }
  153. string[] strarr = insertSQL.ToArray();
  154. string sqlcount = "";
  155. for (int j = 0; j < strarr.Length; j++)
  156. {
  157. sqlcount += strarr[j];
  158. if (j > 0 && (j % 1000 == 0 || j == strarr.Length - 1))
  159. {
  160. dh.ExecuteSql("begin " + sqlcount + " end;", "insert");
  161. sqlcount = "";
  162. }
  163. }
  164. insertSQL.Clear();
  165. //更新为失效状态
  166. dh.ExecuteSql("UPDATE CHIPTABLE SET STATUS_=0 WHERE CHIP_CODE='" + chipcode + "'", "insert");
  167. //插入有效
  168. dh.ExecuteSql("INSERT INTO CHIPTABLE (CHIP_CODE,TABLE_NAME,STATUS_) VALUES ('" + chipcode + "','RECORD$" + test + "',-1)", "insert");
  169. dh.ExecuteSql("update datacenter$chip@SZSI_P set readstatus_=-1 where chipcode_='" + chipcode + "'", "update");
  170. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.ff") + chipcode + "\n");
  171. }
  172. }
  173. else
  174. {
  175. dh.ExecuteSql("update datacenter$chip@SZSI_P set readstatus_=-99 where chipcode_='" + chipcode + "'", "update");
  176. dh.ExecuteSql("insert into EDCsyscfaillog@SZSI_P(el_id,el_ip,el_synctime,el_chipcode,el_reason) values(EDCsyscfaillog_seq.nextval@SZSI_P,'" + IPAddress + "',sysdate,'" + chipcode + "','Excel文件不存在,无法解析')", "insert");
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. dh.ExecuteSql("update datacenter$chip@SZSI_P set readstatus_=-99 where chipcode_='" + chipcode + "'", "update");
  182. dh.ExecuteSql("insert into EDCsyscfaillog@SZSI_P(el_id,el_ip,el_synctime,el_chipcode,el_reason) values(EDCsyscfaillog_seq.nextval@SZSI_P,'" + IPAddress + "',sysdate,'" + chipcode + "','" + ex.Message + "')", "insert");
  183. }
  184. }
  185. }
  186. public void exec(string exePath, string parameters)
  187. {
  188. Process process = new Process();
  189. process.StartInfo.FileName = exePath;
  190. process.StartInfo.Arguments = parameters;
  191. process.StartInfo.UseShellExecute = false;
  192. process.StartInfo.CreateNoWindow = true;
  193. process.StartInfo.RedirectStandardOutput = true;
  194. process.Start();
  195. }
  196. private void button2_Click(object sender, EventArgs e)
  197. {
  198. Timer1_Tick(sender, e);
  199. }
  200. }
  201. }