Globals.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Text.RegularExpressions;
  8. using System.Reflection;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using CoreFS.CA06;
  12. using Infragistics.Win.UltraWinGrid;
  13. namespace Core.Mes.Client.Comm
  14. {
  15. public class Globals
  16. {
  17. /// <summary>
  18. /// 字符串转是否能转换成非负数
  19. /// </summary>
  20. /// <param name="str"></param>
  21. /// <returns></returns>
  22. public static bool Is_Below_zero(string str)
  23. {
  24. try
  25. {
  26. if (IsDouble(str) && double.Parse(str) > 0)
  27. return true;
  28. else
  29. return false;
  30. }
  31. catch
  32. {
  33. return false;
  34. }
  35. }
  36. /// <summary>
  37. /// 保留指定位数小数
  38. /// </summary>
  39. /// <param name="sVal"></param>
  40. /// <param name="Digits"></param>
  41. /// <returns></returns>
  42. public static string ValWithDigits(string sVal, int Digits)
  43. {
  44. try
  45. {
  46. decimal d = decimal.Parse(sVal);
  47. string sFormat = "0";
  48. if (Digits > 0)
  49. {
  50. sFormat += ".";
  51. for (int i = 0; i < Digits; i++)
  52. sFormat += "0";
  53. }
  54. return d.ToString(sFormat);
  55. }
  56. catch
  57. {
  58. return "";
  59. }
  60. }
  61. /// <summary>
  62. /// 单元格复制内容
  63. /// </summary>
  64. /// <param name="strMessage"></param>
  65. public static void cellCopy(string strMessage)
  66. {
  67. Clipboard.SetDataObject(strMessage, false);
  68. }
  69. public static void CopyDataToDataTable(DataTable source, DataTable dest, bool clearExists)
  70. {
  71. if (source != null && dest != null)
  72. {
  73. if (clearExists)
  74. {
  75. dest.Rows.Clear();
  76. }
  77. DataRow row;
  78. for (int i = 0; i < source.Rows.Count; i++)
  79. {
  80. row = dest.NewRow();
  81. for (int j = 0; j < source.Columns.Count; j++)
  82. {
  83. try
  84. {
  85. if (dest.Columns.Contains(source.Columns[j].ColumnName))
  86. {
  87. row[source.Columns[j].ColumnName] = source.Rows[i][j];
  88. }
  89. }
  90. catch { }
  91. }
  92. dest.Rows.Add(row);
  93. }
  94. }
  95. }
  96. public static bool ContaisColumn(ref UltraGrid ultraGrid, string strColumn)
  97. {
  98. try
  99. {
  100. if (ultraGrid == null || ultraGrid.DisplayLayout.Bands[0].Columns.Count == 0 || string.IsNullOrEmpty(strColumn))
  101. {
  102. return false;
  103. }
  104. foreach (UltraGridColumn col in ultraGrid.DisplayLayout.Bands[0].Columns)
  105. {
  106. if (col.Key.Equals(strColumn))
  107. {
  108. return true;
  109. }
  110. }
  111. }
  112. catch { }
  113. return false;
  114. }
  115. public static void LocateRecord(ref UltraGrid ultraGrid, ref Hashtable Columns, out UltraGridRow row)
  116. {
  117. row = null;
  118. try
  119. {
  120. if (ultraGrid == null || ultraGrid.Rows.Count == 0)
  121. {
  122. return;
  123. }
  124. string strColumn = "";
  125. object value = null;
  126. ArrayList alistCols = new ArrayList(Columns.Keys);
  127. for (int i = 0; i < alistCols.Count; i++)
  128. {
  129. strColumn = alistCols[i].ToString();
  130. if (!ContaisColumn(ref ultraGrid, strColumn))
  131. {
  132. return;
  133. }
  134. }
  135. bool Found = false;
  136. UltraGridRow temp = null;
  137. for (int i = 0; i < ultraGrid.Rows.Count; i++)
  138. {
  139. Found = true; ;
  140. temp = ultraGrid.Rows[i];
  141. for (int j = 0; j < alistCols.Count; j++)
  142. {
  143. strColumn = alistCols[j].ToString();
  144. value = Columns[strColumn];
  145. if (!temp.Cells[strColumn].Value.Equals(value))
  146. {
  147. Found = false;
  148. break;
  149. }
  150. }
  151. if (Found)
  152. {
  153. ultraGrid.ActiveRow = temp;
  154. row = temp;
  155. return;
  156. }
  157. }
  158. }
  159. catch (Exception ex) { string str = ex.Message; }
  160. }
  161. /// <summary>
  162. /// 清除UltraGrid的DataSource的数据
  163. /// </summary>
  164. /// <param name="ulGrid"></param>
  165. public static void ClearGridDataSourceData(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  166. {
  167. if (ulGrid.DataSource is DataSet)
  168. {
  169. for (int i = ((DataSet)ulGrid.DataSource).Tables.Count - 1; i >= 0; i--)
  170. {
  171. ((DataSet)ulGrid.DataSource).Tables[i].Rows.Clear();
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// 全选选中的子行
  177. /// </summary>
  178. /// <param name="Gride">UltraGrid</param>
  179. /// <param name="ColumnName">复选框列名</param>
  180. /// <param name="BoolValue">值</param>
  181. public static void doCheckedAllChildRow(Infragistics.Win.UltraWinGrid.UltraGrid Gride, string ColumnName, string BoolValue)
  182. {
  183. Infragistics.Win.UltraWinGrid.UltraGridRow ulRow;
  184. if (Gride.ActiveRow.ParentRow == null)
  185. {
  186. ulRow = Gride.ActiveRow;
  187. }
  188. else
  189. {
  190. ulRow = Gride.ActiveRow.ParentRow;
  191. }
  192. int Index = ulRow.Index;
  193. for (int i = 0; i < Gride.Rows[Index].ChildBands[0].Rows.Count; i++)
  194. {
  195. Gride.Rows[Index].ChildBands[0].Rows[i].Cells[ColumnName].Value = BoolValue;
  196. }
  197. }
  198. /// <summary>
  199. /// 选中行中指定的子行
  200. /// </summary>
  201. /// <param name="Gride">UltraGrid</param>
  202. /// <param name="ColumnName">复选框列名</param>
  203. /// <param name="ColumnName2">需要与子行集合比对的列名</param>
  204. /// <param name="sArgs">需要选中的子行集合</param>
  205. public static void doCheckedAppointChildRow(RowEventArgs e, string ColumnName,string ColumnName2, string[] sArgs)
  206. {
  207. for (int i = 0; i < e.Row.ChildBands[0].Rows.Count; i++)
  208. {
  209. for (int t = 0; t < sArgs.Length; t++)
  210. {
  211. if (e.Row.ChildBands[0].Rows[i].Cells[ColumnName2].Value.ToString() == sArgs[t].ToString())
  212. {
  213. e.Row.ChildBands[0].Rows[i].Cells[ColumnName].Value = "True";
  214. }
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// 获取子行中选中的列ID(取的列名为ID_)
  220. /// </summary>
  221. /// <param name="Gride">UltraGrid</param>
  222. /// <param name="ColumnName">复选框列名</param>
  223. public static string doCheckedRowValue(Infragistics.Win.UltraWinGrid.UltraGrid Gride, string ColumnName)
  224. {
  225. Infragistics.Win.UltraWinGrid.UltraGridRow ulRow;
  226. if (Gride.ActiveRow.ParentRow == null)
  227. {
  228. ulRow = Gride.ActiveRow;
  229. }
  230. else
  231. {
  232. ulRow = Gride.ActiveRow.ParentRow;
  233. }
  234. int Index = ulRow.Index;
  235. string IDString = "";
  236. for (int i = 0; i < Gride.Rows[Index].ChildBands[0].Rows.Count; i++)
  237. {
  238. if (Gride.Rows[Index].ChildBands[0].Rows[i].Cells[ColumnName].Text == "True")
  239. {
  240. IDString += Gride.Rows[Index].ChildBands[0].Rows[i].Cells["ID_"].Value + ",";
  241. }
  242. }
  243. if (IDString != "")
  244. {
  245. IDString = IDString.Substring(0, IDString.Length - 1);
  246. }
  247. return IDString;
  248. }
  249. /// <summary>
  250. /// 选中或取消选中,当前Gride中的所有行
  251. /// </summary>
  252. /// <param name="Gride">UltraGrid</param>
  253. /// <param name="ColumnName">复选框列名</param>
  254. /// <param name="BoolValue">值</param>
  255. public static void doCheckeAllRow(Infragistics.Win.UltraWinGrid.UltraGrid Gride, string ColumnName, string BoolValue)
  256. {
  257. if (Gride != null)
  258. {
  259. for (int i = 0; i < Gride.Rows.Count; i++)
  260. {
  261. Gride.Rows[i].Cells[ColumnName].Value = BoolValue;
  262. }
  263. }
  264. }
  265. #region "数据验证"
  266. /// <summary>
  267. /// 获取字符串字节长度
  268. /// </summary>
  269. /// <param name="strVal">需要获取长度的字符串</param>
  270. /// <returns>字符串字节长度</returns>
  271. public static int GetStrBytesLength(string strVal)
  272. {
  273. int lenTotal = 0;
  274. int n = strVal.Length;
  275. string strWord = "";
  276. int asc;
  277. for (int i = 0; i < n; i++)
  278. {
  279. strWord = strVal.Substring(i, 1);
  280. asc = Convert.ToChar(strWord);
  281. if (asc < 0 || asc > 127)
  282. lenTotal = lenTotal + 2;
  283. else
  284. lenTotal = lenTotal + 1;
  285. }
  286. return lenTotal;
  287. }
  288. /// <summary>
  289. /// 验证DataTable是否为空
  290. /// </summary>
  291. /// <param name="dt">DataTable数据</param>
  292. /// <returns>true为空值</returns>
  293. public static bool IsNullTable(DataTable dt)
  294. {
  295. if (dt == null || dt.Rows.Count <= 0)
  296. {
  297. return true;
  298. }
  299. else
  300. {
  301. return false;
  302. }
  303. }
  304. /// <summary>
  305. /// 校验字符串是否只包含字母与数字
  306. /// </summary>
  307. /// <param name="toVerified">需要校验的字符串</param>
  308. /// <returns>true表示符合要求,false表示不符合要求</returns>
  309. public static bool IsOnlyLetterAndDigit(string toVerified)
  310. {
  311. Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");
  312. return rx.IsMatch(toVerified.Trim(), 0);
  313. }
  314. /// <summary>
  315. /// 检验是否是整数
  316. /// </summary>
  317. /// <param name="str">需要检验的字符串</param>
  318. /// <returns>是否为整数:true是整数,false非整数</returns>
  319. public static bool IsInt(string str)
  320. {
  321. Regex rx = new Regex(@"^[0123456789]+$");
  322. return rx.IsMatch(str);
  323. }
  324. /// <summary>
  325. /// 检验是否是整数
  326. /// </summary>
  327. /// <param name="sVal"></param>
  328. /// <returns></returns>
  329. public static bool IsInt32(string sVal)
  330. {
  331. try
  332. {
  333. Int32.Parse(sVal);
  334. return true;
  335. }
  336. catch
  337. {
  338. return false;
  339. }
  340. }
  341. /// <summary>
  342. /// 校验是否为正的浮点数
  343. /// </summary>
  344. /// <param name="price">需要检验的字符串</param>
  345. /// <returns>是否为正浮点,是返回true,否则返回false</returns>
  346. public static bool IsFloat(string str)
  347. {
  348. Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
  349. return rx.IsMatch(str.Trim());
  350. }
  351. /// <summary>
  352. /// 验证浮点数
  353. /// </summary>
  354. /// <param name="sVal"></param>
  355. /// <returns></returns>
  356. public static bool IsDouble(string sVal)
  357. {
  358. try
  359. {
  360. Double.Parse(sVal);
  361. return true;
  362. }
  363. catch
  364. {
  365. return false;
  366. }
  367. }
  368. /// <summary>
  369. /// 检验是否为数字
  370. /// </summary>
  371. /// <param name="str">需要检验的字符串</param>
  372. /// <returns>是否为数字:true代表是,false代表否</returns>
  373. public static bool IsNumber(string str)
  374. {
  375. Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
  376. return rx.IsMatch(str);
  377. }
  378. /// <summary>
  379. /// 检验字符串是否为日期时间
  380. /// </summary>
  381. /// <param name="str">需要检验的字符串</param>
  382. /// <returns>是否为日期时间:true代表是,false代表否</returns>
  383. public static bool IsNotDateTime(string str)
  384. {
  385. DateTime dt = new DateTime();
  386. return (!(DateTime.TryParse(str, out dt)));
  387. }
  388. /// <summary>
  389. /// 检验字符串是否为邮政编码
  390. /// </summary>
  391. /// <param name="str">需要检验的字符串</param>
  392. /// <returns>是否为邮政编码:true代表是,false代表否</returns>
  393. public static bool IsPostCode(string str)
  394. {
  395. Regex rx = new Regex(@"^[0123456789]{6}$");
  396. return rx.IsMatch(str);
  397. }
  398. /// <summary>
  399. /// 检验字符串是否为身份证号
  400. /// </summary>
  401. /// <param name="str">需要检验的字符串</param>
  402. /// <returns>是否为身份证号:true代表是,false代表否</returns>
  403. public static bool IsCode(string str)
  404. {
  405. Regex rx = new Regex(@"^[0123456789]{15,18}$");
  406. return rx.IsMatch(str);
  407. }
  408. /// <summary>
  409. /// 检验字符串是否为电子邮件
  410. /// </summary>
  411. /// <param name="str">需要检验的字符串</param>
  412. /// <returns>是否为电子邮件:true代表是,false代表否</returns>
  413. public static bool IsEMail(string str)
  414. {
  415. Regex rx = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");
  416. return rx.IsMatch(str);
  417. }
  418. /// <summary>
  419. /// 检验字符串是否为中国地区的电话号码
  420. /// </summary>
  421. /// <param name="str">需要检验的字符串</param>
  422. /// <returns>是否为中国地区的电话号码:true代表是,false代表否</returns>
  423. public static bool IsPhoneNumber(string str)
  424. {
  425. Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");
  426. return rx.IsMatch(str);
  427. }
  428. /// <summary>
  429. /// 检验字符串是否为汉字
  430. /// </summary>
  431. /// <param name="str">需要检验的字符串</param>
  432. /// <returns>是否为汉字:true代表是,false代表否</returns>
  433. public static bool IsChinese(string str)
  434. {
  435. Regex rx = new Regex(@"[\u4e00-\u9fbb]+$");
  436. return rx.IsMatch(str);
  437. }
  438. /// <summary>
  439. /// 检验字符串是否为双字节字符(包括汉字)
  440. /// </summary>
  441. /// <param name="str">需要检验的字符串</param>
  442. /// <returns>是否为双字节字符:true代表是,false代表否</returns>
  443. public static bool IsDoubleByteChar(string str)
  444. {
  445. Regex rx = new Regex(@"[^x00-xff]");
  446. return rx.IsMatch(str);
  447. }
  448. /// <summary>
  449. /// 检验字符串是否为URL地址
  450. /// </summary>
  451. /// <param name="str">需要检验的字符串</param>
  452. /// <returns>是否为URL地址:true代表是,false代表否</returns>
  453. public static bool IsURLAddress(string str)
  454. {
  455. Regex rx = new Regex(@"[a-zA-z]+://[^s]*");
  456. return rx.IsMatch(str);
  457. }
  458. /// <summary>
  459. /// 检验字符串是否为IP地址
  460. /// </summary>
  461. /// <param name="str">需要检验的字符串</param>
  462. /// <returns>是否为IP地址:true代表是,false代表否</returns>
  463. public static bool IsIPAddress(string str)
  464. {
  465. Regex rx = new Regex(@"d+.d+.d+.d+");
  466. return rx.IsMatch(str);
  467. }
  468. /// <summary>
  469. /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)
  470. /// </summary>
  471. /// <param name="toEvaluate">指定的要被处理的字符串</param>
  472. /// <returns>清除HTML标签后的字符串</returns>
  473. public static string RemoveHtmlTags(string toEvaluate)
  474. {
  475. Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);
  476. return rx.Replace(toEvaluate, "");
  477. }
  478. /// <summary>
  479. /// 判断输入的字符串是否完全匹配正则
  480. /// </summary>
  481. /// <param name="RegexExpression">正则表达式</param>
  482. /// <param name="str">待判断的字符串</param>
  483. /// <returns></returns>
  484. public static bool IsValiable(string RegexExpression, string str)
  485. {
  486. bool blResult = false;
  487. Regex rep = new Regex(RegexExpression, RegexOptions.IgnoreCase);
  488. //blResult = rep.IsMatch(str);
  489. Match mc = rep.Match(str);
  490. if (mc.Success)
  491. {
  492. if (mc.Value == str) blResult = true;
  493. }
  494. return blResult;
  495. }
  496. /// <summary>
  497. /// 判断DataSet是否是空值
  498. /// </summary>
  499. /// <param name="ds">DataSet数据</param>
  500. /// <returns>true 空值</returns>
  501. public static bool IsNullData(DataSet ds)
  502. {
  503. if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  504. {
  505. return true;
  506. }
  507. else
  508. {
  509. return false;
  510. }
  511. }
  512. /// <summary>
  513. /// 值比对 sSign:符号,sMin:最小值,sMax:最大值,sRealVal:实际值
  514. /// </summary>
  515. /// <param name="sSign"></param>
  516. /// <param name="sMin"></param>
  517. /// <param name="sMax"></param>
  518. /// <param name="sRealVal"></param>
  519. /// <returns></returns>
  520. public static bool ValIsEligible(string sSign, string sMin, string sMax, string sRealVal)
  521. {
  522. try
  523. {
  524. switch (sSign)
  525. {
  526. case ">":
  527. return (double.Parse(sMin) < double.Parse(sRealVal));
  528. case ">=":
  529. return (double.Parse(sMin) <= double.Parse(sRealVal));
  530. case "=":
  531. if (Globals.IsDouble(sMin))
  532. return (double.Parse(sMin) == double.Parse(sRealVal));
  533. else
  534. return (sMin == sRealVal);
  535. case "<":
  536. return (double.Parse(sMax) > double.Parse(sRealVal));
  537. case "<=":
  538. return (double.Parse(sMax) >= double.Parse(sRealVal));
  539. default:
  540. return true;
  541. }
  542. }
  543. catch
  544. {
  545. return false;
  546. }
  547. }
  548. #endregion
  549. /// <summary>
  550. /// 数字转换成中文数字
  551. /// </summary>
  552. /// <param name="strNum"></param>
  553. /// <returns></returns>
  554. public static string ConvertNumberToChinese(string strNum)
  555. {
  556. string[] Nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  557. string[] Digits = { "", "拾", "佰", "仟" };
  558. string[] Units = { "元", "万", "亿", "万亿" };
  559. string x, y, z = "";
  560. if (strNum.Length > 2)
  561. {
  562. x = strNum.Substring(0, strNum.Length - 2);
  563. y = strNum.Substring(strNum.Length - 2, 2);
  564. }
  565. else
  566. {
  567. x = "";
  568. y = strNum;
  569. }
  570. if (y.Length == 2)
  571. {
  572. int n = Convert.ToInt32(y.Substring(0, 1));
  573. z = Nums[n] + "角";
  574. }
  575. if (y.Length > 0)
  576. {
  577. int n = Convert.ToInt32(y.Substring(y.Length - 1, 1));
  578. z += Nums[n] + "分";
  579. }
  580. if (y.Length == 0)
  581. {
  582. if (x.Length == 0)
  583. z = "零元整";
  584. else
  585. z = "整";
  586. }
  587. string S = ""; //返回值
  588. int p = 0; //字符位置指针
  589. int m = x.Length % 4; //取模
  590. // 四位一组得到组数
  591. int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);
  592. // 外层循环在所有组中循环
  593. // 从左到右 高位到低位 四位一组 逐组处理
  594. // 每组最后加上一个单位: "[万亿]","[亿]","[万]"
  595. for (int i = k; i > 0; i--)
  596. {
  597. int L = 4;
  598. if (i == k && m != 0)
  599. {
  600. L = m;
  601. }
  602. // 得到一组四位数 最高位组有可能不足四位
  603. string s = x.Substring(p, L);
  604. int l = s.Length;
  605. // 内层循环在该组中的每一位数上循环 从左到右 高位到低位
  606. for (int j = 0; j < l; j++)
  607. {
  608. //处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
  609. int n = Convert.ToInt32(s.Substring(j, 1));
  610. if (n == 0)
  611. {
  612. if (j < l - 1
  613. && Convert.ToInt32(s.Substring(j + 1, 1)) > 0 //后一位(右低)
  614. && !S.EndsWith(Nums[n]))
  615. {
  616. S += Nums[n];
  617. }
  618. }
  619. else
  620. {
  621. //处理 1013 一千零"十三", 1113 一千一百"一十三"
  622. if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0) && j == l - 2))
  623. {
  624. S += Nums[n];
  625. }
  626. S += Digits[l - j - 1];
  627. }
  628. }
  629. p += L;
  630. // 每组最后加上一个单位: [万],[亿] 等
  631. if (i < k) //不是最高位的一组
  632. {
  633. if (Convert.ToInt32(s) != 0)
  634. {
  635. //如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
  636. S += Units[i - 1];
  637. }
  638. }
  639. else
  640. {
  641. //处理最高位的一组,最后必须加上单位
  642. S += Units[i - 1];
  643. }
  644. }
  645. return S + z;
  646. }
  647. /// <summary>
  648. /// 时间计算返回分
  649. /// </summary>
  650. /// <param name="startTime"></param>
  651. /// <param name="endTime"></param>
  652. /// <returns></returns>
  653. public static int caculateTime(DateTime startTime, DateTime endTime)
  654. {
  655. int lStayDuration = 0;
  656. TimeSpan odtSpan;
  657. if (endTime > startTime)
  658. {
  659. odtSpan = endTime - startTime;
  660. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
  661. }
  662. else if (startTime != endTime)
  663. {
  664. if (startTime > DateTime.Now)
  665. lStayDuration = 0;
  666. else
  667. {
  668. odtSpan = DateTime.Now - startTime;
  669. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
  670. }
  671. }
  672. return lStayDuration;
  673. }
  674. /// <summary>
  675. /// 时间计算返回秒
  676. /// </summary>
  677. /// <param name="startTime"></param>
  678. /// <param name="endTime"></param>
  679. /// <returns></returns>
  680. public static int caculateTimeSeconds(DateTime startTime, DateTime endTime)
  681. {
  682. int lStayDuration = 0;
  683. TimeSpan odtSpan;
  684. if (endTime > startTime)
  685. {
  686. odtSpan = endTime - startTime;
  687. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalSeconds));
  688. }
  689. else if (startTime != endTime)
  690. {
  691. if (startTime > DateTime.Now)
  692. lStayDuration = 0;
  693. else
  694. {
  695. odtSpan = DateTime.Now - startTime;
  696. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalSeconds));
  697. }
  698. }
  699. return lStayDuration;
  700. }
  701. /// <summary>
  702. /// 获取两个时间段的差值
  703. /// </summary>
  704. /// <param name="startTime"></param>
  705. /// <param name="endTime"></param>
  706. /// <returns></returns>
  707. public static int JudgeTime(DateTime startTime, DateTime endTime)
  708. {
  709. int lStayDuration = 0;
  710. TimeSpan odtSpan;
  711. if (endTime > startTime)
  712. {
  713. odtSpan = endTime - startTime;
  714. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
  715. }
  716. return lStayDuration;
  717. }
  718. /// <summary>
  719. /// 将ultragrid的数据导出到Excel中
  720. /// </summary>
  721. /// <param name="ulGrid">要导出数据的ulGrid名称</param>
  722. /// <param name="sFileName">Excel文件名</param>
  723. public static void ulGridToExcel(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid, string sFileName)
  724. {
  725. try
  726. {
  727. Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter ulGridExt = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
  728. ulGridExt.CellExporting += new Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventHandler(ultraGridExcelExporter1_CellExporting);
  729. System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  730. if (ulGrid.Rows.Count == 0)
  731. {
  732. MessageBox.Show("没有数据!", "提示");
  733. return;
  734. }
  735. saveFileDialog1.FileName = sFileName + DateTime.Now.ToString("yyMMdd") + ".xls";
  736. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  737. {
  738. string sFullName = saveFileDialog1.FileName;
  739. ulGridExt.Export(ulGrid, sFullName);
  740. ProcessStartInfo p = new ProcessStartInfo(sFullName);
  741. p.WorkingDirectory = Path.GetDirectoryName(sFullName);
  742. Process.Start(p);
  743. }
  744. }
  745. catch (Exception ex)
  746. {
  747. MessageBox.Show("导出失败,原因:" + ex.Message);
  748. }
  749. }
  750. /// <summary>
  751. /// 当Grid导出EXCEL时,列字段中的值为编码时,将编码转换成中文
  752. /// </summary>
  753. /// <param name="sender"></param>
  754. /// <param name="e"></param>
  755. public static void ultraGridExcelExporter1_CellExporting(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventArgs e)
  756. {
  757. try
  758. {
  759. if (e.GridColumn.RowLayoutColumnInfo.LabelPosition == Infragistics.Win.UltraWinGrid.LabelPosition.LabelOnly)
  760. {
  761. e.Cancel = true;
  762. }
  763. if (e.GridColumn.EditorControl != null || e.GridColumn.ValueList != null)
  764. {
  765. e.Value = e.GridRow.GetCellText(e.GridColumn);
  766. }
  767. }
  768. catch
  769. {
  770. }
  771. }
  772. /// <summary>
  773. /// 清除Grid的列过滤
  774. /// </summary>
  775. /// <param name="ulGrid">Grid名称</param>
  776. public static void ClearUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  777. {
  778. ulGrid.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();
  779. }
  780. /// <summary>
  781. /// 增加Grid的列过滤
  782. /// </summary>
  783. /// <param name="ulGrid"></param>
  784. public static void AddUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  785. {
  786. ulGrid.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
  787. }
  788. //查找treeView结点的父结点
  789. public static TreeNode findParentNode(TreeNode preLvlNode, string parentKey)
  790. {
  791. if (preLvlNode.Name == parentKey) return preLvlNode;
  792. foreach (TreeNode node in preLvlNode.Nodes)
  793. {
  794. if (node.Name.Length <= parentKey.Length)
  795. {
  796. if (node.Nodes.Count > 0)
  797. {
  798. TreeNode tNode = findParentNode(node, parentKey);
  799. if (tNode != null)
  800. return tNode;
  801. }
  802. else
  803. if (node.Name == parentKey)
  804. return node;
  805. }
  806. else
  807. return null;
  808. }
  809. return null;
  810. }
  811. }
  812. }