Query_SpecialReport.cs 21 KB

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