IcsFromERPJob
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.

381 lines
11 KiB

4 months ago
4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ICSSoft.FromERP
  6. {
  7. public static class ConvertExt
  8. {
  9. public static string ToStringBz(this DateTime date)
  10. {
  11. return date.ToString("yyyy-MM-dd HH:mm:ss");
  12. }
  13. public static char ToChar(this object data)
  14. {
  15. char returnValue = default(char);
  16. if (data == null)
  17. return returnValue;
  18. else
  19. {
  20. if (char.TryParse(data.ToString(), out returnValue))
  21. return returnValue;
  22. else
  23. return returnValue;
  24. }
  25. }
  26. #region 文本转换
  27. public static string ToStringExt(this object data)
  28. {
  29. return data == null ? string.Empty : data.ToString();
  30. }
  31. #endregion
  32. #region 日期转换
  33. /// <summary>
  34. /// 转换为日期
  35. /// </summary>
  36. /// <param name="data">数据</param>
  37. /// <returns>DateTime.</returns>
  38. public static DateTime ToDate(this object data)
  39. {
  40. try
  41. {
  42. if (data == null)
  43. return DateTime.MinValue;
  44. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
  45. {
  46. string strValue = data.ToStringExt();
  47. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
  48. }
  49. DateTime result;
  50. return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
  51. }
  52. catch
  53. {
  54. return DateTime.MinValue;
  55. }
  56. }
  57. /// <summary>
  58. /// 转换为可空日期
  59. /// </summary>
  60. /// <param name="data">数据</param>
  61. /// <returns>System.Nullable&lt;DateTime&gt;.</returns>
  62. public static DateTime? ToDateOrNull(this object data)
  63. {
  64. try
  65. {
  66. if (data == null)
  67. return null;
  68. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
  69. {
  70. string strValue = data.ToStringExt();
  71. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
  72. }
  73. DateTime result;
  74. bool isValid = DateTime.TryParse(data.ToString(), out result);
  75. if (isValid)
  76. return result;
  77. return null;
  78. }
  79. catch
  80. {
  81. return null;
  82. }
  83. }
  84. /// <summary>
  85. /// 转换为日期
  86. /// </summary>
  87. /// <param name="data">数据</param>
  88. /// <returns>DateTime.</returns>
  89. public static DateTime ToDateTime(this object data)
  90. {
  91. try
  92. {
  93. if (data == null)
  94. return DateTime.MinValue;
  95. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{14}$"))
  96. {
  97. string strValue = data.ToStringExt();
  98. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt(),
  99. strValue.Substring(8, 2).ToInt(), strValue.Substring(10, 2).ToInt(), strValue.Substring(12, 2).ToInt());
  100. }
  101. DateTime result;
  102. return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
  103. }
  104. catch
  105. {
  106. return DateTime.MinValue;
  107. }
  108. }
  109. /// <summary>
  110. /// 转换为可空日期
  111. /// </summary>
  112. /// <param name="data">数据</param>
  113. /// <returns>System.Nullable&lt;DateTime&gt;.</returns>
  114. public static DateTime? ToDateTimeOrNull(this object data)
  115. {
  116. try
  117. {
  118. if (data == null)
  119. return null;
  120. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{14}$"))
  121. {
  122. string strValue = data.ToStringExt();
  123. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt(),
  124. strValue.Substring(8, 2).ToInt(), strValue.Substring(10, 2).ToInt(), strValue.Substring(12, 2).ToInt());
  125. }
  126. DateTime result;
  127. bool isValid = DateTime.TryParse(data.ToString(), out result);
  128. if (isValid)
  129. return result;
  130. return null;
  131. }
  132. catch
  133. {
  134. return null;
  135. }
  136. }
  137. #endregion
  138. public static bool ToBool(this object data)
  139. {
  140. Boolean returnValue = false;
  141. if (data == null)
  142. return returnValue;
  143. else
  144. {
  145. if (Boolean.TryParse(data.ToString(), out returnValue))
  146. return returnValue;
  147. else
  148. return returnValue;
  149. }
  150. }
  151. public static Nullable<bool> ToBoolWithNull(this object data)
  152. {
  153. Boolean returnValue = false;
  154. if (data == null)
  155. return null;
  156. else
  157. {
  158. if (Boolean.TryParse(data.ToString(), out returnValue))
  159. return returnValue;
  160. else
  161. return null;
  162. }
  163. }
  164. #region 数字转换
  165. public static decimal ToDecimal(this object data)
  166. {
  167. decimal returnValue = 0;
  168. if (data == null)
  169. return returnValue;
  170. else
  171. {
  172. if (decimal.TryParse(data.ToString(), out returnValue))
  173. {
  174. return returnValue;
  175. }
  176. else
  177. {
  178. return 0;
  179. }
  180. }
  181. }
  182. public static Nullable<decimal> ToDecimalWithNull(this object data)
  183. {
  184. decimal returnValue = 0;
  185. if (data == null)
  186. return null;
  187. else
  188. {
  189. if (decimal.TryParse(data.ToString(), out returnValue))
  190. return returnValue;
  191. else
  192. return null;
  193. }
  194. }
  195. public static float ToFloat(this object data)
  196. {
  197. float returnValue = 0;
  198. if (data == null)
  199. return returnValue;
  200. else
  201. {
  202. if (float.TryParse(data.ToString(), out returnValue))
  203. {
  204. return returnValue;
  205. }
  206. else
  207. {
  208. return returnValue;
  209. }
  210. }
  211. }
  212. public static Nullable<float> ToFloatWithNull(this object data)
  213. {
  214. float returnValue = 0;
  215. if (data == null)
  216. return null;
  217. else
  218. {
  219. if (float.TryParse(data.ToString(), out returnValue))
  220. return returnValue;
  221. else
  222. return null;
  223. }
  224. }
  225. public static double ToDouble(this object data)
  226. {
  227. double returnValue = 0;
  228. if (data == null)
  229. return returnValue;
  230. else
  231. {
  232. if (double.TryParse(data.ToString(), out returnValue))
  233. {
  234. return returnValue;
  235. }
  236. else
  237. {
  238. return 0;
  239. }
  240. }
  241. }
  242. public static Nullable<double> ToDoubleWithNull(this object data)
  243. {
  244. double returnValue = 0;
  245. if (data == null)
  246. return null;
  247. else
  248. {
  249. if (double.TryParse(data.ToString(), out returnValue))
  250. return returnValue;
  251. else
  252. return null;
  253. }
  254. }
  255. public static Int64 ToInt64(this object data)
  256. {
  257. Int64 returnValue = 0;
  258. if (data == null)
  259. return returnValue;
  260. else
  261. {
  262. if (Int64.TryParse(data.ToString(), out returnValue))
  263. return returnValue;
  264. else
  265. return 0;
  266. }
  267. }
  268. public static Nullable<Int64> ToInt64WithNull(this object data)
  269. {
  270. Int64 returnValue = 0;
  271. if (data == null)
  272. return null;
  273. else
  274. {
  275. if (Int64.TryParse(data.ToString(), out returnValue))
  276. return returnValue;
  277. else
  278. return null;
  279. }
  280. }
  281. public static Int32 ToInt(this object data)
  282. {
  283. Int32 returnValue = 0;
  284. if (data == null)
  285. return returnValue;
  286. else
  287. {
  288. if (Int32.TryParse(data.ToString(), out returnValue))
  289. {
  290. return returnValue;
  291. }
  292. else
  293. {
  294. return 0;
  295. }
  296. }
  297. }
  298. public static Nullable<Int32> ToIntWithNull(this object data)
  299. {
  300. if (data == null)
  301. return null;
  302. int result;
  303. bool isValid = int.TryParse(data.ToString(), out result);
  304. if (isValid)
  305. return result;
  306. return null;
  307. }
  308. public static Int16 ToInt16(this object data)
  309. {
  310. Int16 returnValue = 0;
  311. if (data == null)
  312. return returnValue;
  313. else
  314. {
  315. if (Int16.TryParse(data.ToString(), out returnValue))
  316. {
  317. return returnValue;
  318. }
  319. else
  320. {
  321. return 0;
  322. }
  323. }
  324. }
  325. public static Nullable<Int16> ToInt16WithNull(this object data)
  326. {
  327. Int16 returnValue = 0;
  328. if (data == null)
  329. return null;
  330. else
  331. {
  332. if (Int16.TryParse(data.ToString(), out returnValue))
  333. return returnValue;
  334. else
  335. return null;
  336. }
  337. }
  338. public static bool IsNumeric(this object o)
  339. {
  340. if (o == null || o.ToString() == "")
  341. return false;
  342. string strNum = o.ToString();
  343. System.Text.RegularExpressions.Regex reg1
  344. = new System.Text.RegularExpressions.Regex(@"^-?[0-9]*$");
  345. return reg1.IsMatch(strNum);
  346. }
  347. #endregion
  348. }
  349. }