Query_SpecialReport.cs 20 KB

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