Query_SpecialReport.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using DevExpress.Printing.Core.PdfExport.Metafile;
  2. using DevExpress.XtraCharts.Native;
  3. using ExcelHelper;
  4. using LabelManager2;
  5. using NPOI.HSSF.UserModel;
  6. using NPOI.SS.Formula.Functions;
  7. using NPOI.SS.UserModel;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using UAS_MES_NEW.DataOperate;
  16. using UAS_MES_NEW.Entity;
  17. using UAS_MES_NEW.PublicForm;
  18. using UAS_MES_NEW.PublicMethod;
  19. namespace UAS_MES_NEW.Query
  20. {
  21. public partial class Query_SpecialReport : Form
  22. {
  23. DataHelper dh = SystemInf.dh;
  24. Thread InitPrint;
  25. public Query_SpecialReport()
  26. {
  27. InitializeComponent();
  28. }
  29. private void Query_SpecialReport_Load(object sender, EventArgs e)
  30. {
  31. pr_code.TableName = "product";
  32. pr_code.SelectField = "pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 产品规格";
  33. pr_code.FormName = Name;
  34. pr_code.DBTitle = "物料查询";
  35. pr_code.SetValueField = new string[] { "pr_code" };
  36. }
  37. private static string lpad(int length, string number)
  38. {
  39. while (number.Length < length)
  40. {
  41. number = "0" + number;
  42. }
  43. number = number.Substring(number.Length - length, length);
  44. return number;
  45. }
  46. private void inoutno_TextChanged(object sender, EventArgs e)
  47. {
  48. }
  49. DataTable importdata;
  50. private void XY_Click(object sender, EventArgs e)
  51. {
  52. ImportExcel1.Filter = "(*.xls)|*.xls";
  53. DialogResult result;
  54. result = ImportExcel1.ShowDialog();
  55. if (result == DialogResult.OK)
  56. {
  57. XYFilePath.Text = ImportExcel1.FileName;
  58. importdata = ExcelToDataTable(ImportExcel1.FileName, true);
  59. ExportFileDialog.Description = "选择导出的路径";
  60. DialogResult result1 = ExportFileDialog.ShowDialog();
  61. if (result1 == DialogResult.OK)
  62. {
  63. InitPrint = new Thread(ExportData);
  64. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "导出贴片机数据");
  65. BaseUtil.SetFormCenter(stw);
  66. stw.ShowDialog();
  67. }
  68. }
  69. }
  70. ExcelHandler excel = new ExcelHandler();
  71. private void ExportData()
  72. {
  73. try
  74. {
  75. string time = System.DateTime.Now.ToString("yyyyMMddhhmmss");
  76. Stream fs = new FileStream(ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".txt", FileMode.CreateNew, FileAccess.ReadWrite);
  77. fs.Dispose();
  78. List<string> list = new List<string>();
  79. for (int i = importdata.Rows.Count - 1; i >= 0; i--)
  80. {
  81. string Refer = importdata.Rows[i]["Refer"].ToString();
  82. DataTable bom = (DataTable)dh.ExecuteSql("select bd_soncode,replace(wm_concat(distinct bd_location),',',' ')||';'||replace(wm_concat(distinct bd_soncode),',',' ')||' '||replace(wm_concat(distinct PRE_REPCODE),',',' ') from BOMDetail " +
  83. "LEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code left join ProdReplace on pre_bdid =bd_id where " +
  84. "bo_mothercode='" + pr_code.Text + "' and instr(','||bd_location||',','," + Refer + ",')>0 group by bd_soncode", "select");
  85. if (bom.Rows.Count > 0)
  86. {
  87. if (bom.Rows[0][1].ToString() != "")
  88. {
  89. if (!list.Contains(bom.Rows[0][1].ToString()))
  90. {
  91. list.Add(bom.Rows[0][1].ToString());
  92. }
  93. }
  94. importdata.Rows[i][1] = bom.Rows[0][0].ToString();
  95. }
  96. else
  97. {
  98. importdata.Rows.RemoveAt(i);
  99. }
  100. Process.Text = (i + 1) + "/" + importdata.Rows.Count;
  101. }
  102. excel.ExportExcel(importdata, ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".xls");
  103. StreamWriter sw = File.AppendText(ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".txt");
  104. for (int i = 0; i < list.Count; i++)
  105. {
  106. Console.WriteLine(list[i]);
  107. sw.WriteLine(list[i]);
  108. }
  109. sw.Close();
  110. }
  111. catch (Exception ex)
  112. {
  113. MessageBox.Show(ex.Message);
  114. }
  115. }
  116. public static bool OpenCSVFile(ref DataTable mycsvdt, string filepath)
  117. {
  118. string strpath = filepath; //csv文件的路径
  119.             try
  120. {
  121. int intColCount = 0;
  122. bool blnFlag = true;
  123. DataColumn mydc;
  124. DataRow mydr;
  125. string strline;
  126. string[] aryline;
  127. StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.UTF8);
  128. while ((strline = mysr.ReadLine()) != null)
  129. {
  130. aryline = strline.Split(new char[] { ',' });
  131. //给datatable加上列名
  132. if (blnFlag)
  133. {
  134. blnFlag = false;
  135. intColCount = aryline.Length;
  136. int col = 0;
  137. for (int i = 0; i < aryline.Length; i++)
  138. {
  139. col = i + 1;
  140. mydc = new DataColumn(col.ToString());
  141. mycsvdt.Columns.Add(mydc);
  142. }
  143. }
  144. //填充数据并加入到datatable中
  145. mydr = mycsvdt.NewRow();
  146. for (int i = 0; i < aryline.Length; i++)
  147. {
  148. mydr[i] = aryline[i];
  149. }
  150. mycsvdt.Rows.Add(mydr);
  151. }
  152. mysr.Close();
  153. return true;
  154. }
  155. catch (Exception ex)
  156. {
  157. Console.WriteLine(ex.Message + ex.StackTrace);
  158. return false;
  159. }
  160. }
  161. private void TraceFile_Click(object sender, EventArgs e)
  162. {
  163. ImportExcel1.Filter = "(*.xls)|*.xls";
  164. DialogResult result;
  165. result = ImportExcel1.ShowDialog();
  166. if (result == DialogResult.OK)
  167. {
  168. TraceFilePath.Text = ImportExcel1.FileName;
  169. DataTable dt = ExcelToDataTable1(ImportExcel1.FileName, false);
  170. List<string> STF_MODEL = new List<string>();
  171. List<string> STF_FEEDER = new List<string>();
  172. List<string> STF_FEEDERNO = new List<string>();
  173. List<string> STF_LOCATION = new List<string>();
  174. List<string> STF_MACHINE = new List<string>();
  175. string stf_code = dt.Rows[0][9].ToString();
  176. for (int i = 0; i < dt.Rows.Count; i++)
  177. {
  178. STF_MODEL.Add(dt.Rows[i][0].ToString());
  179. STF_FEEDER.Add(dt.Rows[i][2].ToString());
  180. STF_FEEDERNO.Add(dt.Rows[i][10].ToString());
  181. STF_LOCATION.Add(dt.Rows[i][12].ToString());
  182. STF_MACHINE.Add(dt.Rows[i][1].ToString());
  183. }
  184. dh.ExecuteSql("delete from SMTTRACEFILE where stf_code='" + stf_code + "'", "delete");
  185. dh.BatchInsert("insert into SMTTRACEFILE (STF_ID,stf_prodcode, STF_MODEL, STF_FEEDER, STF_FEEDERNO, STF_LOCATION,STF_MACHINE, STF_CODE, STF_INDATE, STF_INMAN,custname,spec,bomversion,workdate,softname,filecode,releasedate,tableab,ecn,machinename,mademan)" +
  186. "values(SMTTRACEFILE_SEQ.nextval,'" + pr_code.Text + "',:STF_MODEL,:STF_FEEDER,:STF_FEEDERNO,:STF_LOCATION,:STF_MACHINE,'" + stf_code + "',sysdate,'" + User.UserName + "'," +
  187. "'" + custname.Text + "','" + spec.Text + "','" + bomversion.Text + "',to_date('" + workdate.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'),'" + softname.Text + "','" + filecode.Text + "'," +
  188. "to_date('" + releasedate.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'),'" + tableab.Text + "','" + ecn.Text + "','" + machinename.Text + "','" + mademan.Text + "')",
  189. new string[] { "STF_MODEL", "STF_FEEDER", "STF_FEEDERNO", "STF_LOCATION", "STF_MACHINE" }, STF_MODEL.ToArray(), STF_FEEDER.ToArray(), STF_FEEDERNO.ToArray(), STF_LOCATION.ToArray(), STF_MACHINE.ToArray());
  190. dh.ExecuteSql("delete from SMTTRACEFILE where ( STF_MODEL is null and STF_FEEDER is null and STF_FEEDERNO is null and STF_MACHINE is null ) or stf_model in ('料槽','数量','印刷板') or STF_MACHINE in('贴片生产线:','设置:','***') ", "delete");
  191. dataGridView1.DataSource = dt;
  192. //DataTable dt1 = (DataTable)dh.ExecuteSql("select STF_ID, STF_MODEL,STF_MACHINE, STF_FEEDER, STF_FEEDERNO, STF_LOCATION,replace(pr_orispeccode,' ','\n')pr_orispeccode,bo_mothercode,bd_baseqty,bd_soncode,bd_location, STF_CODE, STF_INDATE, STF_INMAN,sumqty from SMTTRACEFILE left join (select bo_mothercode,bd_baseqty,bd_soncode,replace(bd_location,',',' ')bd_location from BOMDetail LEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code where bo_mothercode='" + pr_code.Text + "' ) on bo_mothercode=stf_prodcode and bd_soncode=STF_LOCATION left join product on pr_code=STF_LOCATION " +
  193. // "left join (select sum(bd_baseqty)-1 sumqty,bo_mothercode mothercode from BOMDetail LEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code where bo_mothercode='" + pr_code.Text + "' group by bo_mothercode)on mothercode=bo_mothercode where stf_prodcode='" + pr_code.Text + "' order by stf_id", "select");
  194. DataTable dt1 = (DataTable)dh.ExecuteSql("select * from (select STF_ID, STF_MODEL,STF_MACHINE, STF_FEEDER, STF_FEEDERNO, STF_LOCATION,replace(pr_orispeccode,' ','\n')pr_orispeccode, bo_mothercode,to_char(bd_baseqty)bd_baseqty,bd_soncode," +
  195. "bd_location, STF_CODE, STF_INDATE, STF_INMAN,sumqty from SMTTRACEFILE left join (select bo_mothercode,bd_baseqty,bd_soncode,replace(bd_location,',',' ')bd_location from BOMDetail " +
  196. "LEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code where bo_mothercode='" + pr_code.Text + "') on bo_mothercode=stf_prodcode and bd_soncode=STF_LOCATION left join product on " +
  197. "pr_code=STF_LOCATION left join (select sum(bd_baseqty)-1 sumqty,bo_mothercode mothercode from BOMDetail LEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code where " +
  198. "bo_mothercode='" + pr_code.Text + "' group by bo_mothercode)on mothercode=bo_mothercode where stf_prodcode='" + pr_code.Text + "' union select max(stf_id),'',max(STF_MACHINE),''," +
  199. "'代',max(pr_code),replace(max(pr_orispeccode),' ','\n')pr_orispeccode, max(bo_mothercode),'',pr_code,'',max(STF_CODE),max(STF_INDATE),max(stf_inman),0 from " +
  200. " SMTTRACEFILE left join (select bd_repcode,bo_mothercode,bd_baseqty,bd_soncode,replace(bd_location,',',' ')bd_location from BOMDetail LEFT JOIN bom on bd_bomid=bo_id where " +
  201. "bo_mothercode='" + pr_code.Text + "' and bd_repcode is not null) on bo_mothercode=stf_prodcode and bd_soncode=STF_LOCATION left join product on instr(bd_repcode,pr_code)>0 where " +
  202. "stf_prodcode='" + pr_code.Text + "' and bd_repcode is not null group by stf_id,bd_repcode,pr_code order by stf_id) ", "select");
  203. ExportFileDialog.Description = "选择导出的路径";
  204. DialogResult result1 = ExportFileDialog.ShowDialog();
  205. if (result1 == DialogResult.OK)
  206. {
  207. excel.ExportExcel_LIANGAN(dt1, ExportFileDialog.SelectedPath + "\\" + pr_code.Text + "_" + System.DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss") + ".xls", custname.Text, spec.Text, bomversion.Text, workdate.Text, softname.Text, filecode.Text, releasedate.Text, tableab.Text, ecn.Text, machinename.Text, mademan.Text);
  208. }
  209. }
  210. }
  211. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  212. {
  213. DataTable dataTable = null;
  214. FileStream fs = null;
  215. DataColumn column = null;
  216. DataRow dataRow = null;
  217. IWorkbook workbook = null;
  218. ISheet sheet = null;
  219. IRow row = null;
  220. ICell cell = null;
  221. int startRow = 0;
  222. try
  223. {
  224. using (fs = File.OpenRead(filePath))
  225. {
  226. // 2007版本
  227. workbook = new HSSFWorkbook(fs);
  228. if (workbook != null)
  229. {
  230. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  231. dataTable = new DataTable();
  232. if (sheet != null)
  233. {
  234. int rowCount = sheet.LastRowNum;//总行数
  235. if (rowCount > 0)
  236. {
  237. IRow firstRow = sheet.GetRow(0);//第一行
  238. int cellCount = firstRow.LastCellNum;//列数
  239. //构建datatable的列
  240. if (isColumnName)
  241. {
  242. startRow = 1;//如果第一行是列名,则从第二行开始读取
  243. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  244. {
  245. cell = firstRow.GetCell(i);
  246. if (cell != null)
  247. {
  248. if (cell.StringCellValue != null)
  249. {
  250. column = new DataColumn(cell.StringCellValue);
  251. dataTable.Columns.Add(column);
  252. }
  253. }
  254. }
  255. }
  256. else
  257. {
  258. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  259. {
  260. column = new DataColumn("column" + (i + 1));
  261. dataTable.Columns.Add(column);
  262. }
  263. }
  264. //填充行
  265. for (int i = startRow; i <= rowCount; ++i)
  266. {
  267. row = sheet.GetRow(i);
  268. if (row == null) continue;
  269. dataRow = dataTable.NewRow();
  270. for (int j = row.FirstCellNum; j < cellCount; ++j)
  271. {
  272. cell = row.GetCell(j);
  273. if (cell == null)
  274. {
  275. dataRow[j] = "";
  276. }
  277. else
  278. {
  279. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  280. switch (cell.CellType)
  281. {
  282. case CellType.BLANK:
  283. dataRow[j] = "";
  284. break;
  285. case CellType.NUMERIC:
  286. short format = cell.CellStyle.DataFormat;
  287. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  288. if (format == 14 || format == 31 || format == 57 || format == 58)
  289. dataRow[j] = cell.DateCellValue;
  290. else
  291. dataRow[j] = cell.NumericCellValue;
  292. break;
  293. case CellType.STRING:
  294. dataRow[j] = cell.StringCellValue;
  295. break;
  296. case CellType.FORMULA:
  297. dataRow[j] = cell.StringCellValue;
  298. break;
  299. }
  300. }
  301. }
  302. dataTable.Rows.Add(dataRow);
  303. }
  304. }
  305. }
  306. }
  307. }
  308. return dataTable;
  309. }
  310. catch (Exception ex)
  311. {
  312. Console.WriteLine(ex.Message);
  313. if (fs != null)
  314. {
  315. fs.Close();
  316. }
  317. return null;
  318. }
  319. }
  320. public static DataTable ExcelToDataTable1(string filePath, bool isColumnName)
  321. {
  322. DataTable dataTable = null;
  323. FileStream fs = null;
  324. DataColumn column = null;
  325. DataRow dataRow = null;
  326. IWorkbook workbook = null;
  327. ISheet sheet = null;
  328. IRow row = null;
  329. ICell cell = null;
  330. int startRow = 0;
  331. try
  332. {
  333. using (fs = File.OpenRead(filePath))
  334. {
  335. // 2007版本
  336. workbook = new HSSFWorkbook(fs);
  337. if (workbook != null)
  338. {
  339. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  340. dataTable = new DataTable();
  341. if (sheet != null)
  342. {
  343. int rowCount = sheet.LastRowNum;//总行数
  344. if (rowCount > 0)
  345. {
  346. IRow firstRow = sheet.GetRow(1);//第一行
  347. int cellCount = firstRow.LastCellNum;//列数
  348. //构建datatable的列
  349. if (isColumnName)
  350. {
  351. startRow = 1;//如果第一行是列名,则从第二行开始读取
  352. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  353. {
  354. cell = firstRow.GetCell(i);
  355. if (cell != null)
  356. {
  357. if (cell.StringCellValue != null)
  358. {
  359. column = new DataColumn(cell.StringCellValue);
  360. dataTable.Columns.Add(column);
  361. }
  362. }
  363. }
  364. }
  365. else
  366. {
  367. startRow = 1;
  368. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  369. {
  370. column = new DataColumn("column" + (i + 1));
  371. dataTable.Columns.Add(column);
  372. }
  373. }
  374. //填充行
  375. for (int i = startRow; i <= rowCount; ++i)
  376. {
  377. row = sheet.GetRow(i);
  378. Console.WriteLine(i);
  379. if (row == null) continue;
  380. dataRow = dataTable.NewRow();
  381. for (int j = 0; j < 40; ++j)
  382. {
  383. cell = row.GetCell(j);
  384. if (cell == null)
  385. {
  386. dataRow[j] = "";
  387. }
  388. else
  389. {
  390. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  391. switch (cell.CellType)
  392. {
  393. case CellType.BLANK:
  394. dataRow[j] = "";
  395. break;
  396. case CellType.NUMERIC:
  397. short format = cell.CellStyle.DataFormat;
  398. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  399. if (format == 14 || format == 31 || format == 57 || format == 58)
  400. dataRow[j] = cell.DateCellValue;
  401. else
  402. dataRow[j] = cell.NumericCellValue;
  403. break;
  404. case CellType.STRING:
  405. dataRow[j] = cell.StringCellValue;
  406. break;
  407. case CellType.FORMULA:
  408. dataRow[j] = cell.StringCellValue;
  409. break;
  410. }
  411. }
  412. }
  413. dataTable.Rows.Add(dataRow);
  414. }
  415. }
  416. }
  417. }
  418. }
  419. return dataTable;
  420. }
  421. catch (Exception ex)
  422. {
  423. Console.WriteLine(ex.Message);
  424. if (fs != null)
  425. {
  426. fs.Close();
  427. }
  428. return null;
  429. }
  430. }
  431. }
  432. }