Query_SpecialReport.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using DevExpress.Printing.Core.PdfExport.Metafile;
  2. using LabelManager2;
  3. using NPOI.HSSF.UserModel;
  4. using NPOI.SS.Formula.Functions;
  5. using NPOI.SS.UserModel;
  6. using System;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using UAS_MES_NEW.DataOperate;
  13. using UAS_MES_NEW.Entity;
  14. using UAS_MES_NEW.PublicForm;
  15. using UAS_MES_NEW.PublicMethod;
  16. namespace UAS_MES_NEW.Query
  17. {
  18. public partial class Query_SpecialReport : Form
  19. {
  20. DataHelper dh = SystemInf.dh;
  21. Thread InitPrint;
  22. public Query_SpecialReport()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Query_SpecialReport_Load(object sender, EventArgs e)
  27. {
  28. pr_code.TableName = "product";
  29. pr_code.SelectField = "pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 产品规格";
  30. pr_code.FormName = Name;
  31. pr_code.DBTitle = "物料查询";
  32. pr_code.SetValueField = new string[] { "pr_code" };
  33. }
  34. private static string lpad(int length, string number)
  35. {
  36. while (number.Length < length)
  37. {
  38. number = "0" + number;
  39. }
  40. number = number.Substring(number.Length - length, length);
  41. return number;
  42. }
  43. private void inoutno_TextChanged(object sender, EventArgs e)
  44. {
  45. }
  46. private void XY_Click(object sender, EventArgs e)
  47. {
  48. ImportExcel1.Filter = "(*.xls)|*.xls";
  49. DialogResult result;
  50. result = ImportExcel1.ShowDialog();
  51. DataTable BOM = (DataTable)dh.ExecuteSql("select * from BOMDetail LEFT JOIN bom on bd_bomid=bo_id left join " +
  52. "Product ON bd_soncode=pr_code where bo_mothercode='" + pr_code + "'", "select");
  53. if (result == DialogResult.OK)
  54. {
  55. XYFilePath.Text = ImportExcel1.FileName;
  56. DataTable dt = ExcelToDataTable(ImportExcel1.FileName, true);
  57. for (int i = 0; i < dt.Rows.Count; i++)
  58. {
  59. for (int j = 0; j < BOM.Rows.Count; j++)
  60. {
  61. string Refer = dt.Rows[i]["Refer"].ToString();
  62. }
  63. }
  64. }
  65. }
  66. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  67. {
  68. DataTable dataTable = null;
  69. FileStream fs = null;
  70. DataColumn column = null;
  71. DataRow dataRow = null;
  72. IWorkbook workbook = null;
  73. ISheet sheet = null;
  74. IRow row = null;
  75. ICell cell = null;
  76. int startRow = 0;
  77. try
  78. {
  79. using (fs = File.OpenRead(filePath))
  80. {
  81. // 2007版本
  82. workbook = new HSSFWorkbook(fs);
  83. if (workbook != null)
  84. {
  85. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  86. dataTable = new DataTable();
  87. if (sheet != null)
  88. {
  89. int rowCount = sheet.LastRowNum;//总行数
  90. if (rowCount > 0)
  91. {
  92. IRow firstRow = sheet.GetRow(0);//第一行
  93. int cellCount = firstRow.LastCellNum;//列数
  94. //构建datatable的列
  95. if (isColumnName)
  96. {
  97. startRow = 1;//如果第一行是列名,则从第二行开始读取
  98. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  99. {
  100. cell = firstRow.GetCell(i);
  101. if (cell != null)
  102. {
  103. if (cell.StringCellValue != null)
  104. {
  105. column = new DataColumn(cell.StringCellValue);
  106. dataTable.Columns.Add(column);
  107. }
  108. }
  109. }
  110. }
  111. else
  112. {
  113. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  114. {
  115. column = new DataColumn("column" + (i + 1));
  116. dataTable.Columns.Add(column);
  117. }
  118. }
  119. //填充行
  120. for (int i = startRow; i <= rowCount; ++i)
  121. {
  122. row = sheet.GetRow(i);
  123. if (row == null) continue;
  124. dataRow = dataTable.NewRow();
  125. for (int j = row.FirstCellNum; j < cellCount; ++j)
  126. {
  127. cell = row.GetCell(j);
  128. if (cell == null)
  129. {
  130. dataRow[j] = "";
  131. }
  132. else
  133. {
  134. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  135. switch (cell.CellType)
  136. {
  137. case CellType.BLANK:
  138. dataRow[j] = "";
  139. break;
  140. case CellType.NUMERIC:
  141. short format = cell.CellStyle.DataFormat;
  142. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  143. if (format == 14 || format == 31 || format == 57 || format == 58)
  144. dataRow[j] = cell.DateCellValue;
  145. else
  146. dataRow[j] = cell.NumericCellValue;
  147. break;
  148. case CellType.STRING:
  149. dataRow[j] = cell.StringCellValue;
  150. break;
  151. case CellType.FORMULA:
  152. dataRow[j] = cell.StringCellValue;
  153. break;
  154. }
  155. }
  156. }
  157. dataTable.Rows.Add(dataRow);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return dataTable;
  164. }
  165. catch (Exception ex)
  166. {
  167. Console.WriteLine(ex.Message);
  168. if (fs != null)
  169. {
  170. fs.Close();
  171. }
  172. return null;
  173. }
  174. }
  175. }
  176. }