Globals.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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 Core.Mes.Client.Comm;
  13. public class Globals
  14. {
  15. /// <summary>
  16. /// 从服务端查询数据(query)
  17. /// </summary>
  18. /// <param name="methodId">服务端MethodId</param>
  19. /// <param name="param">参数数组,需与服务端方法参数一致</param>
  20. /// <param name="ob">界面OB对象</param>
  21. /// <exception cref="MESException">自定义异常</exception>
  22. /// <returns>查询数据集</returns>
  23. public static DataTable GetData(string methodId, object[] param, OpeBase ob)
  24. {
  25. CoreClientParam ccp = new CoreClientParam();
  26. ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf("."));
  27. ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1);
  28. ccp.ServerParams = param;
  29. ccp = ob.ExecuteSortResultByQueryToDataTable(ccp, CoreInvokeType.Internal);
  30. if (ccp == null)
  31. {
  32. throw new MESException("服务端处理失败!");
  33. }
  34. if (ccp.ReturnCode < 0)
  35. {
  36. throw new MESException("服务端处理失败!", ccp.ReturnCode, ccp.ReturnInfo);
  37. }
  38. return ccp.SourceDataTable;
  39. }
  40. /// <summary>
  41. /// 向服务端设置数据(insert、update、delete、procedure)
  42. /// </summary>
  43. /// <param name="methodId">服务端MethodId</param>
  44. /// <param name="param">参数数组,需与服务端方法参数一致</param>
  45. /// <param name="ob">界面OB对象</param>
  46. /// <exception cref="MESException">自定义异常</exception>
  47. /// <returns>count处理数据行数,0 or -num表示失败</returns>
  48. public static int SetData(string methodId, object[] param, OpeBase ob)
  49. {
  50. CoreClientParam ccp = new CoreClientParam();
  51. ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf("."));
  52. ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1);
  53. ccp.ServerParams = param;
  54. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  55. if (ccp == null)
  56. {
  57. throw new MESException("服务端处理失败!");
  58. }
  59. if (ccp.ReturnCode < 0)
  60. {
  61. throw new MESException("服务端处理失败!", ccp.ReturnCode, ccp.ReturnInfo);
  62. }
  63. int count = 0;
  64. if (!int.TryParse(ccp.ReturnObject!=null?ccp.ReturnObject.ToString():null, out count))
  65. {
  66. throw new MESException("服务端处理成功,但返回处理数量不是数字!");
  67. }
  68. return count;
  69. }
  70. /// <summary>
  71. /// 初始化Combox数据
  72. /// </summary>
  73. /// <param name="cmbx">ComboBox控件</param>
  74. /// <param name="dset">DataSet数据源</param>
  75. public static void FillComboxItems(ComboBox cmbx, DataSet dset)
  76. {
  77. if (dset.Tables.Count > 0 && dset.Tables[0].Columns.Count > 1)
  78. {
  79. ArrayList aryTmp = new ArrayList();
  80. for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
  81. {
  82. aryTmp.Add(new ValueListItem(dset.Tables[0].Rows[i][0].ToString(), dset.Tables[0].Rows[i][1].ToString()));
  83. }
  84. cmbx.DataSource = aryTmp;
  85. cmbx.DisplayMember = "Name";
  86. cmbx.ValueMember = "ID";
  87. }
  88. }
  89. /// <summary>
  90. /// 初始化Combox数据,加入数据筛选
  91. /// </summary>
  92. /// <param name="cmbx">ComboBox控件</param>
  93. /// <param name="dset">DataSet数据源</param>
  94. /// <param name="filterCondition">RowFilter条件</param>
  95. public static void FillComboxItems(ComboBox cmbx, DataSet dset, string filterCondition)
  96. {
  97. if (dset.Tables.Count > 0 && dset.Tables[0].Columns.Count > 1)
  98. {
  99. DataView dvw = dset.Tables[0].DefaultView;
  100. dvw.RowFilter = filterCondition;
  101. ArrayList aryTmp = new ArrayList();
  102. for (int i = 0; i < dvw.Count; i++)
  103. {
  104. aryTmp.Add(new ValueListItem(dvw[i][0].ToString(), dvw[i][1].ToString()));
  105. }
  106. cmbx.DataSource = aryTmp;
  107. cmbx.DisplayMember = "Name";
  108. cmbx.ValueMember = "ID";
  109. }
  110. }
  111. /// <summary>
  112. /// 初始化UltraCombo数据
  113. /// </summary>
  114. /// <param name="ulcme">ultraComboEditor控件</param>
  115. /// <param name="dset">DataSet数据源</param>
  116. public static void FillUltraComboItems(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataSet dset)
  117. {
  118. if (dset.Tables.Count > 0 && dset.Tables[0].Columns.Count > 1)
  119. {
  120. for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
  121. ulcme.Items.Add(dset.Tables[0].Rows[i][0].ToString(), dset.Tables[0].Rows[i][1].ToString());
  122. }
  123. }
  124. /// <summary>
  125. /// 初始化CheckedListBox数据,加入数据筛选
  126. /// </summary>
  127. /// <param name="list">CheckedListBox控件</param>
  128. /// <param name="ds">DataSet数据源</param>
  129. /// <param name="filterConddition">RowFilter条件</param>
  130. public static void FillCHKItems(CheckedListBox list, DataSet ds, string filterConddition)
  131. {
  132. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Columns.Count > 1)
  133. {
  134. DataView dvw = ds.Tables[0].DefaultView;
  135. dvw.RowFilter = filterConddition;
  136. System.Web.UI.WebControls.ListItem item;
  137. for (int i = 0; i < dvw.Count; i++)
  138. {
  139. item = new System.Web.UI.WebControls.ListItem(dvw[i][1].ToString(), dvw[i][0].ToString());
  140. list.Items.Add(item);
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// 初始化UltraComboEditor数据
  146. /// </summary>
  147. /// <param name="ulcme"></param>
  148. /// <param name="dt"></param>
  149. public static void FillUlcmeItems(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataTable dt)
  150. {
  151. if (dt.Columns.Count > 0)
  152. {
  153. ulcme.Items.Clear();
  154. for (int i = 0; i < dt.Rows.Count; i++)
  155. ulcme.Items.Add(dt.Rows[i][0].ToString());
  156. }
  157. }
  158. /// <summary>
  159. /// 初始化UltraCombo数据,加入数据筛选
  160. /// </summary>
  161. /// <param name="ulcme">ultraComboEditor控件</param>
  162. /// <param name="dset">DataSet数据源</param>
  163. /// <param name="filterCondition">RowFilter条件</param>
  164. public static void FillUltraComboItems(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataSet dset, string filterCondition)
  165. {
  166. if (dset.Tables.Count > 0 && dset.Tables[0].Columns.Count > 1)
  167. {
  168. DataView dvw = dset.Tables[0].DefaultView;
  169. dvw.RowFilter = filterCondition;
  170. for (int i = 0; i < dvw.Count; i++)
  171. ulcme.Items.Add(dvw[i][0].ToString(), dvw[i][1].ToString());
  172. }
  173. }
  174. /// <summary>
  175. /// 初始化UltraCombo数据
  176. /// </summary>
  177. /// <param name="ulcme"></param>
  178. /// <param name="dt">DataTable</param>
  179. public static void FillUltraComboItems(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataTable dt)
  180. {
  181. if (dt.Columns.Count > 1)
  182. {
  183. for (int i = 0; i < dt.Rows.Count; i++)
  184. ulcme.Items.Add(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString());
  185. }
  186. }
  187. /// <summary>
  188. /// 初始化UltraCombo数据
  189. /// </summary>
  190. /// <param name="ulcme"></param>
  191. /// <param name="dv">DataView</param>
  192. public static void FillUltraComboItems(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataView dv)
  193. {
  194. if (dv.Count > 0)
  195. {
  196. for (int i = 0; i < dv.Count; i++)
  197. ulcme.Items.Add(dv[i]["BASECODE"].ToString(), dv[i]["BASENAME"].ToString());
  198. }
  199. }
  200. /// <summary>
  201. /// 初始化UltraCombo数据,并添加“空”
  202. /// </summary>
  203. /// <param name="ulcme">ultraComboEditor控件</param>
  204. /// <param name="dset">DataSet数据源</param>
  205. public static void FillUlcmItemsAddEmpty(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataSet dset)
  206. {
  207. ulcme.Items.Add("", "空");
  208. FillUltraComboItems(ulcme, dset);
  209. }
  210. /// <summary>
  211. /// 初始化UltraCombo数据,并添加“空”;加入数据筛选
  212. /// </summary>
  213. /// <param name="ulcme">ultraComboEditor控件</param>
  214. /// <param name="dset">DataSet数据源</param>
  215. /// <param name="filterCondition">RowFilter条件</param>
  216. public static void FillUlcmItemsAddEmpty(Infragistics.Win.UltraWinEditors.UltraComboEditor ulcme, DataSet dset, string filterCondition)
  217. {
  218. ulcme.Items.Add("", "空");
  219. FillUltraComboItems(ulcme, dset, filterCondition);
  220. }
  221. /// <summary>
  222. /// 字符串转是否能转换成非负数
  223. /// </summary>
  224. /// <param name="str"></param>
  225. /// <returns></returns>
  226. public static bool Is_Below_zero(string str)
  227. {
  228. try
  229. {
  230. if (IsDouble(str) && double.Parse(str) > 0)
  231. return true;
  232. else
  233. return false;
  234. }
  235. catch
  236. {
  237. return false;
  238. }
  239. }
  240. /// <summary>
  241. /// 删除记录激活指定行
  242. /// </summary>
  243. /// <param name="grid">ultraGrid</param>
  244. /// <param name="delRowIndex">删除行的索引</param>
  245. public static void GridAfterDelRow_ReSelectRow(ref Infragistics.Win.UltraWinGrid.UltraGrid grid, int delRowIndex)
  246. {
  247. if (grid.Rows.Count == 0) return;
  248. if ((delRowIndex + 1) > grid.Rows.Count)
  249. {
  250. grid.Rows[delRowIndex - 1].Activate();
  251. grid.Rows[delRowIndex - 1].Selected = true;
  252. }
  253. else
  254. {
  255. grid.Rows[delRowIndex].Activate();
  256. grid.Rows[delRowIndex].Selected = true;
  257. }
  258. }
  259. /// <summary>
  260. /// 全单位修约(GB 8170-87)
  261. /// </summary>
  262. /// <param name="sOriVal">原始值</param>
  263. /// <param name="ten_interval">10的n次方修约间隔</param>
  264. /// <returns>修约后值</returns>
  265. public static string XYFullUnit(string sOriVal, sbyte ten_interval)
  266. {
  267. try
  268. {
  269. decimal interval = Convert.ToDecimal(Math.Pow(10, ten_interval));//修约间隔
  270. decimal d = decimal.Parse(sOriVal) / interval;
  271. bool isPlus = (d > 0); //是否为正数
  272. d = Math.Abs(d);
  273. int i = (int)d;
  274. if ((d - i) < 0.5m)
  275. {
  276. d = i * interval;
  277. }
  278. else if ((d - i) > 0.5m)
  279. {
  280. d = (i + 1) * interval;
  281. }
  282. else //(d-i) == 0.5m
  283. {
  284. if ((i % 2) == 0) //双数舍去
  285. d = i * interval;
  286. else //单数进一
  287. d = (i + 1) * interval;
  288. }
  289. if (!isPlus)
  290. d = 0 - d;
  291. if (ten_interval >= 0)
  292. return d.ToString();
  293. else
  294. return ValWithDigits(d.ToString(), Math.Abs(ten_interval));
  295. }
  296. catch
  297. {
  298. return "";
  299. }
  300. }
  301. /// <summary>
  302. /// 保留指定位数小数
  303. /// </summary>
  304. /// <param name="sVal"></param>
  305. /// <param name="Digits"></param>
  306. /// <returns></returns>
  307. public static string ValWithDigits(string sVal, int Digits)
  308. {
  309. try
  310. {
  311. decimal d = decimal.Parse(sVal);
  312. string sFormat = "0";
  313. if (Digits > 0)
  314. {
  315. sFormat += ".";
  316. for (int i = 0; i < Digits; i++)
  317. sFormat += "0";
  318. }
  319. return d.ToString(sFormat);
  320. }
  321. catch
  322. {
  323. return "";
  324. }
  325. }
  326. /// <summary>
  327. /// 单元格复制内容
  328. /// </summary>
  329. /// <param name="strMessage"></param>
  330. public static void cellCopy(string strMessage)
  331. {
  332. Clipboard.SetDataObject(strMessage, false);
  333. }
  334. /// <summary>
  335. /// 清除UltraGrid的DataSource的数据
  336. /// </summary>
  337. /// <param name="ulGrid"></param>
  338. public static void ClearGridDataSourceData(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  339. {
  340. if (ulGrid.DataSource is DataSet)
  341. {
  342. for (int i = ((DataSet)ulGrid.DataSource).Tables.Count - 1; i >= 0; i--)
  343. {
  344. ((DataSet)ulGrid.DataSource).Tables[i].Rows.Clear();
  345. }
  346. }
  347. }
  348. #region "数据验证"
  349. /// <summary>
  350. /// 验证DataTable是否为空
  351. /// </summary>
  352. /// <param name="dt">DataTable数据</param>
  353. /// <returns>true为空值</returns>
  354. public static bool IsNullTable(DataTable dt)
  355. {
  356. if (dt == null || dt.Rows.Count <= 0)
  357. {
  358. return true;
  359. }
  360. else
  361. {
  362. return false;
  363. }
  364. }
  365. /// <summary>
  366. /// 校验字符串是否只包含字母与数字
  367. /// </summary>
  368. /// <param name="toVerified">需要校验的字符串</param>
  369. /// <returns>true表示符合要求,false表示不符合要求</returns>
  370. public static bool IsOnlyLetterAndDigit(string toVerified)
  371. {
  372. Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");
  373. return rx.IsMatch(toVerified.Trim(), 0);
  374. }
  375. /// <summary>
  376. /// 检验是否是整数
  377. /// </summary>
  378. /// <param name="str">需要检验的字符串</param>
  379. /// <returns>是否为整数:true是整数,false非整数</returns>
  380. public static bool IsInt(string str)
  381. {
  382. Regex rx = new Regex(@"^[0123456789]+$");
  383. return rx.IsMatch(str);
  384. }
  385. /// <summary>
  386. /// 检验是否是整数(庹建勇)
  387. /// </summary>
  388. /// <param name="sVal"></param>
  389. /// <returns></returns>
  390. public static bool IsInt32(string sVal)
  391. {
  392. try
  393. {
  394. Int32.Parse(sVal);
  395. return true;
  396. }
  397. catch
  398. {
  399. return false;
  400. }
  401. }
  402. /// <summary>
  403. /// 校验是否为正的浮点数
  404. /// </summary>
  405. /// <param name="price">需要检验的字符串</param>
  406. /// <returns>是否为正浮点,是返回true,否则返回false</returns>
  407. public static bool IsFloat(string str)
  408. {
  409. Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
  410. return rx.IsMatch(str.Trim());
  411. }
  412. /// <summary>
  413. /// 验证浮点数
  414. /// </summary>
  415. /// <param name="sVal"></param>
  416. /// <returns></returns>
  417. public static bool IsDouble(string sVal)
  418. {
  419. try
  420. {
  421. Double.Parse(sVal);
  422. return true;
  423. }
  424. catch
  425. {
  426. return false;
  427. }
  428. }
  429. /// <summary>
  430. /// 检验是否为数字
  431. /// </summary>
  432. /// <param name="str">需要检验的字符串</param>
  433. /// <returns>是否为数字:true代表是,false代表否</returns>
  434. public static bool IsNumber(string str)
  435. {
  436. Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
  437. return rx.IsMatch(str);
  438. }
  439. /// <summary>
  440. /// 检验字符串是否为日期时间
  441. /// </summary>
  442. /// <param name="str">需要检验的字符串</param>
  443. /// <returns>是否为日期时间:true代表是,false代表否</returns>
  444. public static bool IsNotDateTime(string str)
  445. {
  446. DateTime dt = new DateTime();
  447. return (!(DateTime.TryParse(str, out dt)));
  448. }
  449. /// <summary>
  450. /// 检验字符串是否为邮政编码
  451. /// </summary>
  452. /// <param name="str">需要检验的字符串</param>
  453. /// <returns>是否为邮政编码:true代表是,false代表否</returns>
  454. public static bool IsPostCode(string str)
  455. {
  456. Regex rx = new Regex(@"^[0123456789]{6}$");
  457. return rx.IsMatch(str);
  458. }
  459. /// <summary>
  460. /// 检验字符串是否为身份证号
  461. /// </summary>
  462. /// <param name="str">需要检验的字符串</param>
  463. /// <returns>是否为身份证号:true代表是,false代表否</returns>
  464. public static bool IsCode(string str)
  465. {
  466. Regex rx = new Regex(@"^[0123456789]{15,18}$");
  467. return rx.IsMatch(str);
  468. }
  469. /// <summary>
  470. /// 检验字符串是否为电子邮件
  471. /// </summary>
  472. /// <param name="str">需要检验的字符串</param>
  473. /// <returns>是否为电子邮件:true代表是,false代表否</returns>
  474. public static bool IsEMail(string str)
  475. {
  476. Regex rx = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");
  477. return rx.IsMatch(str);
  478. }
  479. /// <summary>
  480. /// 检验字符串是否为中国地区的电话号码
  481. /// </summary>
  482. /// <param name="str">需要检验的字符串</param>
  483. /// <returns>是否为中国地区的电话号码:true代表是,false代表否</returns>
  484. public static bool IsPhoneNumber(string str)
  485. {
  486. Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");
  487. return rx.IsMatch(str);
  488. }
  489. /// <summary>
  490. /// 检验字符串是否为汉字
  491. /// </summary>
  492. /// <param name="str">需要检验的字符串</param>
  493. /// <returns>是否为汉字:true代表是,false代表否</returns>
  494. public static bool IsChinese(string str)
  495. {
  496. Regex rx = new Regex(@"u4e00-u9fa5");
  497. return rx.IsMatch(str);
  498. }
  499. /// <summary>
  500. /// 检验字符串是否为双字节字符(包括汉字)
  501. /// </summary>
  502. /// <param name="str">需要检验的字符串</param>
  503. /// <returns>是否为双字节字符:true代表是,false代表否</returns>
  504. public static bool IsDoubleByteChar(string str)
  505. {
  506. Regex rx = new Regex(@"[^x00-xff]");
  507. return rx.IsMatch(str);
  508. }
  509. /// <summary>
  510. /// 检验字符串是否为URL地址
  511. /// </summary>
  512. /// <param name="str">需要检验的字符串</param>
  513. /// <returns>是否为URL地址:true代表是,false代表否</returns>
  514. public static bool IsURLAddress(string str)
  515. {
  516. Regex rx = new Regex(@"[a-zA-z]+://[^s]*");
  517. return rx.IsMatch(str);
  518. }
  519. /// <summary>
  520. /// 检验字符串是否为IP地址
  521. /// </summary>
  522. /// <param name="str">需要检验的字符串</param>
  523. /// <returns>是否为IP地址:true代表是,false代表否</returns>
  524. public static bool IsIPAddress(string str)
  525. {
  526. Regex rx = new Regex(@"d+.d+.d+.d+");
  527. return rx.IsMatch(str);
  528. }
  529. /// <summary>
  530. /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)
  531. /// </summary>
  532. /// <param name="toEvaluate">指定的要被处理的字符串</param>
  533. /// <returns>清除HTML标签后的字符串</returns>
  534. public static string RemoveHtmlTags(string toEvaluate)
  535. {
  536. Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);
  537. return rx.Replace(toEvaluate, "");
  538. }
  539. /// <summary>
  540. /// 判断输入的字符串是否完全匹配正则
  541. /// </summary>
  542. /// <param name="RegexExpression">正则表达式</param>
  543. /// <param name="str">待判断的字符串</param>
  544. /// <returns></returns>
  545. public static bool IsValiable(string RegexExpression, string str)
  546. {
  547. bool blResult = false;
  548. Regex rep = new Regex(RegexExpression, RegexOptions.IgnoreCase);
  549. //blResult = rep.IsMatch(str);
  550. Match mc = rep.Match(str);
  551. if (mc.Success)
  552. {
  553. if (mc.Value == str) blResult = true;
  554. }
  555. return blResult;
  556. }
  557. /// <summary>
  558. /// 判断DataSet是否是空值
  559. /// </summary>
  560. /// <param name="ds">DataSet数据</param>
  561. /// <returns>true 空值</returns>
  562. public static bool IsNullData(DataSet ds)
  563. {
  564. if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  565. {
  566. return true;
  567. }
  568. else
  569. {
  570. return false;
  571. }
  572. }
  573. /// <summary>
  574. /// 值比对 sSign:符号,sMin:最小值,sMax:最大值,sRealVal:实际值
  575. /// </summary>
  576. /// <param name="sSign"></param>
  577. /// <param name="sMin"></param>
  578. /// <param name="sMax"></param>
  579. /// <param name="sRealVal"></param>
  580. /// <returns></returns>
  581. public static bool ValIsEligible(string sSign, string sMin, string sMax, string sRealVal)
  582. {
  583. try
  584. {
  585. switch (sSign)
  586. {
  587. case ">":
  588. return (double.Parse(sMin) < double.Parse(sRealVal));
  589. case ">=":
  590. return (double.Parse(sMin) <= double.Parse(sRealVal));
  591. case "=":
  592. if (Globals.IsDouble(sMin))
  593. return (double.Parse(sMin) == double.Parse(sRealVal));
  594. else
  595. return (sMin == sRealVal);
  596. case "<":
  597. return (double.Parse(sMax) > double.Parse(sRealVal));
  598. case "<=":
  599. return (double.Parse(sMax) >= double.Parse(sRealVal));
  600. default:
  601. return true;
  602. }
  603. }
  604. catch
  605. {
  606. return false;
  607. }
  608. }
  609. #endregion
  610. /// <summary>
  611. /// 数字转换成中文数字
  612. /// </summary>
  613. /// <param name="strNum"></param>
  614. /// <returns></returns>
  615. public static string ConvertNumberToChinese(string strNum)
  616. {
  617. string[] Nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  618. string[] Digits = { "", "拾", "佰", "仟" };
  619. string[] Units = { "元", "万", "亿", "万亿" };
  620. string x, y, z = "";
  621. if (strNum.Length > 2)
  622. {
  623. x = strNum.Substring(0, strNum.Length - 2);
  624. y = strNum.Substring(strNum.Length - 2, 2);
  625. }
  626. else
  627. {
  628. x = "";
  629. y = strNum;
  630. }
  631. if (y.Length == 2)
  632. {
  633. int n = Convert.ToInt32(y.Substring(0, 1));
  634. z = Nums[n] + "角";
  635. }
  636. if (y.Length > 0)
  637. {
  638. int n = Convert.ToInt32(y.Substring(y.Length - 1, 1));
  639. z += Nums[n] + "分";
  640. }
  641. if (y.Length == 0)
  642. {
  643. if (x.Length == 0)
  644. z = "零元整";
  645. else
  646. z = "整";
  647. }
  648. string S = ""; //返回值
  649. int p = 0; //字符位置指针
  650. int m = x.Length % 4; //取模
  651. // 四位一组得到组数
  652. int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);
  653. // 外层循环在所有组中循环
  654. // 从左到右 高位到低位 四位一组 逐组处理
  655. // 每组最后加上一个单位: "[万亿]","[亿]","[万]"
  656. for (int i = k; i > 0; i--)
  657. {
  658. int L = 4;
  659. if (i == k && m != 0)
  660. {
  661. L = m;
  662. }
  663. // 得到一组四位数 最高位组有可能不足四位
  664. string s = x.Substring(p, L);
  665. int l = s.Length;
  666. // 内层循环在该组中的每一位数上循环 从左到右 高位到低位
  667. for (int j = 0; j < l; j++)
  668. {
  669. //处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
  670. int n = Convert.ToInt32(s.Substring(j, 1));
  671. if (n == 0)
  672. {
  673. if (j < l - 1
  674. && Convert.ToInt32(s.Substring(j + 1, 1)) > 0 //后一位(右低)
  675. && !S.EndsWith(Nums[n]))
  676. {
  677. S += Nums[n];
  678. }
  679. }
  680. else
  681. {
  682. //处理 1013 一千零"十三", 1113 一千一百"一十三"
  683. if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0) && j == l - 2))
  684. {
  685. S += Nums[n];
  686. }
  687. S += Digits[l - j - 1];
  688. }
  689. }
  690. p += L;
  691. // 每组最后加上一个单位: [万],[亿] 等
  692. if (i < k) //不是最高位的一组
  693. {
  694. if (Convert.ToInt32(s) != 0)
  695. {
  696. //如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
  697. S += Units[i - 1];
  698. }
  699. }
  700. else
  701. {
  702. //处理最高位的一组,最后必须加上单位
  703. S += Units[i - 1];
  704. }
  705. }
  706. return S + z;
  707. }
  708. /// <summary>
  709. /// 打开其他工程中的窗体
  710. /// </summary>
  711. /// <param name="vParentFrm">主窗体 (this.MdiParent)</param>
  712. /// <param name="vKey">一般为窗体的全称 + 配置信息(可无)</param>
  713. /// <param name="vAssemblyName">程序集名</param>
  714. /// <param name="vClassName">窗体的全称</param>
  715. /// <param name="vCaption">窗体的中文名</param>
  716. public static void OpenOtherAssemblyForm(Form vParentFrm, string vKey, string vAssemblyName,
  717. string vClassName, string vCaption)
  718. {
  719. //检查窗体是否已经打开
  720. foreach (Form mdiChild in vParentFrm.MdiChildren)
  721. {
  722. if ((mdiChild as FrmBase).Key == vKey)
  723. {
  724. mdiChild.Activate();
  725. return;
  726. }
  727. }
  728. //实例化窗体并打开
  729. try
  730. {
  731. Assembly baseFormAssembly = Assembly.Load(vAssemblyName);
  732. Type type = baseFormAssembly.GetType(vClassName);
  733. System.Diagnostics.Debug.Assert(type.IsSubclassOf(typeof(FrmBase)));
  734. FrmBase form = Activator.CreateInstance(type, true) as FrmBase;
  735. form.MdiParent = vParentFrm;
  736. form.Text = vCaption;
  737. form.Key = vKey;
  738. form.Show();
  739. }
  740. catch (Exception ex)
  741. {
  742. Console.WriteLine(ex.Message);
  743. }
  744. }
  745. ///// <summary>
  746. ///// 获取当前班次
  747. ///// </summary>
  748. ///// <returns></returns>
  749. //public static string GetCurrentClass(RemotingHelp RemotingHelp)
  750. //{
  751. // string szOut = "";
  752. // CallingMessage par = new CallingMessage();
  753. // par.ServerName = "lgJobMgt";
  754. // par.AssemblyName = "Core.LgMes.Server.lgJobMgt";
  755. // par.ClassName = "Core.LgMes.Server.lgJobMgt.classCommonModule";
  756. // par.MethodName = "GetCurrDuty";
  757. // par.args = null;
  758. // object obj = ClientCommon._RemotingHelp.ExecuteMethod(par, out szOut);
  759. // if (szOut == "" && obj != null)
  760. // {
  761. // return obj as string;
  762. // }
  763. // return "";
  764. //}
  765. /// <summary>
  766. /// 班次转换
  767. /// </summary>
  768. /// <returns></returns>
  769. public static string ClassConvert(string szClassID)
  770. {
  771. if (szClassID.Trim() == "")
  772. return "";
  773. if (szClassID.Length == 2)
  774. szClassID = szClassID.Substring(1);
  775. string szClassName = "";
  776. switch (szClassID)
  777. {
  778. case "1":
  779. szClassName = "甲";
  780. break;
  781. case "2":
  782. szClassName = "乙";
  783. break;
  784. case "3":
  785. szClassName = "丙";
  786. break;
  787. case "4":
  788. szClassName = "丁";
  789. break;
  790. default:
  791. break;
  792. }
  793. return szClassName;
  794. }
  795. /// <summary>
  796. /// 时间计算返回分
  797. /// </summary>
  798. /// <param name="startTime"></param>
  799. /// <param name="endTime"></param>
  800. /// <returns></returns>
  801. public static int caculateTime(DateTime startTime, DateTime endTime)
  802. {
  803. int lStayDuration = 0;
  804. TimeSpan odtSpan;
  805. if (endTime > startTime)
  806. {
  807. odtSpan = endTime - startTime;
  808. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
  809. }
  810. else if (startTime != endTime)
  811. {
  812. if (startTime > DateTime.Now)
  813. lStayDuration = 0;
  814. else
  815. {
  816. odtSpan = DateTime.Now - startTime;
  817. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
  818. }
  819. }
  820. return lStayDuration;
  821. }
  822. /// <summary>
  823. /// 时间计算返回秒
  824. /// </summary>
  825. /// <param name="startTime"></param>
  826. /// <param name="endTime"></param>
  827. /// <returns></returns>
  828. public static int caculateTimeSeconds(DateTime startTime, DateTime endTime)
  829. {
  830. int lStayDuration = 0;
  831. TimeSpan odtSpan;
  832. if (endTime > startTime)
  833. {
  834. odtSpan = endTime - startTime;
  835. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalSeconds));
  836. }
  837. else if (startTime != endTime)
  838. {
  839. if (startTime > DateTime.Now)
  840. lStayDuration = 0;
  841. else
  842. {
  843. odtSpan = DateTime.Now - startTime;
  844. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalSeconds));
  845. }
  846. }
  847. return lStayDuration;
  848. }
  849. /// <summary>
  850. /// 获取两个时间段的差值
  851. /// </summary>
  852. /// <param name="startTime"></param>
  853. /// <param name="endTime"></param>
  854. /// <returns></returns>
  855. public static int JudgeTime(DateTime startTime, DateTime endTime)
  856. {
  857. int lStayDuration = 0;
  858. TimeSpan odtSpan;
  859. if (endTime > startTime)
  860. {
  861. odtSpan = endTime - startTime;
  862. lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
  863. }
  864. return lStayDuration;
  865. }
  866. /// <summary>
  867. /// 将ultragrid的数据导出到Excel中
  868. /// </summary>
  869. /// <param name="ulGrid">要导出数据的ulGrid名称</param>
  870. /// <param name="sFileName">Excel文件名</param>
  871. public static void ulGridToExcel(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid, string sFileName)
  872. {
  873. try
  874. {
  875. Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter ulGridExt = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
  876. //ulGridExt.CellExporting += new Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventHandler(ultraGridExcelExporter1_CellExporting);
  877. System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  878. if (ulGrid.Rows.Count == 0)
  879. {
  880. MessageBox.Show("没有数据!", "提示");
  881. return;
  882. }
  883. saveFileDialog1.FileName = sFileName + DateTime.Now.ToString("yyMMdd");
  884. saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
  885. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  886. {
  887. string sFullName = saveFileDialog1.FileName;
  888. ulGridExt.Export(ulGrid, sFullName);
  889. ProcessStartInfo p = new ProcessStartInfo(sFullName);
  890. p.WorkingDirectory = Path.GetDirectoryName(sFullName);
  891. Process.Start(p);
  892. }
  893. }
  894. catch (Exception ex)
  895. {
  896. MessageBox.Show("导出失败,原因:" + ex.Message);
  897. }
  898. }
  899. /// <summary>
  900. /// 将ultragrid的数据导出到Excel中
  901. /// </summary>
  902. /// <param name="ulGridList">ulGrid组</param>
  903. /// <param name="sheetNameList">顺序对应的Sheet名称</param>
  904. /// <param name="sFileName">Excel文件名</param>
  905. public static void ulGridToExcel(ArrayList ulGridList, ArrayList sheetNameList, string sFileName)
  906. {
  907. try
  908. {
  909. Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter ulGridExt = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
  910. //ulGridExt.CellExporting += new Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventHandler(ultraGridExcelExporter1_CellExporting);
  911. System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  912. saveFileDialog1.FileName = sFileName + DateTime.Now.ToString("yyMMdd") + ".xls";
  913. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  914. {
  915. string sFullName = saveFileDialog1.FileName;
  916. Infragistics.Excel.Workbook workbook1 = new Infragistics.Excel.Workbook();
  917. for (int i = 0; i < ulGridList.Count; i++)
  918. {
  919. Infragistics.Win.UltraWinGrid.UltraGrid itemGrid = ulGridList[i] as Infragistics.Win.UltraWinGrid.UltraGrid;
  920. string sheetname = (sheetNameList.Count > i && sheetNameList[i].ToString() != "")
  921. ? sheetNameList[i].ToString() : ("sheet" + i.ToString());
  922. workbook1.Worksheets.Add(sheetname);
  923. //没有数据时会出错,所以没有数据的面不不调用
  924. if (itemGrid.Rows.Count > 0)
  925. ulGridExt.Export(itemGrid, workbook1.Worksheets[i]);
  926. }
  927. workbook1.Save(sFullName);
  928. }
  929. }
  930. catch (Exception ex)
  931. {
  932. MessageBox.Show("导出失败,原因:" + ex.Message);
  933. }
  934. }
  935. ///// <summary>
  936. ///// 当Grid导出EXCEL时,列字段中的值为编码时,将编码转换成中文
  937. ///// </summary>
  938. ///// <param name="sender"></param>
  939. ///// <param name="e"></param>
  940. //public static void ultraGridExcelExporter1_CellExporting(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventArgs e)
  941. //{
  942. // try
  943. // {
  944. // if (e.GridColumn.RowLayoutColumnInfo.LabelPosition == Infragistics.Win.UltraWinGrid.LabelPosition.LabelOnly)
  945. // {
  946. // e.Cancel = true;
  947. // }
  948. // if (e.GridColumn.EditorControl != null || e.GridColumn.ValueList != null)
  949. // {
  950. // e.Value = e.GridRow.GetCellText(e.GridColumn);
  951. // }
  952. // }
  953. // catch
  954. // {
  955. // }
  956. //}
  957. /// <summary>
  958. /// 清除Grid的列过滤
  959. /// </summary>
  960. /// <param name="ulGrid">Grid名称</param>
  961. public static void ClearUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  962. {
  963. ulGrid.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();
  964. }
  965. /// <summary>
  966. /// 增加Grid的列过滤
  967. /// </summary>
  968. /// <param name="ulGrid"></param>
  969. public static void AddUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  970. {
  971. ulGrid.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
  972. }
  973. //查找treeView结点的父结点
  974. public static TreeNode findParentNode(TreeNode preLvlNode, string parentKey)
  975. {
  976. if (preLvlNode.Name == parentKey) return preLvlNode;
  977. foreach (TreeNode node in preLvlNode.Nodes)
  978. {
  979. if (node.Name.Length <= parentKey.Length)
  980. {
  981. if (node.Nodes.Count > 0)
  982. {
  983. TreeNode tNode = findParentNode(node, parentKey);
  984. if (tNode != null)
  985. return tNode;
  986. }
  987. else
  988. if (node.Name == parentKey)
  989. return node;
  990. }
  991. else
  992. return null;
  993. }
  994. return null;
  995. }
  996. }
  997. public class ValueListItem
  998. {
  999. string _id = "";
  1000. string _name = "";
  1001. public string ID
  1002. {
  1003. get { return _id; }
  1004. set { _id = value; }
  1005. }
  1006. public string Name
  1007. {
  1008. get { return _name; }
  1009. set { _name = value; }
  1010. }
  1011. public ValueListItem(string sID, string sName)
  1012. {
  1013. _id = sID;
  1014. _name = sName;
  1015. }
  1016. public override string ToString()
  1017. {
  1018. return _name;
  1019. }
  1020. }