Form4.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.Net;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using UMESDLLService;
  17. namespace FileWatcher
  18. {
  19. public partial class Form4 : Form
  20. {
  21. public Form4()
  22. {
  23. InitializeComponent();
  24. }
  25. private void label12_Click(object sender, EventArgs e)
  26. {
  27. }
  28. private void textBox7_TextChanged(object sender, EventArgs e)
  29. {
  30. }
  31. private void textBox3_TextChanged(object sender, EventArgs e)
  32. {
  33. }
  34. Thread InitDB;
  35. Thread InitDB1;
  36. Thread InitDB2;
  37. Thread InitDB3;
  38. DataTable dt;
  39. //DataHelper dh = new DataHelper();
  40. private void Form4_Load(object sender, EventArgs e)
  41. {
  42. dt = ExcelToDataTable(@"C:\Users\callm\Desktop\Autosave\Vortex-ICC ID数据库1.xlsx", true);
  43. InitDB = new Thread(ConnectDB);
  44. InitDB.Start();
  45. InitDB1 = new Thread(ConnectDB1);
  46. InitDB1.Start();
  47. InitDB2 = new Thread(ConnectDB2);
  48. InitDB2.Start();
  49. InitDB3 = new Thread(ConnectDB3);
  50. InitDB3.Start();
  51. }
  52. private void ConnectDB()
  53. {
  54. DataHelper dh = new DataHelper();
  55. for (int i = 150000; i <= 200000; i++)
  56. {
  57. Console.WriteLine(i);
  58. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  59. dh.ExecuteSql(sql, "insert");
  60. }
  61. }
  62. private void ConnectDB1()
  63. {
  64. DataHelper dh = new DataHelper();
  65. for (int i = 350000; i <= 400000; i++)
  66. {
  67. Console.WriteLine(i);
  68. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  69. dh.ExecuteSql(sql, "insert");
  70. }
  71. }
  72. private void ConnectDB2()
  73. {
  74. DataHelper dh = new DataHelper();
  75. for (int i = 550000; i <= 600000; i++)
  76. {
  77. Console.WriteLine(i);
  78. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  79. dh.ExecuteSql(sql, "insert");
  80. }
  81. }
  82. private void ConnectDB3()
  83. {
  84. DataHelper dh = new DataHelper();
  85. for (int i = 650000; i <= 700000; i++)
  86. {
  87. Console.WriteLine( i);
  88. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  89. dh.ExecuteSql(sql, "insert");
  90. }
  91. }
  92. public static string ToUrlEncode(string strCode)
  93. {
  94. StringBuilder sb = new StringBuilder();
  95. byte[] byStr = System.Text.Encoding.UTF8.GetBytes(strCode); //默认是System.Text.Encoding.Default.GetBytes(str)
  96. System.Text.RegularExpressions.Regex regKey = new System.Text.RegularExpressions.Regex("^[A-Za-z0-9]+$");
  97. for (int i = 0; i < byStr.Length; i++)
  98. {
  99. string strBy = Convert.ToChar(byStr[i]).ToString();
  100. if (regKey.IsMatch(strBy))
  101. {
  102. //是字母或者数字则不进行转换
  103. sb.Append(strBy);
  104. }
  105. else
  106. {
  107. sb.Append(@"%" + Convert.ToString(byStr[i], 16));
  108. }
  109. }
  110. return (sb.ToString());
  111. }
  112. private void textBox4_TextChanged(object sender, EventArgs e)
  113. {
  114. }
  115. private void SaveFileToJPG(string filename)
  116. {
  117. DataHelper dh = new DataHelper();
  118. DataTable dt = ExcelToDataTable(@"C:\Users\callm\Desktop\Autosave\Vortex-ICC ID数据库.xlsx", true);
  119. for (int i = 0; i < dt.Rows.Count; i++)
  120. {
  121. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  122. dh.ExecuteSql(sql, "insert");
  123. }
  124. }
  125. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  126. {
  127. DataTable dataTable = null;
  128. FileStream fs = null;
  129. DataColumn column = null;
  130. DataRow dataRow = null;
  131. IWorkbook workbook = null;
  132. ISheet sheet = null;
  133. IRow row = null;
  134. ICell cell = null;
  135. int startRow = 0;
  136. try
  137. {
  138. using (fs = File.OpenRead(filePath))
  139. {
  140. // 2007版本
  141. if (filePath.IndexOf(".xlsx") > 0)
  142. {
  143. workbook = new XSSFWorkbook(fs);
  144. }
  145. // 2003版本
  146. else if (filePath.IndexOf(".xls") > 0)
  147. {
  148. workbook = new HSSFWorkbook(fs);
  149. }
  150. if (workbook != null)
  151. {
  152. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  153. dataTable = new DataTable();
  154. if (sheet != null)
  155. {
  156. int rowCount = sheet.LastRowNum;//总行数
  157. if (rowCount > 0)
  158. {
  159. IRow firstRow = sheet.GetRow(0);//第一行
  160. int cellCount = firstRow.LastCellNum;//列数
  161. //构建datatable的列
  162. if (isColumnName)
  163. {
  164. startRow = 1;//如果第一行是列名,则从第二行开始读取
  165. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  166. {
  167. cell = firstRow.GetCell(i);
  168. if (cell != null)
  169. {
  170. if (cell.StringCellValue != null)
  171. {
  172. column = new DataColumn(cell.StringCellValue);
  173. dataTable.Columns.Add(column);
  174. }
  175. }
  176. }
  177. }
  178. else
  179. {
  180. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  181. {
  182. column = new DataColumn("column" + (i + 1));
  183. dataTable.Columns.Add(column);
  184. }
  185. }
  186. //填充行
  187. for (int i = startRow; i <= rowCount; ++i)
  188. {
  189. row = sheet.GetRow(i);
  190. if (row == null) continue;
  191. dataRow = dataTable.NewRow();
  192. for (int j = row.FirstCellNum; j < cellCount; ++j)
  193. {
  194. cell = row.GetCell(j);
  195. if (cell == null)
  196. {
  197. dataRow[j] = "";
  198. }
  199. else
  200. {
  201. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  202. switch (cell.CellType)
  203. {
  204. case CellType.Blank:
  205. dataRow[j] = "";
  206. break;
  207. case CellType.Numeric:
  208. short format = cell.CellStyle.DataFormat;
  209. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  210. if (format == 14 || format == 31 || format == 57 || format == 58)
  211. dataRow[j] = cell.DateCellValue;
  212. else
  213. dataRow[j] = cell.NumericCellValue;
  214. break;
  215. case CellType.String:
  216. dataRow[j] = cell.StringCellValue;
  217. break;
  218. case CellType.Formula:
  219. dataRow[j] = cell.StringCellValue;
  220. break;
  221. }
  222. }
  223. }
  224. dataTable.Rows.Add(dataRow);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. return dataTable;
  231. }
  232. catch (Exception ex)
  233. {
  234. Console.WriteLine(ex.Message);
  235. if (fs != null)
  236. {
  237. fs.Close();
  238. }
  239. return null;
  240. }
  241. }
  242. }
  243. }