You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
9.0 KiB

2 years ago
  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.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text.RegularExpressions;
  13. namespace EasyBL
  14. {
  15. public class Util
  16. {
  17. public static string GetLastExceptionMsg(Exception e)
  18. {
  19. var sRes = "";
  20. var eCur = e;
  21. while (null != eCur.InnerException)
  22. {
  23. eCur = eCur.InnerException;
  24. }
  25. sRes = eCur.Message;
  26. return sRes;
  27. }
  28. public static string GetValueByPropertyName(object i_oTarget, string i_sPropertyName, out object o_oRes)
  29. {
  30. string sMsg = null;
  31. object oRes = null;
  32. try
  33. {
  34. do
  35. {
  36. var pi = i_oTarget.GetType().GetProperty(i_sPropertyName);
  37. if (null == pi)
  38. {
  39. sMsg = "NO PROPERTY(MAYBE ORM ENTITY NOT SYNC)";
  40. break;
  41. }
  42. oRes = pi.GetValue(i_oTarget, null);
  43. }
  44. while (false);
  45. }
  46. catch (Exception ex)
  47. {
  48. sMsg = GetLastExceptionMsg(ex);
  49. }
  50. o_oRes = oRes;
  51. return sMsg;
  52. }
  53. public static string SetValueToInstByPropertyName(object i_oTarget, string i_sPropertyName, object i_v)
  54. {
  55. string sMsg = null;
  56. try
  57. {
  58. do
  59. {
  60. var pi = i_oTarget.GetType().GetProperty(i_sPropertyName);
  61. if (null == pi)
  62. {
  63. sMsg = "NO PROPERTY(MAYBE ORM ENTITY NOT SYNC)";
  64. break;
  65. }
  66. var od = ConvertValue(pi, i_v);
  67. pi.SetValue(i_oTarget, od, null);
  68. }
  69. while (false);
  70. }
  71. catch (Exception ex)
  72. {
  73. sMsg = GetLastExceptionMsg(ex);
  74. }
  75. return sMsg;
  76. }
  77. public static object ConvertValue(PropertyInfo i_pi, object i_oValue)
  78. {
  79. var tRealType = GetRealType(i_pi.PropertyType);
  80. return Convert.ChangeType(i_oValue, tRealType);
  81. }
  82. public static Type GetRealType(Type i_tTest)
  83. {
  84. return Nullable.GetUnderlyingType(i_tTest) ?? i_tTest;
  85. }
  86. public static object GetInstByType(Type i_tType)
  87. {
  88. return Activator.CreateInstance(i_tType);
  89. }
  90. /// <summary>
  91. /// Create Instance by name
  92. /// </summary>
  93. /// <param name="i_sTypeName"></param>
  94. /// <param name="o_oRes"></param>
  95. /// <returns></returns>
  96. public static string GetInstByClassName(string i_sTypeName, out object o_oRes)
  97. {
  98. object oRes = null;
  99. string sMsg = null;
  100. do
  101. {
  102. var tCur = GetTypeByTypeName(i_sTypeName);
  103. if (null == tCur)
  104. {
  105. sMsg = "NO THIS ENTITY";
  106. break;
  107. }
  108. oRes = Activator.CreateInstance(tCur);
  109. if (null == oRes)
  110. {
  111. sMsg = "ENTITY CREATE FAIL";
  112. break;
  113. }
  114. }
  115. while (false);
  116. o_oRes = oRes;
  117. return sMsg;
  118. }
  119. public static Type GetTypeByTypeName(string i_sTypeName)
  120. {
  121. Type tRes = null;
  122. var sCodebase = Assembly.GetExecutingAssembly().GetName().CodeBase;
  123. sCodebase = sCodebase.Substring(0, sCodebase.LastIndexOf("/"));
  124. var assAll = AppDomain.CurrentDomain.GetAssemblies().Where(f => false == f.IsDynamic).ToArray();
  125. var la = new List<Assembly>();
  126. foreach (Assembly a in assAll)
  127. {
  128. try
  129. {
  130. if (null != a.CodeBase && a.CodeBase.StartsWith(sCodebase))
  131. {
  132. la.Add(a);
  133. }
  134. }
  135. catch (Exception e)
  136. {
  137. Console.Write(e.Message);
  138. }
  139. }
  140. foreach (Assembly a in la)
  141. {
  142. var at = a.GetTypes().ToArray<Type>();
  143. foreach (Type t in at)
  144. {
  145. if (false == t.IsClass)
  146. {
  147. continue;
  148. }
  149. if (t.Name == i_sTypeName)
  150. {
  151. tRes = t;
  152. break;
  153. }
  154. }
  155. if (null != tRes)
  156. {
  157. break;
  158. }
  159. }
  160. return tRes;
  161. }
  162. public static string XLSToDataTable(string i_sSrcExcel, out DataTable o_oRes)
  163. {
  164. DataTable dtRes = null;
  165. string sMsg = null;
  166. do
  167. {
  168. try
  169. {
  170. using (var dtTemp = new DataTable())
  171. {
  172. if (!File.Exists(i_sSrcExcel))
  173. {
  174. sMsg = "FILE NOT EXIST";
  175. break;
  176. }
  177. ISheet sheet = null;
  178. var sr = new FileStream(i_sSrcExcel, FileMode.Open);
  179. if (i_sSrcExcel.ToLower().EndsWith(".xls"))
  180. {
  181. var workbook = new HSSFWorkbook(sr);
  182. sheet = workbook.GetSheetAt(0);
  183. }
  184. else if (i_sSrcExcel.ToLower().EndsWith(".xlsx"))
  185. {
  186. var workbook = new XSSFWorkbook(sr);
  187. sheet = workbook.GetSheetAt(0);
  188. }
  189. var sSheetName = sheet.SheetName;
  190. var headerRow = sheet.GetRow(0);
  191. int cellCount = headerRow.LastCellNum;
  192. for (int j = headerRow.FirstCellNum; j < cellCount; j++)
  193. {
  194. var dc = dtTemp.Columns.Add();
  195. object oData = headerRow.GetCell(j);
  196. dc.ColumnName = oData.ToString();
  197. dc.DataType = typeof(string);
  198. }
  199. for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  200. {
  201. var row = sheet.GetRow(i);
  202. var dr = dtTemp.Rows.Add();
  203. for (int j = row.FirstCellNum; j < cellCount; j++)
  204. {
  205. object oData = row.GetCell(j);
  206. dr[j] = oData?.ToString();
  207. }
  208. }
  209. sr.Close();
  210. sheet = null;
  211. dtRes = dtTemp;
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. sMsg = GetLastExceptionMsg(ex);
  217. }
  218. }
  219. while (false);
  220. o_oRes = dtRes;
  221. return sMsg;
  222. }
  223. public static bool IsValidEmail(string i_sTest)
  224. {
  225. return Regex.IsMatch(i_sTest, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
  226. }
  227. public static string Format(string i_sFormat, object i_oObject)
  228. {
  229. foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(i_oObject))
  230. {
  231. i_sFormat = i_sFormat.Replace("{" + prop.Name + "}", (prop.GetValue(i_oObject) ?? "(null)").ToString());
  232. }
  233. return i_sFormat;
  234. }
  235. public static string ProcessCmd(string i_sProcessName, string i_sAgruments)
  236. {
  237. string sRes = null;
  238. try
  239. {
  240. var psi = new ProcessStartInfo
  241. {
  242. Arguments = i_sAgruments,
  243. FileName = i_sProcessName,
  244. RedirectStandardOutput = true,
  245. UseShellExecute = false,
  246. CreateNoWindow = true
  247. };
  248. using (var p = new Process
  249. {
  250. StartInfo = psi
  251. })
  252. {
  253. p.Start();
  254. sRes = p.StandardOutput.ReadToEnd();
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. sRes = GetLastExceptionMsg(ex);
  260. }
  261. return sRes;
  262. }
  263. }
  264. }