Query_SpecialReport.cs 23 KB

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