Query_SpecialReport.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 DataTable ExcelToDataTable(string filePath, bool isColumnName)
  101. {
  102. DataTable dataTable = null;
  103. FileStream fs = null;
  104. DataColumn column = null;
  105. DataRow dataRow = null;
  106. IWorkbook workbook = null;
  107. ISheet sheet = null;
  108. IRow row = null;
  109. ICell cell = null;
  110. int startRow = 0;
  111. try
  112. {
  113. using (fs = File.OpenRead(filePath))
  114. {
  115. // 2007版本
  116. workbook = new HSSFWorkbook(fs);
  117. if (workbook != null)
  118. {
  119. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  120. dataTable = new DataTable();
  121. if (sheet != null)
  122. {
  123. int rowCount = sheet.LastRowNum;//总行数
  124. if (rowCount > 0)
  125. {
  126. IRow firstRow = sheet.GetRow(0);//第一行
  127. int cellCount = firstRow.LastCellNum;//列数
  128. //构建datatable的列
  129. if (isColumnName)
  130. {
  131. startRow = 1;//如果第一行是列名,则从第二行开始读取
  132. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  133. {
  134. cell = firstRow.GetCell(i);
  135. if (cell != null)
  136. {
  137. if (cell.StringCellValue != null)
  138. {
  139. column = new DataColumn(cell.StringCellValue);
  140. dataTable.Columns.Add(column);
  141. }
  142. }
  143. }
  144. }
  145. else
  146. {
  147. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  148. {
  149. column = new DataColumn("column" + (i + 1));
  150. dataTable.Columns.Add(column);
  151. }
  152. }
  153. //填充行
  154. for (int i = startRow; i <= rowCount; ++i)
  155. {
  156. row = sheet.GetRow(i);
  157. if (row == null) continue;
  158. dataRow = dataTable.NewRow();
  159. for (int j = row.FirstCellNum; j < cellCount; ++j)
  160. {
  161. cell = row.GetCell(j);
  162. if (cell == null)
  163. {
  164. dataRow[j] = "";
  165. }
  166. else
  167. {
  168. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  169. switch (cell.CellType)
  170. {
  171. case CellType.BLANK:
  172. dataRow[j] = "";
  173. break;
  174. case CellType.NUMERIC:
  175. short format = cell.CellStyle.DataFormat;
  176. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  177. if (format == 14 || format == 31 || format == 57 || format == 58)
  178. dataRow[j] = cell.DateCellValue;
  179. else
  180. dataRow[j] = cell.NumericCellValue;
  181. break;
  182. case CellType.STRING:
  183. dataRow[j] = cell.StringCellValue;
  184. break;
  185. case CellType.FORMULA:
  186. dataRow[j] = cell.StringCellValue;
  187. break;
  188. }
  189. }
  190. }
  191. dataTable.Rows.Add(dataRow);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. return dataTable;
  198. }
  199. catch (Exception ex)
  200. {
  201. Console.WriteLine(ex.Message);
  202. if (fs != null)
  203. {
  204. fs.Close();
  205. }
  206. return null;
  207. }
  208. }
  209. }
  210. }