Query_SpecialReport.cs 23 KB

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