Query_SpecialReport.cs 18 KB

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