Query_SpecialReport.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. DataView view = new DataView(importdata);
  103. view.Sort = "Column1";
  104. DataTable sortedTable = view.ToTable();
  105. SwapColumns(sortedTable, "Side", "Agree");
  106. excel.ExportExcel(sortedTable, ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".xls");
  107. StreamWriter sw = File.AppendText(ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".txt");
  108. for (int i = 0; i < list.Count; i++)
  109. {
  110. Console.WriteLine(list[i]);
  111. sw.WriteLine(list[i]);
  112. }
  113. sw.Close();
  114. }
  115. catch (Exception ex)
  116. {
  117. MessageBox.Show(ex.Message);
  118. }
  119. }
  120. private static string SwapColumns(DataTable dataTable, string columnName1, string columnName2)
  121. {
  122. if (dataTable.Columns.Contains(columnName1) && dataTable.Columns.Contains(columnName2))
  123. {
  124. int index1 = dataTable.Columns[columnName1].Ordinal;
  125. int index2 = dataTable.Columns[columnName2].Ordinal;
  126. dataTable.Columns[columnName1].SetOrdinal(index2);
  127. dataTable.Columns[columnName2].SetOrdinal(index1);
  128. }
  129. return "";
  130. }
  131. public static bool OpenCSVFile(ref DataTable mycsvdt, string filepath)
  132. {
  133. string strpath = filepath; //csv文件的路径
  134.             try
  135. {
  136. int intColCount = 0;
  137. bool blnFlag = true;
  138. DataColumn mydc;
  139. DataRow mydr;
  140. string strline;
  141. string[] aryline;
  142. StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.UTF8);
  143. while ((strline = mysr.ReadLine()) != null)
  144. {
  145. aryline = strline.Split(new char[] { ',' });
  146. //给datatable加上列名
  147. if (blnFlag)
  148. {
  149. blnFlag = false;
  150. intColCount = aryline.Length;
  151. int col = 0;
  152. for (int i = 0; i < aryline.Length; i++)
  153. {
  154. col = i + 1;
  155. mydc = new DataColumn(col.ToString());
  156. mycsvdt.Columns.Add(mydc);
  157. }
  158. }
  159. //填充数据并加入到datatable中
  160. mydr = mycsvdt.NewRow();
  161. for (int i = 0; i < aryline.Length; i++)
  162. {
  163. mydr[i] = aryline[i];
  164. }
  165. mycsvdt.Rows.Add(mydr);
  166. }
  167. mysr.Close();
  168. return true;
  169. }
  170. catch (Exception ex)
  171. {
  172. Console.WriteLine(ex.Message + ex.StackTrace);
  173. return false;
  174. }
  175. }
  176. private void TraceFile_Click(object sender, EventArgs e)
  177. {
  178. ImportExcel1.Filter = "(*.xls)|*.xls";
  179. DialogResult result;
  180. result = ImportExcel1.ShowDialog();
  181. if (result == DialogResult.OK)
  182. {
  183. TraceFilePath.Text = ImportExcel1.FileName;
  184. DataTable dt = ExcelToDataTable1(ImportExcel1.FileName, false);
  185. List<string> STF_MODEL = new List<string>();
  186. List<string> STF_FEEDER = new List<string>();
  187. List<string> STF_FEEDERNO = new List<string>();
  188. List<string> STF_LOCATION = new List<string>();
  189. List<string> STF_MACHINE = new List<string>();
  190. string stf_code = dt.Rows[0][9].ToString();
  191. for (int i = 0; i < dt.Rows.Count; i++)
  192. {
  193. STF_MODEL.Add(dt.Rows[i][0].ToString());
  194. STF_FEEDER.Add(dt.Rows[i][2].ToString());
  195. STF_FEEDERNO.Add(dt.Rows[i][10].ToString());
  196. STF_LOCATION.Add(dt.Rows[i][12].ToString());
  197. STF_MACHINE.Add(dt.Rows[i][1].ToString());
  198. }
  199. dh.ExecuteSql("delete from SMTTRACEFILE where stf_code='" + stf_code + "'", "delete");
  200. 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)" +
  201. "values(SMTTRACEFILE_SEQ.nextval,'" + pr_code.Text + "',:STF_MODEL,:STF_FEEDER,:STF_FEEDERNO,:STF_LOCATION,:STF_MACHINE,'" + stf_code + "',sysdate,'" + User.UserName + "'," +
  202. "'" + custname.Text + "','" + spec.Text + "','" + bomversion.Text + "',to_date('" + workdate.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'),'" + softname.Text + "','" + filecode.Text + "'," +
  203. "to_date('" + releasedate.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'),'" + tableab.Text + "','" + ecn.Text + "','" + machinename.Text + "','" + mademan.Text + "')",
  204. 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());
  205. 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");
  206. dataGridView1.DataSource = dt;
  207. //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 " +
  208. // "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");
  209. 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," +
  210. "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 " +
  211. "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 " +
  212. "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 " +
  213. "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),''," +
  214. "'代',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 " +
  215. " 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 " +
  216. "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 " +
  217. "stf_prodcode='" + pr_code.Text + "' and bd_repcode is not null group by stf_id,bd_repcode,pr_code order by stf_id) ", "select");
  218. ExportFileDialog.Description = "选择导出的路径";
  219. DialogResult result1 = ExportFileDialog.ShowDialog();
  220. if (result1 == DialogResult.OK)
  221. {
  222. 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);
  223. }
  224. }
  225. }
  226. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  227. {
  228. DataTable dataTable = null;
  229. FileStream fs = null;
  230. DataColumn column = null;
  231. DataRow dataRow = null;
  232. IWorkbook workbook = null;
  233. ISheet sheet = null;
  234. IRow row = null;
  235. ICell cell = null;
  236. int startRow = 0;
  237. try
  238. {
  239. using (fs = File.OpenRead(filePath))
  240. {
  241. // 2007版本
  242. workbook = new HSSFWorkbook(fs);
  243. if (workbook != null)
  244. {
  245. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  246. dataTable = new DataTable();
  247. if (sheet != null)
  248. {
  249. int rowCount = sheet.LastRowNum;//总行数
  250. if (rowCount > 0)
  251. {
  252. IRow firstRow = sheet.GetRow(0);//第一行
  253. int cellCount = firstRow.LastCellNum;//列数
  254. //构建datatable的列
  255. if (isColumnName)
  256. {
  257. startRow = 1;//如果第一行是列名,则从第二行开始读取
  258. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  259. {
  260. cell = firstRow.GetCell(i);
  261. if (cell != null)
  262. {
  263. if (cell.StringCellValue != null)
  264. {
  265. column = new DataColumn(cell.StringCellValue);
  266. dataTable.Columns.Add(column);
  267. }
  268. }
  269. }
  270. }
  271. else
  272. {
  273. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  274. {
  275. column = new DataColumn("column" + (i + 1));
  276. dataTable.Columns.Add(column);
  277. }
  278. }
  279. //填充行
  280. for (int i = startRow; i <= rowCount; ++i)
  281. {
  282. row = sheet.GetRow(i);
  283. if (row == null) continue;
  284. dataRow = dataTable.NewRow();
  285. for (int j = row.FirstCellNum; j < cellCount; ++j)
  286. {
  287. cell = row.GetCell(j);
  288. if (cell == null)
  289. {
  290. dataRow[j] = "";
  291. }
  292. else
  293. {
  294. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  295. switch (cell.CellType)
  296. {
  297. case CellType.BLANK:
  298. dataRow[j] = "";
  299. break;
  300. case CellType.NUMERIC:
  301. short format = cell.CellStyle.DataFormat;
  302. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  303. if (format == 14 || format == 31 || format == 57 || format == 58)
  304. dataRow[j] = cell.DateCellValue;
  305. else
  306. dataRow[j] = cell.NumericCellValue;
  307. break;
  308. case CellType.STRING:
  309. dataRow[j] = cell.StringCellValue;
  310. break;
  311. case CellType.FORMULA:
  312. dataRow[j] = cell.StringCellValue;
  313. break;
  314. }
  315. }
  316. }
  317. dataTable.Rows.Add(dataRow);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. return dataTable;
  324. }
  325. catch (Exception ex)
  326. {
  327. Console.WriteLine(ex.Message);
  328. if (fs != null)
  329. {
  330. fs.Close();
  331. }
  332. return null;
  333. }
  334. }
  335. public static DataTable ExcelToDataTable1(string filePath, bool isColumnName)
  336. {
  337. DataTable dataTable = null;
  338. FileStream fs = null;
  339. DataColumn column = null;
  340. DataRow dataRow = null;
  341. IWorkbook workbook = null;
  342. ISheet sheet = null;
  343. IRow row = null;
  344. ICell cell = null;
  345. int startRow = 0;
  346. try
  347. {
  348. using (fs = File.OpenRead(filePath))
  349. {
  350. // 2007版本
  351. workbook = new HSSFWorkbook(fs);
  352. if (workbook != null)
  353. {
  354. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  355. dataTable = new DataTable();
  356. if (sheet != null)
  357. {
  358. int rowCount = sheet.LastRowNum;//总行数
  359. if (rowCount > 0)
  360. {
  361. IRow firstRow = sheet.GetRow(1);//第一行
  362. int cellCount = firstRow.LastCellNum;//列数
  363. //构建datatable的列
  364. if (isColumnName)
  365. {
  366. startRow = 1;//如果第一行是列名,则从第二行开始读取
  367. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  368. {
  369. cell = firstRow.GetCell(i);
  370. if (cell != null)
  371. {
  372. if (cell.StringCellValue != null)
  373. {
  374. column = new DataColumn(cell.StringCellValue);
  375. dataTable.Columns.Add(column);
  376. }
  377. }
  378. }
  379. }
  380. else
  381. {
  382. startRow = 1;
  383. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  384. {
  385. column = new DataColumn("column" + (i + 1));
  386. dataTable.Columns.Add(column);
  387. }
  388. }
  389. //填充行
  390. for (int i = startRow; i <= rowCount; ++i)
  391. {
  392. row = sheet.GetRow(i);
  393. Console.WriteLine(i);
  394. if (row == null) continue;
  395. dataRow = dataTable.NewRow();
  396. for (int j = 0; j < 40; ++j)
  397. {
  398. cell = row.GetCell(j);
  399. if (cell == null)
  400. {
  401. dataRow[j] = "";
  402. }
  403. else
  404. {
  405. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  406. switch (cell.CellType)
  407. {
  408. case CellType.BLANK:
  409. dataRow[j] = "";
  410. break;
  411. case CellType.NUMERIC:
  412. short format = cell.CellStyle.DataFormat;
  413. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  414. if (format == 14 || format == 31 || format == 57 || format == 58)
  415. dataRow[j] = cell.DateCellValue;
  416. else
  417. dataRow[j] = cell.NumericCellValue;
  418. break;
  419. case CellType.STRING:
  420. dataRow[j] = cell.StringCellValue;
  421. break;
  422. case CellType.FORMULA:
  423. dataRow[j] = cell.StringCellValue;
  424. break;
  425. }
  426. }
  427. }
  428. dataTable.Rows.Add(dataRow);
  429. }
  430. }
  431. }
  432. }
  433. }
  434. return dataTable;
  435. }
  436. catch (Exception ex)
  437. {
  438. Console.WriteLine(ex.Message);
  439. if (fs != null)
  440. {
  441. fs.Close();
  442. }
  443. return null;
  444. }
  445. }
  446. }
  447. }