Form4.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.XSSF.UserModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. namespace FileWatcher
  15. {
  16. public partial class Form4 : Form
  17. {
  18. public Form4()
  19. {
  20. InitializeComponent();
  21. }
  22. private void label12_Click(object sender, EventArgs e)
  23. {
  24. }
  25. private void textBox7_TextChanged(object sender, EventArgs e)
  26. {
  27. }
  28. private void textBox3_TextChanged(object sender, EventArgs e)
  29. {
  30. }
  31. DataHelper dh = new DataHelper();
  32. private void Form4_Load(object sender, EventArgs e)
  33. {
  34. //DirectoryInfo folder = new DirectoryInfo(@"C:\\Users\\callm\\Desktop\\照片\\");
  35. //foreach (FileInfo file in folder.GetFiles("*.*", SearchOption.AllDirectories))
  36. //{
  37. // Dictionary<string, object> dic = new Dictionary<string, object>
  38. // {
  39. // { "em_name", "管理员" },
  40. // { "em_code", "ADMIN" },
  41. // { "caller", "AGING" }
  42. // };
  43. // string fp_id;
  44. // string fp_path;
  45. // BaseUtil.UploadFilesToRemoteUrl("http://192.168.6.253:8099/mes/MEScommon/uploadFiles.action?_noc=1", file.FullName, dic, out fp_path, out fp_id);
  46. //}
  47. //var myThread = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\1.xlsx"));
  48. //myThread.Start();
  49. //var myThread1 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\2.xlsx"));
  50. //myThread1.Start();
  51. //var myThread2 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\3.xlsx"));
  52. //myThread2.Start();
  53. //var myThread3 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\4.xlsx"));
  54. //myThread3.Start();
  55. //var myThread4 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\5.xlsx"));
  56. //myThread4.Start();
  57. //var myThread5 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\6.xlsx"));
  58. //myThread5.Start();
  59. //var myThread6 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\7.xlsx"));
  60. //myThread6.Start();
  61. //var myThread7 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\8.xlsx"));
  62. //myThread7.Start();
  63. //var myThread8 = new Thread(() => SaveFileToJPG(@"C:\Users\callm\Desktop\导入数据\9.xlsx"));
  64. //myThread8.Start();
  65. }
  66. private void textBox4_TextChanged(object sender, EventArgs e)
  67. {
  68. }
  69. private void SaveFileToJPG(string filename)
  70. {
  71. DataHelper dh = new DataHelper();
  72. DataTable dt = ExcelToDataTable(filename, true);
  73. for (int i = 0; i < dt.Rows.Count; i++)
  74. {
  75. string sql = "insert into deviceproduct(dp_id,dp_prodcode,dp_decode)values(deviceproduct_seq.nextval,'" + dt.Rows[i]["dp_prodcode"].ToString() + "','" + dt.Rows[i]["de_code"].ToString() + "')";
  76. dh.ExecuteSql(sql, "insert");
  77. }
  78. }
  79. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  80. {
  81. DataTable dataTable = null;
  82. FileStream fs = null;
  83. DataColumn column = null;
  84. DataRow dataRow = null;
  85. IWorkbook workbook = null;
  86. ISheet sheet = null;
  87. IRow row = null;
  88. ICell cell = null;
  89. int startRow = 0;
  90. try
  91. {
  92. using (fs = File.OpenRead(filePath))
  93. {
  94. // 2007版本
  95. if (filePath.IndexOf(".xlsx") > 0)
  96. {
  97. workbook = new XSSFWorkbook(fs);
  98. }
  99. // 2003版本
  100. else if (filePath.IndexOf(".xls") > 0)
  101. {
  102. workbook = new HSSFWorkbook(fs);
  103. }
  104. if (workbook != null)
  105. {
  106. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  107. dataTable = new DataTable();
  108. if (sheet != null)
  109. {
  110. int rowCount = sheet.LastRowNum;//总行数
  111. if (rowCount > 0)
  112. {
  113. IRow firstRow = sheet.GetRow(0);//第一行
  114. int cellCount = firstRow.LastCellNum;//列数
  115. //构建datatable的列
  116. if (isColumnName)
  117. {
  118. startRow = 1;//如果第一行是列名,则从第二行开始读取
  119. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  120. {
  121. cell = firstRow.GetCell(i);
  122. if (cell != null)
  123. {
  124. if (cell.StringCellValue != null)
  125. {
  126. column = new DataColumn(cell.StringCellValue);
  127. dataTable.Columns.Add(column);
  128. }
  129. }
  130. }
  131. }
  132. else
  133. {
  134. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  135. {
  136. column = new DataColumn("column" + (i + 1));
  137. dataTable.Columns.Add(column);
  138. }
  139. }
  140. //填充行
  141. for (int i = startRow; i <= rowCount; ++i)
  142. {
  143. row = sheet.GetRow(i);
  144. if (row == null) continue;
  145. dataRow = dataTable.NewRow();
  146. for (int j = row.FirstCellNum; j < cellCount; ++j)
  147. {
  148. cell = row.GetCell(j);
  149. if (cell == null)
  150. {
  151. dataRow[j] = "";
  152. }
  153. else
  154. {
  155. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  156. switch (cell.CellType)
  157. {
  158. case CellType.Blank:
  159. dataRow[j] = "";
  160. break;
  161. case CellType.Numeric:
  162. short format = cell.CellStyle.DataFormat;
  163. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  164. if (format == 14 || format == 31 || format == 57 || format == 58)
  165. dataRow[j] = cell.DateCellValue;
  166. else
  167. dataRow[j] = cell.NumericCellValue;
  168. break;
  169. case CellType.String:
  170. dataRow[j] = cell.StringCellValue;
  171. break;
  172. case CellType.Formula:
  173. dataRow[j] = cell.StringCellValue;
  174. break;
  175. }
  176. }
  177. }
  178. dataTable.Rows.Add(dataRow);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. return dataTable;
  185. }
  186. catch (Exception)
  187. {
  188. if (fs != null)
  189. {
  190. fs.Close();
  191. }
  192. return null;
  193. }
  194. }
  195. }
  196. }