Utility.cs 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:Utility.cs
  5. * 2.功能描述:系统各种常用函数和方法类
  6. * 3.函数/方法列表:
  7. * A.IsNull:判断空值
  8. * B.EnableFormCloseBox:设定窗体的关闭按钮是否有效
  9. * C.IsValidDate:判断是否是合法的日期
  10. * D.ChineseToPinyinCap:根据输入汉字,转化成汉字首字母串
  11. * 编辑履历:
  12. * 作者 日期 版本 修改内容
  13. * 欧阳涛 2012/06/07 1.00 新建
  14. * 王文君 2012/07/10 1.00 修改 添加IsUnique方法
  15. * 欧阳涛 2012/07/19 1.00 增加了GetBytesByFilePath()方法,
  16. * 根据文件路径返回文件字节流
  17. *******************************************************************************/
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Data;
  22. using System.Drawing;
  23. using System.Drawing.Imaging;
  24. using System.IO;
  25. using System.Runtime.InteropServices;
  26. using System.Runtime.Serialization.Formatters.Binary;
  27. using System.Text;
  28. using System.Text.RegularExpressions;
  29. using System.Windows.Forms;
  30. using Dongke.IBOSS.PRD.Basics.BaseResources;
  31. using Newtonsoft.Json.Linq;
  32. namespace Dongke.IBOSS.PRD.Basics.Library
  33. {
  34. /// <summary>
  35. /// 系统各种常用函数和方法类
  36. /// </summary>
  37. public static class Utility
  38. {
  39. #region IsDirty
  40. public static bool IsDirty(DataSet dataSet)
  41. {
  42. if (dataSet == null)
  43. {
  44. return false;
  45. }
  46. foreach (DataTable table in dataSet.Tables)
  47. {
  48. foreach (DataRow row in table.Rows)
  49. {
  50. row.EndEdit();
  51. }
  52. }
  53. DataSet changes = dataSet.GetChanges();
  54. if (changes == null)
  55. {
  56. return false;
  57. }
  58. foreach (DataTable table in changes.Tables)
  59. {
  60. if (IsDirty(table))
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. public static bool IsDirty(DataTable table)
  68. {
  69. if (table == null)
  70. {
  71. return false;
  72. }
  73. if (table.GetChanges(DataRowState.Added) != null)
  74. {
  75. return true;
  76. }
  77. if (table.GetChanges(DataRowState.Deleted) != null)
  78. {
  79. return true;
  80. }
  81. DataTable changes = table.GetChanges(DataRowState.Modified);
  82. if (changes != null)
  83. {
  84. foreach (DataRow row in changes.Rows)
  85. {
  86. if (IsDirty(row))
  87. {
  88. return true;
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. public static bool IsDirty(DataRow row)
  95. {
  96. if (row == null)
  97. {
  98. return false;
  99. }
  100. if (row.RowState == DataRowState.Added
  101. || row.RowState == DataRowState.Deleted)
  102. {
  103. return true;
  104. }
  105. if (row.RowState == DataRowState.Modified)
  106. {
  107. foreach (DataColumn column in row.Table.Columns)
  108. {
  109. if (column.ReadOnly)
  110. {
  111. continue;
  112. }
  113. if (!Utility.Compare(row[column, DataRowVersion.Original],
  114. row[column, DataRowVersion.Current]))
  115. {
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. #endregion
  123. #region 申明API函数
  124. /// <summary>
  125. /// 写入INI文件
  126. /// </summary>
  127. /// <param name="section">节点名称[如[TypeName]]</param>
  128. /// <param name="key">键</param>
  129. /// <param name="val">值</param>
  130. /// <param name="filepath">文件路径</param>
  131. /// <returns></returns>
  132. [DllImport("kernel32")]
  133. private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
  134. /// <summary>
  135. /// 读取INI文件
  136. /// </summary>
  137. /// <param name="section">节点名称</param>
  138. /// <param name="key">键</param>
  139. /// <param name="def">值</param>
  140. /// <param name="retval">stringbulider对象</param>
  141. /// <param name="size">字节大小</param>
  142. /// <param name="filePath">文件路径</param>
  143. /// <returns></returns>
  144. [DllImport("kernel32")]
  145. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
  146. #endregion
  147. #region 成员变量
  148. // 获取屏幕DPI用
  149. private static readonly Control _control;
  150. // 图形
  151. private static readonly Graphics _graphics;
  152. /// <summary>
  153. /// 毫米和像素的转换系数
  154. /// </summary>
  155. private const float MILLIMETER_PER_INCH = 25.4f;
  156. /// <summary>
  157. /// 磅和像素的转换系数
  158. /// </summary>
  159. private const float POINT_PER_INCH = 72f;
  160. /// <summary>
  161. /// 屏幕的DPI
  162. /// </summary>
  163. private static float SCREEN_DPI;
  164. #endregion
  165. #region 构造函数
  166. static Utility()
  167. {
  168. _control = new Control();
  169. _graphics = _control.CreateGraphics();
  170. SCREEN_DPI = _graphics.DpiX;
  171. }
  172. #endregion
  173. #region FindWindow()
  174. [DllImport("user32.dll")]
  175. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  176. #endregion
  177. #region WakeupWindow()
  178. public static bool WakeupWindow(IntPtr hWnd)
  179. {
  180. if (hWnd == System.IntPtr.Zero)
  181. {
  182. return false;
  183. }
  184. if (Win32.IsIconic(hWnd) || !Win32.IsWindowVisible(hWnd))
  185. {
  186. Win32.ShowWindowAsync(hWnd, Win32.SW_RESTORE);
  187. }
  188. Win32.SetForegroundWindow(hWnd);
  189. return true;
  190. }
  191. #endregion
  192. #region 基本共通函数
  193. /// <summary>
  194. /// 空值判断
  195. /// </summary>
  196. /// <param name="value">判断对象</param>
  197. /// <returns>
  198. /// True:是空值
  199. /// False:不是空值
  200. /// </returns>
  201. public static bool IsNull(object value)
  202. {
  203. if (value == null || value == DBNull.Value)
  204. {
  205. return true;
  206. }
  207. return false;
  208. }
  209. /// <summary>
  210. /// 过滤sql语句中非法的字符,并替换
  211. /// </summary>
  212. /// <param name="value">替换前的sql语句</param>
  213. /// <returns>替换后的sql语句字符</returns>
  214. public static string SelectFilterLike(string value)
  215. {
  216. if (string.IsNullOrEmpty(value))
  217. {
  218. return string.Empty;
  219. }
  220. return value.Replace(@"'", @"''").Replace(@"[", @"[[]")
  221. .Replace(@"_", @"[_]").Replace(@"%", @"[%]").Replace(@"*", @"[*]");
  222. }
  223. /// <summary>
  224. /// 控制窗体的关闭按钮是否能用
  225. /// </summary>
  226. /// <param name="hWnd">窗体句柄</param>
  227. /// <param name="bEnable">True:可用 False:不可用</param>
  228. /// <returns>
  229. /// 设置成功与否
  230. /// </returns>
  231. public static bool EnableFormCloseBox(IntPtr hWnd, bool bEnable)
  232. {
  233. if (hWnd == System.IntPtr.Zero)
  234. {
  235. return false;
  236. }
  237. IntPtr hMenu = Win32.GetSystemMenu(hWnd, 0);
  238. if (hMenu == System.IntPtr.Zero)
  239. {
  240. return false;
  241. }
  242. if (bEnable)
  243. {
  244. Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE, Win32.MF_ENABLED);
  245. }
  246. else
  247. {
  248. Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE,
  249. Win32.MF_DISABLED | Win32.MF_GRAYED);
  250. }
  251. return true;
  252. }
  253. /// <summary>
  254. /// 判断是否是正确的日期格式
  255. /// </summary>
  256. /// <param name="strDateTime">被判断的日期</param>
  257. /// <param name="dateTime">返回被格式化的日期</param>
  258. /// <returns>
  259. /// True:日期格式
  260. /// False:不是日期格式
  261. /// </returns>
  262. public static bool IsValidDate(string strDateTime, out DateTime? dateTime)
  263. {
  264. DateTime returnDatetime;
  265. if (DateTime.TryParse(strDateTime, out returnDatetime))
  266. {
  267. dateTime = returnDatetime;
  268. return true;
  269. }
  270. else
  271. {
  272. dateTime = null;
  273. return false;
  274. }
  275. }
  276. /// <summary>
  277. /// 取得显示顺序的最大值
  278. /// </summary>
  279. /// <param name="table">数据表</param>
  280. /// <param name="maxValName">列名</param>
  281. /// <returns>
  282. /// 该数据表里的最大显示顺序
  283. /// </returns>
  284. public static int GetMaxValue(DataTable table, string maxValName)
  285. {
  286. if (table == null)
  287. {
  288. return -1;
  289. }
  290. if (table.Columns.Contains(maxValName))
  291. {
  292. DataRow[] rows = table.Select("0 <= " + maxValName, maxValName + " DESC");
  293. if (rows == null || rows.Length == 0)
  294. {
  295. return -1;
  296. }
  297. object value = rows[0][maxValName];
  298. if (value == null)
  299. {
  300. return -1;
  301. }
  302. int maxValue = int.Parse(value.ToString());
  303. if (maxValue == short.MaxValue)
  304. {
  305. maxValue--;
  306. }
  307. return maxValue;
  308. }
  309. else
  310. {
  311. return -1;
  312. //DataRow[] rows = table.Select("0 <= " + maxValName,maxValName + "DESC");
  313. //object value = rows[0][maxValName];
  314. //int maxValue = int.Parse(value.ToString());
  315. //return maxValue;
  316. }
  317. }
  318. /// <summary>
  319. /// 取得数据表里的最大编码值
  320. /// </summary>
  321. /// <param name="table">数据表</param>
  322. /// <param name="maxValName">列名</param>
  323. /// <returns>该数据表里的最大编码值(string类型)</returns>
  324. /// <remarks>
  325. /// 2012.07.10 余再伟 创建
  326. /// </remarks>
  327. public static string GetStringMaxValue(DataTable table, string maxValName)
  328. {
  329. // 表为空时候直接返回空值
  330. if (table == null)
  331. {
  332. return string.Empty;
  333. }
  334. // 表中包含该列名的时候返回该数据表的最大编码值
  335. if (table.Columns.Contains(maxValName))
  336. {
  337. DataRow[] rows = table.Select("'0' <= " + maxValName, maxValName + " DESC");
  338. if (rows == null || rows.Length == 0)
  339. {
  340. return string.Empty;
  341. }
  342. object value = rows[0][maxValName];
  343. if (value == null)
  344. {
  345. return string.Empty;
  346. }
  347. string maxValue = value.ToString();
  348. return maxValue;
  349. }
  350. else
  351. {
  352. return string.Empty;
  353. }
  354. }
  355. /// <summary>
  356. /// 校验值是否重复
  357. /// </summary>
  358. /// <param name="curCellValue">当前Column的值</param>
  359. /// <param name="curRow">当前行的行号</param>
  360. /// <param name="dataGridView">所在的DataGridView</param>
  361. /// <param name="uniqueValue">比较值</param>
  362. /// <returns>是否存在</returns>
  363. public static bool IsUnique(string curCellValue, int curRow, DataGridView dataGridView, string uniqueValue)
  364. {
  365. foreach (DataGridViewRow rowItem in dataGridView.Rows)
  366. {
  367. if (rowItem.Index == curRow)
  368. {
  369. continue;
  370. }
  371. // 字典名称
  372. if (rowItem.Cells[uniqueValue].Value != null)
  373. {
  374. if (rowItem.Cells[uniqueValue].Value.ToString().Trim().
  375. Equals(curCellValue.Trim()))
  376. {
  377. return false;
  378. }
  379. }
  380. }
  381. return true;
  382. }
  383. /// <summary>
  384. /// 根据输入的汉字,返回所有汉字拼音的首字母串
  385. /// </summary>
  386. /// <param name="chineseString">输入的汉字</param>
  387. /// <returns>汉字拼音的首字母串</returns>
  388. public static string ChineseToPinyinCap(string chineseString)
  389. {
  390. string pinyinCapString = ""; // 转换后的拼音首字母字符串
  391. // 每个汉字存储需要两个字节
  392. byte[] ZW = new byte[2];
  393. long ChineseStr_int;
  394. string CharStr;
  395. string ChinaStr = "";
  396. chineseString = ToDBC(chineseString);
  397. for (int i = 0; i <= chineseString.Length - 1; i++)
  398. {
  399. // 取出一个汉字
  400. CharStr = chineseString.Substring(i, 1).ToString();
  401. // 得到汉字的字节表示
  402. ZW = System.Text.Encoding.Default.GetBytes(CharStr);
  403. // 得到汉字符的字节数组
  404. if (ZW.Length == 2)
  405. {
  406. int i1 = (short)(ZW[0]);
  407. int i2 = (short)(ZW[1]);
  408. ChineseStr_int = i1 * 256 + i2;
  409. #region 参数
  410. // table of the constant list
  411. // 'A';      //45217..45252
  412. // 'B';      //45253..45760
  413. // 'C';      //45761..46317
  414. // 'D';      //46318..46825
  415. // 'E';      //46826..47009
  416. // 'F';      //47010..47296
  417. // 'G';      //47297..47613
  418. // 'H';      //47614..48118
  419. // 'J';      //48119..49061
  420. // 'K';      //49062..49323
  421. // 'L';      //49324..49895
  422. // 'M';      //49896..50370
  423. // 'N';      //50371..50613
  424. // 'O';      //50614..50621
  425. // 'P';      //50622..50905
  426. // 'Q';      //50906..51386
  427. // 'R';      //51387..51445
  428. // 'S';      //51446..52217
  429. // 'T';      //52218..52697
  430. //没有U,V
  431. // 'W';      //52698..52979
  432. // 'X';      //52980..53640
  433. // 'Y';      //53689..54480
  434. // 'Z';      //54481..55289
  435. #endregion
  436. if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
  437. {
  438. ChinaStr = "A";
  439. }
  440. else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
  441. {
  442. ChinaStr = "B";
  443. }
  444. else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
  445. {
  446. ChinaStr = "C";
  447. }
  448. else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
  449. {
  450. ChinaStr = "D";
  451. }
  452. else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
  453. {
  454. ChinaStr = "E";
  455. }
  456. else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
  457. {
  458. ChinaStr = "F";
  459. }
  460. else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
  461. {
  462. ChinaStr = "G";
  463. }
  464. else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
  465. {
  466. ChinaStr = "H";
  467. }
  468. else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
  469. {
  470. ChinaStr = "J";
  471. }
  472. else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
  473. {
  474. ChinaStr = "K";
  475. }
  476. else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
  477. {
  478. ChinaStr = "L";
  479. }
  480. else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
  481. {
  482. ChinaStr = "M";
  483. }
  484. else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
  485. {
  486. ChinaStr = "N";
  487. }
  488. else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
  489. {
  490. ChinaStr = "O";
  491. }
  492. else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
  493. {
  494. ChinaStr = "P";
  495. }
  496. else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
  497. {
  498. ChinaStr = "Q";
  499. }
  500. else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
  501. {
  502. ChinaStr = "R";
  503. }
  504. else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
  505. {
  506. ChinaStr = "S";
  507. }
  508. else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
  509. {
  510. ChinaStr = "T";
  511. }
  512. else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
  513. {
  514. ChinaStr = "W";
  515. }
  516. else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
  517. {
  518. ChinaStr = "X";
  519. }
  520. else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
  521. {
  522. ChinaStr = "Y";
  523. }
  524. else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
  525. {
  526. ChinaStr = "Z";
  527. }
  528. else
  529. {
  530. ChinaStr = CharStr;
  531. }
  532. pinyinCapString = pinyinCapString + ChinaStr;
  533. }
  534. else
  535. {
  536. pinyinCapString = pinyinCapString + CharStr.ToUpper();
  537. }
  538. }
  539. return pinyinCapString;
  540. }
  541. #endregion
  542. #region 半角全角转换
  543. /// <summary>
  544. /// 转半角的函数(DBC)
  545. /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  546. /// 全角空格为12288,半角空格为32
  547. /// </summary>
  548. /// <param name="inputString">任意字符串</param>
  549. /// <returns>半角字符串</returns>
  550. public static string ToDBC(string inputString)
  551. {
  552. char[] c = inputString.ToCharArray();
  553. for (int i = 0; i < c.Length; i++)
  554. {
  555. if (c[i] == 12288)
  556. {
  557. c[i] = (char)32;
  558. continue;
  559. }
  560. if (c[i] > 65280 && c[i] < 65375)
  561. {
  562. c[i] = (char)(c[i] - 65248);
  563. }
  564. }
  565. return new String(c);
  566. }
  567. /// <summary>
  568. /// 转全角的函数(SBC)
  569. /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  570. /// 全角空格为12288,半角空格为32
  571. /// </summary>
  572. /// <param name="inputString">任意字符串</param>
  573. /// <returns>全角字符串</returns>
  574. public static string ToSBC(string inputString)
  575. {
  576. // 半角转全角:
  577. char[] c = inputString.ToCharArray();
  578. for (int i = 0; i < c.Length; i++)
  579. {
  580. if (c[i] == 32)
  581. {
  582. c[i] = (char)12288;
  583. continue;
  584. }
  585. if (c[i] < 127)
  586. c[i] = (char)(c[i] + 65248);
  587. }
  588. return new String(c);
  589. }
  590. #endregion
  591. #region 正则判断数据合法性
  592. /// <summary>
  593. /// 验证邮箱格式
  594. /// </summary>
  595. /// <param name="email">要验证的邮箱地址</param>
  596. /// <returns>
  597. /// True:邮箱格式正确
  598. /// False:邮箱格式错误
  599. /// </returns>
  600. public static bool IsValidEmail(string email)
  601. {
  602. if (!Regex.IsMatch(email,
  603. @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"))
  604. {
  605. return false;
  606. }
  607. else
  608. {
  609. return true;
  610. }
  611. }
  612. ///// <summary>
  613. ///// 验证身份证输入是否正确
  614. ///// </summary>
  615. ///// <param name="IdCard">要验证的身份证号</param>
  616. ///// <returns>
  617. ///// True:身份证格式正确
  618. ///// False:身份证格式错误
  619. ///// </returns>
  620. //public static bool IsIDcard(string IdCard)
  621. //{
  622. // bool result = false;
  623. // if (IdCard.Length <= 15)
  624. // {
  625. // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING15);
  626. // }
  627. // else
  628. // {
  629. // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING);
  630. // }
  631. // return result;
  632. //}
  633. /// <summary>
  634. /// 判断身份证号是否合法(对外部公开)
  635. /// </summary>
  636. /// <param name="strCardID">身份证号(旧/新)</param>
  637. /// <returns>false:不合法 true:合法</returns>
  638. public static bool IsIDcard(string strCardID)
  639. {
  640. strCardID = strCardID.ToLower();
  641. // 验证身份证的位数
  642. string strRegex = @"(\d{15}$)|(\d{17}(\d|x)$)";
  643. if (!Regex.IsMatch(strCardID, strRegex))
  644. {
  645. return false;
  646. }
  647. // 身份证数据有效性验证
  648. string strNewCardID = strCardID;
  649. if (strCardID.Length == 15)
  650. {
  651. strNewCardID = Convert15To18(strCardID);
  652. }
  653. if (!CheckCardIDInfo(strNewCardID))
  654. {
  655. return false;
  656. }
  657. return true;
  658. }
  659. /// <summary>
  660. /// 判断身份证号是否合法
  661. /// </summary>
  662. /// <param name="strCardID">新身份证号</param>
  663. /// <returns>false:不合法 true:合法</returns>
  664. private static bool CheckCardIDInfo(string strCardID)
  665. {
  666. string[] aCity = new string[] { null, null, null, null, null, null, null, null, null,
  667. null, null, "北京", "天津 ", "河北", "山西", "内蒙古", null, null, null, null,
  668. null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海",
  669. "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南",
  670. "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川",
  671. "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃",
  672. "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null,
  673. null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null,
  674. null, null, null, "国外" };
  675. strCardID = strCardID.ToLower();
  676. strCardID = strCardID.Replace("x", "a");
  677. try
  678. {
  679. if (aCity[int.Parse(strCardID.Substring(0, 2))] == null)
  680. {
  681. return false;
  682. }
  683. DateTime.Parse(strCardID.Substring(6, 4) + "-"
  684. + strCardID.Substring(10, 2) + "-"
  685. + strCardID.Substring(12, 2));
  686. }
  687. catch
  688. {
  689. return false;
  690. }
  691. double iSum = 0;
  692. for (int i = 17; i >= 0; i--)
  693. {
  694. iSum += (System.Math.Pow(2, i) % 11) * int.Parse(strCardID[17 - i].ToString(),
  695. System.Globalization.NumberStyles.HexNumber);
  696. }
  697. if (iSum % 11 != 1)
  698. {
  699. return false;
  700. }
  701. return true;
  702. }
  703. /// <summary>
  704. /// 从老身份证升位到新身份证
  705. /// </summary>
  706. /// <param name="strCardID">老身份证号码</param>
  707. /// <returns>新身份证号码</returns>
  708. private static string Convert15To18(string strCardID)
  709. {
  710. int intN = 0;
  711. // 加权因子常数
  712. int[] intW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
  713. // 校验码常数
  714. string strLastCode = "10X98765432";
  715. // 新身份证号
  716. string strNewCardID;
  717. strNewCardID = strCardID.Substring(0, 6);
  718. // 填在第6位及第7位上填上‘1’,‘9’两个数字
  719. strNewCardID += "19";
  720. strNewCardID += strCardID.Substring(6, 9);
  721. // 进行加权求和
  722. for (int i = 0; i < 17; i++)
  723. {
  724. intN += int.Parse(strNewCardID.Substring(i, 1)) * intW[i];
  725. }
  726. // 取模运算,得到模值
  727. int intY = intN % 11;
  728. // 从strLastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
  729. strNewCardID += strLastCode.Substring(intY, 1);
  730. return strNewCardID;
  731. }
  732. /// <summary>
  733. /// 验证车牌号输入是否正确
  734. /// </summary>
  735. /// <param name="PlateNumber">要验证的车牌号</param>
  736. /// <returns>
  737. /// True:车牌号格式正确
  738. /// False:车牌号格式错误
  739. /// </returns>
  740. public static bool IsPlateNumber(string PlateNumber)
  741. {
  742. return Regex.IsMatch(PlateNumber, Constant.REGEX_PLATENUMBER_STRING);
  743. }
  744. /// <summary>
  745. /// 验证车架号输入是否正确
  746. /// </summary>
  747. /// <param name="PlateNumber">要验证的车架号</param>
  748. /// <returns>
  749. /// True:车架号格式正确
  750. /// False:车架号格式错误
  751. /// </returns>
  752. public static bool IsVehicleNumber(string VehicleNumber)
  753. {
  754. return Regex.IsMatch(VehicleNumber, Constant.REGEX_VEHICLENUM_STRING);
  755. }
  756. /// <summary>
  757. /// 验证公司网址输入格式是否正确
  758. /// </summary>
  759. /// <param name="http">要验证的网址</param>
  760. /// True:网址格式正确
  761. /// False:网址格式错误
  762. /// <returns></returns>
  763. public static bool IsValidHttp(string http)
  764. {
  765. if (!Regex.IsMatch(http, Constant.REGEX_HTTP_STRING))
  766. {
  767. return false;
  768. }
  769. else
  770. {
  771. return true;
  772. }
  773. }
  774. /// <summary>
  775. /// 验证输入字符串是否为数字类型(包括带小数的数字)
  776. /// </summary>
  777. /// <param name="value">要验证的字符串</param>
  778. /// <returns>
  779. /// True:字符串是数字
  780. /// False:字符串不是数字
  781. /// </returns>
  782. public static bool IsNumeric(string value)
  783. {
  784. return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
  785. }
  786. /// <summary>
  787. /// 验证输入字符串是否为合法时间(HH:mm或者HH:mm:ss)
  788. /// </summary>
  789. /// <param name="value">要验证的字符串</param>
  790. /// <returns>
  791. /// True:字符串是合法时间(HH:mm或者HH:mm:ss)
  792. /// False:字符串不是合法时间(HH:mm或者HH:mm:ss)
  793. /// </returns>
  794. public static bool IsTime(string value)
  795. {
  796. return System.Text.RegularExpressions.Regex.IsMatch(value, @"^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$");
  797. }
  798. /// <summary>
  799. /// 验证输入字符串是否为合法日期(yyyy-MM-dd)
  800. /// </summary>
  801. /// <param name="value">要验证的字符串</param>
  802. /// <returns>
  803. /// True:字符串是合法日期(yyyy-MM-dd)
  804. /// False:字符串不是合法日期(yyyy-MM-dd)
  805. /// </returns>
  806. public static bool IsDate(string value)
  807. {
  808. return System.Text.RegularExpressions.Regex.IsMatch(value,
  809. @"^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
  810. }
  811. #endregion
  812. #region 二进制与文件之间转换
  813. /// <summary>
  814. /// 将文件转换成字节流
  815. /// </summary>
  816. /// <param name="filePath">文件的绝对路径</param>
  817. /// <returns>
  818. /// 文件的字节流
  819. /// </returns>
  820. public static byte[] GetBytesByFilePath(string filePath)
  821. {
  822. // 传入的值非法的情况下,返回NULL
  823. if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
  824. {
  825. return null;
  826. }
  827. // 读取文件到字节流中
  828. FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  829. BinaryReader binaryReader = new BinaryReader(fileStream);
  830. byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
  831. fileStream.Flush();
  832. fileStream.Close();
  833. return fileBytes;
  834. }
  835. /// <summary>
  836. /// 将二进制转换成文件
  837. /// </summary>
  838. /// <param name="filePath">文件存放的全路径</param>
  839. /// <param name="fileByte">二进制文件</param>
  840. /// <returns>
  841. /// True:保存成功
  842. /// False:保存失败
  843. /// </returns>
  844. public static bool BinaryToFile(string filePath, byte[] fileByte)
  845. {
  846. if (string.IsNullOrEmpty(filePath) || fileByte == null)
  847. {
  848. return false;
  849. }
  850. try
  851. {
  852. FileStream filestream = File.Create(filePath, fileByte.Length);
  853. filestream.Write(fileByte, 0, fileByte.Length);
  854. return true;
  855. }
  856. catch (Exception ex)
  857. {
  858. throw ex;
  859. }
  860. }
  861. #endregion
  862. #region 图片与二进制之间转换
  863. /// <summary>
  864. /// 将图片文件序列化成二进制流
  865. /// </summary>
  866. /// <param name="imagePath">文件路径</param>
  867. /// <returns>二进制流文件</returns>
  868. public static MemoryStream ImageToBinary(string imagePath)
  869. {
  870. if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
  871. {
  872. return null;
  873. }
  874. try
  875. {
  876. // 将图片文件序列化成二进制流文件
  877. Image image = new Bitmap(imagePath);
  878. MemoryStream memorystream = new MemoryStream();
  879. BinaryFormatter formatter = new BinaryFormatter();
  880. formatter.Serialize(memorystream, image);
  881. return memorystream;
  882. }
  883. catch (Exception ex)
  884. {
  885. throw ex;
  886. }
  887. }
  888. /// <summary>
  889. /// 将二进制流反序列化成图片文件
  890. /// </summary>
  891. /// <param name="memoryStream">二进制流文件</param>
  892. /// <returns>图片文件</returns>
  893. public static Image StreamToImage(MemoryStream memoryStream)
  894. {
  895. if (memoryStream == null)
  896. {
  897. return null;
  898. }
  899. try
  900. {
  901. // 将流文件反序列化成图片文件
  902. memoryStream.Position = 0;
  903. BinaryFormatter formatter = new BinaryFormatter();
  904. return (Image)formatter.Deserialize(memoryStream);
  905. }
  906. catch (Exception ex)
  907. {
  908. throw ex;
  909. }
  910. }
  911. /// <summary>
  912. /// 将二进制转换成图片文件
  913. /// </summary>
  914. /// <param name="imageBinary">二进制</param>
  915. /// <returns>image</returns>
  916. public static Image BinaryToImage(byte[] imageBinary)
  917. {
  918. if (imageBinary == null)
  919. {
  920. return null;
  921. }
  922. try
  923. {
  924. MemoryStream memoryStream = new MemoryStream(imageBinary);
  925. return Image.FromStream(memoryStream);
  926. }
  927. catch (Exception ex)
  928. {
  929. throw ex;
  930. }
  931. }
  932. /// <summary>
  933. /// 根据Image对象转换成二进制
  934. /// </summary>
  935. /// <param name="image">image对象</param>
  936. /// <returns>
  937. /// 二进制数组
  938. /// </returns>
  939. public static byte[] ImageToByteArray(Image image)
  940. {
  941. if (image == null)
  942. {
  943. return null;
  944. }
  945. try
  946. {
  947. MemoryStream memoryStream = new MemoryStream();
  948. image.Save(memoryStream, ImageFormat.Jpeg);
  949. return memoryStream.ToArray();
  950. }
  951. catch (Exception ex)
  952. {
  953. throw ex;
  954. }
  955. }
  956. #endregion
  957. #region Compare比较函数
  958. [Flags]
  959. public enum CompareType
  960. {
  961. None = 0,
  962. // DBNull.Value值与null值视为同一值
  963. DBNullAsNull = 1,
  964. // string.Empty值null值视为同一值
  965. EmptyAsNull = 2,
  966. // 全部都视为同一值
  967. All = DBNullAsNull | EmptyAsNull
  968. }
  969. /// <summary>
  970. /// 两个值比较函数
  971. /// </summary>
  972. /// <param name="value1">值1</param>
  973. /// <param name="value2">值2</param>
  974. /// <returns>
  975. /// True:两个值相等的情况
  976. /// False:两个值相等的以外情况
  977. /// </returns>
  978. public static bool Compare(object value1, object value2)
  979. {
  980. return Compare(value1, value2, CompareType.None);
  981. }
  982. /// <summary>
  983. /// 两个值比较函数
  984. /// </summary>
  985. /// <param name="value1">值1</param>
  986. /// <param name="value2">值2</param>
  987. /// <param name="compareFlag">比较方式</param>
  988. /// <returns>
  989. /// True:两个值相等的情况
  990. /// False:两个值相等的以外情况
  991. /// </returns>
  992. public static bool Compare(object value1, object value2, CompareType compareFlag)
  993. {
  994. bool isNull1 = (value1 == null);
  995. bool isNull2 = (value2 == null);
  996. if ((compareFlag & CompareType.DBNullAsNull) == CompareType.DBNullAsNull)
  997. {
  998. if (value1 == DBNull.Value)
  999. {
  1000. isNull1 = true;
  1001. }
  1002. if (value2 == DBNull.Value)
  1003. {
  1004. isNull2 = true;
  1005. }
  1006. }
  1007. if ((compareFlag & CompareType.EmptyAsNull) == CompareType.EmptyAsNull)
  1008. {
  1009. if (string.Empty.Equals(value1))
  1010. {
  1011. isNull1 = true;
  1012. }
  1013. if (string.Empty.Equals(value2))
  1014. {
  1015. isNull2 = true;
  1016. }
  1017. }
  1018. if (isNull1 && isNull2)
  1019. {
  1020. return true;
  1021. }
  1022. if (value1 == null || value2 == null)
  1023. {
  1024. return false;
  1025. }
  1026. else
  1027. {
  1028. return CompareSub(value1, value2);
  1029. }
  1030. }
  1031. /// <summary>
  1032. /// 两个值比较函数
  1033. /// </summary>
  1034. /// <param name="value1">值1</param>
  1035. /// <param name="value2">值2</param>
  1036. /// <returns>
  1037. /// True:两个值相等的情况
  1038. /// False:两个值相等的以外情况
  1039. /// </returns>
  1040. private static bool CompareSub(object value1, object value2)
  1041. {
  1042. // 数据类型不一样的情况下,返回false
  1043. if (value1.GetType().Equals(value2.GetType()) == false)
  1044. {
  1045. return false;
  1046. }
  1047. // 枚举比较的情况,不能使用Equals,需单独写方法比较
  1048. if (value1 is System.Collections.IEnumerable
  1049. && value2 is System.Collections.IEnumerable)
  1050. {
  1051. return CompareSub((System.Collections.IEnumerable)value1,
  1052. (System.Collections.IEnumerable)value2);
  1053. }
  1054. else
  1055. {
  1056. return value1.Equals(value2);
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// 两个枚举类型的比较
  1061. /// </summary>
  1062. /// <param name="enumerable1">值1</param>
  1063. /// <param name="enumerable2">值2</param>
  1064. /// <returns>
  1065. /// True:两个值相等的情况
  1066. /// False:两个值相等的以外情况
  1067. /// </returns>
  1068. private static bool CompareSub(System.Collections.IEnumerable enumerable1,
  1069. System.Collections.IEnumerable enumerable2)
  1070. {
  1071. System.Collections.IEnumerator e1 = enumerable1.GetEnumerator();
  1072. System.Collections.IEnumerator e2 = enumerable2.GetEnumerator();
  1073. while (e1.MoveNext())
  1074. {
  1075. if (!e2.MoveNext())
  1076. {
  1077. return false;
  1078. }
  1079. if (!e1.Current.Equals(e2.Current))
  1080. {
  1081. return false;
  1082. }
  1083. }
  1084. return !e2.MoveNext();
  1085. }
  1086. #endregion
  1087. #region 校验数字
  1088. /// <summary>
  1089. /// 校验数据是否符合要求
  1090. /// </summary>
  1091. /// <param name="number">待验证的参数</param>
  1092. /// <param name="numberName">待验证的参数名称</param>
  1093. /// <param name="type">校验的种类</param>
  1094. /// <returns>错误信息</returns>
  1095. /// <remarks>
  1096. /// 2012.10.8 周兴 新建
  1097. /// </remarks>
  1098. public static string IsValidNumber(string number, string numberName, byte type)
  1099. {
  1100. try
  1101. {
  1102. // 如果第一位是-,表面可能是负号,去掉之后进行校验
  1103. if (!string.IsNullOrEmpty(number))
  1104. {
  1105. if (number.IndexOf("-") == 0)
  1106. {
  1107. number = number.Substring(1);
  1108. }
  1109. }
  1110. string errorMsg = string.Empty;
  1111. string maxNumber = string.Empty;
  1112. string regex = string.Empty;
  1113. // 验证Numeric(16,6)
  1114. if (type == 1)
  1115. {
  1116. regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,6})?$";
  1117. maxNumber = "9999999999.999999";
  1118. }
  1119. // 验证Numeric(14,6)
  1120. else if (type == 2)
  1121. {
  1122. regex = @"^(0|([1-9][0-9]{0,7}))(\.[0-9]{0,6})?$";
  1123. maxNumber = "99999999.999999";
  1124. }
  1125. // 验证Numeric(12,6)
  1126. else if (type == 3)
  1127. {
  1128. regex = @"^(0|([1-9][0-9]{0,5}))(\.[0-9]{0,6})?$";
  1129. maxNumber = "999999.999999";
  1130. }
  1131. // 验证Numeric(5,2)
  1132. else if (type == 4)
  1133. {
  1134. regex = @"^(0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?$";
  1135. maxNumber = "999.99";
  1136. }
  1137. // 验证Numeric(5,2)(采购返利方式的返点率)
  1138. else if (type == 5)
  1139. {
  1140. regex = @"^((0|([1-9][0-9]{0,1}))(\.[0-9]{0,2})?|(100(\.0{0,2})?))$";
  1141. maxNumber = "100.00";
  1142. }
  1143. // 验证Numeric(6,2)(系统参数舍零金额限制最大值1000.00)
  1144. else if (type == 6)
  1145. {
  1146. regex = @"^((0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?|(1000(\.0{0,2})?))$";
  1147. maxNumber = "1000.00";
  1148. }
  1149. // 验证Numeric(12,2)(任务策略中任务金额限制)
  1150. else if (type == 7)
  1151. {
  1152. regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,2})?$";
  1153. maxNumber = "9999999999.99";
  1154. }
  1155. // 验证Numeric(6,2)(商品编码中费用分摊系数)
  1156. else if (type == 8)
  1157. {
  1158. regex = @"^(0|([1-9][0-9]{0,3}))(\.[0-9]{0,2})?$";
  1159. maxNumber = "9999.99";
  1160. }
  1161. // 验证Numeric(9,2)(CA员工业绩岗位考核工资基数)
  1162. else if (type == 9)
  1163. {
  1164. regex = @"^(0|([1-9][0-9]{0,6}))(\.[0-9]{0,2})?$";
  1165. maxNumber = "9999999.99";
  1166. }
  1167. // 验证Numeric(3,2)(CA员工业绩提成比率)
  1168. else if (type == 10)
  1169. {
  1170. regex = @"^(0|([0-9]))(\.[0-9]{0,2})?$";
  1171. maxNumber = "9.99";
  1172. }
  1173. // 验证Numeric(3,1)(CB员工业绩提成比率)
  1174. else if (type == 11)
  1175. {
  1176. regex = @"^(0|([1-9][0-9]{0,1}))(\.[0-9]{0,1})?$";
  1177. maxNumber = "99.9";
  1178. }
  1179. // 验证Numeric(7,2)(店长提成策略出库净额递增额)
  1180. else if (type == 12)
  1181. {
  1182. regex = @"^(0|([1-9][0-9]{0,4}))(\.[0-9]{0,2})?$";
  1183. maxNumber = "99999.99";
  1184. }
  1185. if (!Regex.IsMatch(number, regex))
  1186. {
  1187. //errorMsg = string.Format(Messages.MESSAGE_W004, numberName, maxNumber);
  1188. }
  1189. return errorMsg;
  1190. }
  1191. catch (Exception ex)
  1192. {
  1193. throw ex;
  1194. }
  1195. }
  1196. #endregion
  1197. #region 校验电话格式
  1198. /// <summary>
  1199. /// 校验电话是否符合格式
  1200. /// </summary>
  1201. /// <param name="telephone"></param>
  1202. /// <returns></returns>
  1203. public static bool IsValidTelephone(string telephone)
  1204. {
  1205. try
  1206. {
  1207. /**
  1208. * 手机号码
  1209. * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
  1210. * 联通:130,131,132,152,155,156,185,186
  1211. * 电信:133,1349,153,180,181,189
  1212. */
  1213. string mobile = "^1(3[0-9]|5[0-35-9]|8[012345-9])\\d{8}$";
  1214. /**
  1215. * 大陆地区固话及小灵通 区号和电话之间没有-
  1216. * 区号:010,020,021,022,023,024,025,027,028,029
  1217. * 号码:七位或八位
  1218. */
  1219. string phs = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
  1220. if (!Regex.IsMatch(telephone, mobile) && !Regex.IsMatch(telephone, phs))
  1221. {
  1222. return false;
  1223. }
  1224. return true;
  1225. }
  1226. catch (Exception ex)
  1227. {
  1228. throw ex;
  1229. }
  1230. }
  1231. #endregion
  1232. #region 验证规格是否符合计算面积格式
  1233. /// <summary>
  1234. /// 验证规格是否符合计算面积格式
  1235. /// </summary>
  1236. /// <param name="specification">规格</param>
  1237. /// <returns></returns>
  1238. public static bool IsValidAcreage(string specification)
  1239. {
  1240. try
  1241. {
  1242. string regex = @"^\d+\*\d+$";
  1243. if (Regex.IsMatch(specification, regex))
  1244. {
  1245. return true;
  1246. }
  1247. return false;
  1248. }
  1249. catch (Exception ex)
  1250. {
  1251. throw ex;
  1252. }
  1253. }
  1254. #endregion
  1255. #region 验证规格是否符合计算体积格式
  1256. /// <summary>
  1257. /// 验证规格是否符合计算体积格式
  1258. /// </summary>
  1259. /// <param name="specification">规格</param>
  1260. /// <returns></returns>
  1261. public static bool IsValidVolume(string specification)
  1262. {
  1263. try
  1264. {
  1265. string regex = @"^\d+\*\d+\*\d+$";
  1266. if (Regex.IsMatch(specification, regex))
  1267. {
  1268. return true;
  1269. }
  1270. return false;
  1271. }
  1272. catch (Exception ex)
  1273. {
  1274. throw ex;
  1275. }
  1276. }
  1277. #endregion
  1278. #region 长度与像素之间转换
  1279. /// <summary>
  1280. /// 1/100 英寸转换为毫米
  1281. /// </summary>
  1282. /// <remarks>
  1283. /// 表示倍率 100%
  1284. /// </remarks>
  1285. /// <param name="pixel">1/100 英寸</param>
  1286. /// <returns>毫米</returns>
  1287. public static float Inch100ToMillimeter(int inch100)
  1288. {
  1289. float mm = inch100 * MILLIMETER_PER_INCH / 100;
  1290. return mm;
  1291. }
  1292. /// <summary>
  1293. /// 1/100 英寸转换为毫米
  1294. /// </summary>
  1295. /// <remarks>
  1296. /// 表示倍率 100%
  1297. /// </remarks>
  1298. /// <param name="pixel">1/100 英寸</param>
  1299. /// <returns>毫米</returns>
  1300. public static float Inch100ToMillimeter(float inch100)
  1301. {
  1302. float mm = inch100 * MILLIMETER_PER_INCH / 100;
  1303. return mm;
  1304. }
  1305. /// <summary>
  1306. /// 磅转换为毫米
  1307. /// </summary>
  1308. /// <remarks>
  1309. /// 表示倍率 100%
  1310. /// </remarks>
  1311. /// <param name="point">磅</param>
  1312. /// <returns>毫米</returns>
  1313. public static float PointToMillimeter(float point)
  1314. {
  1315. float mm = point / POINT_PER_INCH * MILLIMETER_PER_INCH;
  1316. return mm;
  1317. }
  1318. /// <summary>
  1319. /// 磅转换为像素
  1320. /// </summary>
  1321. /// <remarks>
  1322. /// 表示倍率 100%
  1323. /// </remarks>
  1324. /// <param name="point">磅</param>
  1325. /// <returns>像素</returns>
  1326. public static int PointToPixel(float point)
  1327. {
  1328. decimal pixel = System.Convert.ToDecimal(point) /
  1329. System.Convert.ToDecimal(POINT_PER_INCH) *
  1330. System.Convert.ToDecimal(SCREEN_DPI);
  1331. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1332. }
  1333. /// <summary>
  1334. /// 毫米转换为1/100 英寸
  1335. /// </summary>
  1336. /// <remarks>
  1337. /// 表示倍率 100%
  1338. /// </remarks>
  1339. /// <param name="mm">毫米</param>
  1340. /// <returns>1/100 英寸</returns>
  1341. public static int MillimeterToInch100(float mm)
  1342. {
  1343. float inch100 = mm / MILLIMETER_PER_INCH * 100;
  1344. return System.Convert.ToInt32(Math.Round(inch100, 0));
  1345. }
  1346. /// <summary>
  1347. /// 毫米转换为1/100 英寸
  1348. /// </summary>
  1349. /// <remarks>
  1350. /// 表示倍率 100%
  1351. /// </remarks>
  1352. /// <param name="mm">毫米</param>
  1353. /// <returns>1/100 英寸</returns>
  1354. public static float MillimeterToInch100Single(float mm)
  1355. {
  1356. float inch100 = mm / MILLIMETER_PER_INCH * 100;
  1357. return inch100;
  1358. }
  1359. /// <summary>
  1360. /// 毫米转换为像素
  1361. /// </summary>
  1362. /// <remarks>
  1363. /// 表示倍率 100%
  1364. /// </remarks>
  1365. /// <param name="mm">毫米</param>
  1366. /// <returns>像素</returns>
  1367. public static int MillimeterToPixel(float mm)
  1368. {
  1369. decimal pixel = System.Convert.ToDecimal(mm) /
  1370. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1371. System.Convert.ToDecimal(SCREEN_DPI);
  1372. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1373. }
  1374. /// <summary>
  1375. /// 毫米转换为像素
  1376. /// </summary>
  1377. /// <param name="mm">毫米</param>
  1378. /// <param name="paperZoom">表示倍率%</param>
  1379. /// <returns>像素</returns>
  1380. public static int MillimeterToPixel(float mm, int paperZoom)
  1381. {
  1382. if (0 < paperZoom)
  1383. {
  1384. decimal pixel = System.Convert.ToDecimal(mm) /
  1385. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1386. System.Convert.ToDecimal(SCREEN_DPI) *
  1387. System.Convert.ToDecimal(paperZoom) / 100m;
  1388. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1389. }
  1390. else
  1391. {
  1392. return 0;
  1393. }
  1394. }
  1395. /// <summary>
  1396. /// 毫米转换为像素
  1397. /// </summary>
  1398. /// <param name="mm">毫米</param>
  1399. /// <param name="paperZoom">表示倍率%</param>
  1400. /// <returns>像素</returns>
  1401. public static int MillimeterToPixel(float mm, double paperZoom)
  1402. {
  1403. if (0 < paperZoom)
  1404. {
  1405. decimal pixel = System.Convert.ToDecimal(mm) /
  1406. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1407. System.Convert.ToDecimal(SCREEN_DPI) *
  1408. System.Convert.ToDecimal(paperZoom);
  1409. return System.Convert.ToInt32(Math.Round(pixel, 0));
  1410. }
  1411. else
  1412. {
  1413. return 0;
  1414. }
  1415. }
  1416. /// <summary>
  1417. /// 毫米转换为像素
  1418. /// </summary>
  1419. /// <param name="mm">毫米</param>
  1420. /// <param name="paperZoom">表示倍率%</param>
  1421. /// <returns>像素</returns>
  1422. public static float MillimeterToPixelSingle(float mm, int paperZoom)
  1423. {
  1424. if (0 < paperZoom)
  1425. {
  1426. decimal pixel = System.Convert.ToDecimal(mm) /
  1427. System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
  1428. System.Convert.ToDecimal(SCREEN_DPI) *
  1429. System.Convert.ToDecimal(paperZoom) / 100m;
  1430. return System.Convert.ToSingle(pixel);
  1431. }
  1432. else
  1433. {
  1434. return 0f;
  1435. }
  1436. }
  1437. /// <summary>
  1438. /// 像素转换为毫米
  1439. /// </summary>
  1440. /// <remarks>
  1441. /// 表示倍率 100%
  1442. /// </remarks>
  1443. /// <param name="pixel">像素</param>
  1444. /// <returns>毫米</returns>
  1445. public static float PixelToMillimeter(int pixel)
  1446. {
  1447. decimal mm = System.Convert.ToDecimal(pixel) *
  1448. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1449. System.Convert.ToDecimal(SCREEN_DPI);
  1450. return System.Convert.ToSingle(mm);
  1451. }
  1452. /// <summary>
  1453. /// 像素转换为毫米
  1454. /// </summary>
  1455. /// <param name="pixel">像素</param>
  1456. /// <param name="paperZoom">表示倍率%</param>
  1457. /// <returns>毫米</returns>
  1458. public static float PixelToMillimeter(int pixel, int paperZoom)
  1459. {
  1460. if (0 < paperZoom)
  1461. {
  1462. decimal mm = System.Convert.ToDecimal(pixel) *
  1463. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1464. System.Convert.ToDecimal(SCREEN_DPI) /
  1465. System.Convert.ToDecimal(paperZoom) * 100m;
  1466. return System.Convert.ToSingle(mm);
  1467. }
  1468. else
  1469. {
  1470. return 0f;
  1471. }
  1472. }
  1473. /// <summary>
  1474. /// 像素转换为毫米
  1475. /// </summary>
  1476. /// <param name="pixel">像素</param>
  1477. /// <param name="paperZoom">表示倍率%</param>
  1478. /// <param name="dpi">DPI</param>
  1479. /// <returns>毫米</returns>
  1480. public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
  1481. {
  1482. if (0 < paperZoom)
  1483. {
  1484. decimal mm = System.Convert.ToDecimal(pixel) *
  1485. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1486. System.Convert.ToDecimal(dpi) /
  1487. System.Convert.ToDecimal(paperZoom) * 100m;
  1488. return System.Convert.ToSingle(mm);
  1489. }
  1490. else
  1491. {
  1492. return 0f;
  1493. }
  1494. }
  1495. /// <summary>
  1496. /// 像素转换为毫米
  1497. /// </summary>
  1498. /// <param name="pixel">像素</param>
  1499. /// <param name="paperZoom">表示倍率%</param>
  1500. /// <returns>毫米</returns>
  1501. public static float PixelToMillimeter(int pixel, double paperZoom)
  1502. {
  1503. if (0 < paperZoom)
  1504. {
  1505. decimal mm = System.Convert.ToDecimal(pixel) *
  1506. System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
  1507. System.Convert.ToDecimal(SCREEN_DPI) /
  1508. System.Convert.ToDecimal(paperZoom);
  1509. return System.Convert.ToSingle(mm);
  1510. }
  1511. else
  1512. {
  1513. return 0f;
  1514. }
  1515. }
  1516. /// <summary>
  1517. /// 截断到指定的精度。
  1518. /// </summary>
  1519. /// <param name="value">截断的目标</param>
  1520. /// <param name="digits">精度</param>
  1521. /// <returns>截断后的数值</returns>
  1522. public static float Truncate(float value, int digits)
  1523. {
  1524. double v = Math.Pow(10, digits);
  1525. return Convert.ToSingle(Math.Truncate((double)value * v) / v);
  1526. }
  1527. /// <summary>
  1528. /// 截断到指定的精度。
  1529. /// </summary>
  1530. /// <param name="value">截断的目标</param>
  1531. /// <param name="digits">精度</param>
  1532. /// <returns>截断后的数值</returns>
  1533. public static decimal Truncate(decimal value, int digits)
  1534. {
  1535. decimal v = (decimal)Math.Pow(10, digits);
  1536. return Math.Truncate(value * v) / v;
  1537. }
  1538. #endregion
  1539. #region 字符转成条码图片
  1540. /// <summary>
  1541. /// 根据条码生成条码图片
  1542. /// </summary>
  1543. /// <param name="barcode">条码字符</param>
  1544. /// <returns>条码图片的Stream</returns>
  1545. public static Image GetCode39Image(string barcode)
  1546. {
  1547. try
  1548. {
  1549. byte[] bytes = GetCode39Byte(barcode);
  1550. MemoryStream ms = new MemoryStream(bytes);
  1551. Image returnImage = Image.FromStream(ms);
  1552. return returnImage;
  1553. }
  1554. catch (Exception ex)
  1555. {
  1556. throw ex;
  1557. }
  1558. }
  1559. /// <summary>
  1560. /// 根据条码生成条码图片
  1561. /// </summary>
  1562. /// <param name="barcode">条码字符</param>
  1563. /// <returns>条码图片的Stream</returns>
  1564. public static Stream GetCode39Stream(string barcode)
  1565. {
  1566. try
  1567. {
  1568. byte[] bytes = GetCode39Byte(barcode);
  1569. Stream stream = new MemoryStream(bytes);
  1570. return stream;
  1571. }
  1572. catch (Exception ex)
  1573. {
  1574. throw ex;
  1575. }
  1576. }
  1577. /// <summary>
  1578. /// 根据条码生成条码图片
  1579. /// </summary>
  1580. /// <param name="barcode">条码字符</param>
  1581. /// <returns>条码图片的byte[]</returns>
  1582. public static byte[] GetCode39Byte(string barcode)
  1583. {
  1584. try
  1585. {
  1586. MemoryStream memoryStream = new MemoryStream();
  1587. Bitmap barcodeBitmap = GetCode39Bitmap(barcode);
  1588. barcodeBitmap.Save(memoryStream, ImageFormat.Png);
  1589. byte[] byteImage = new Byte[memoryStream.Length];
  1590. byteImage = memoryStream.ToArray();
  1591. return byteImage;
  1592. }
  1593. catch (Exception ex)
  1594. {
  1595. throw ex;
  1596. }
  1597. }
  1598. /// <summary>
  1599. /// 根据条码生成条码图片
  1600. /// </summary>
  1601. /// <param name="barcode">条码字符</param>
  1602. /// <returns>
  1603. /// Bitmap条码图片
  1604. /// </returns>
  1605. public static Bitmap GetCode39Bitmap(string barcode)
  1606. {
  1607. try
  1608. {
  1609. int x = 5; // 左边界
  1610. int y = 0; // 上边界
  1611. int WidLength = 2; // 粗BarCode长度
  1612. int NarrowLength = 1; // 细BarCode长度
  1613. int BarCodeHeight = 50; // BarCode高度
  1614. int intSourceLength = barcode.Length;
  1615. string strEncode = "010010100"; // 编码字符串·初始值为 起始符号 *
  1616. string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; // Code39的字母
  1617. string[] Code39 = //Code39的各字母对应码
  1618. {
  1619. /**//* 0 */ "000110100",
  1620. /**//* 1 */ "100100001",
  1621. /**//* 2 */ "001100001",
  1622. /**//* 3 */ "101100000",
  1623. /**//* 4 */ "000110001",
  1624. /**//* 5 */ "100110000",
  1625. /**//* 6 */ "001110000",
  1626. /**//* 7 */ "000100101",
  1627. /**//* 8 */ "100100100",
  1628. /**//* 9 */ "001100100",
  1629. /**//* A */ "100001001",
  1630. /**//* B */ "001001001",
  1631. /**//* C */ "101001000",
  1632. /**//* D */ "000011001",
  1633. /**//* E */ "100011000",
  1634. /**//* F */ "001011000",
  1635. /**//* G */ "000001101",
  1636. /**//* H */ "100001100",
  1637. /**//* I */ "001001100",
  1638. /**//* J */ "000011100",
  1639. /**//* K */ "100000011",
  1640. /**//* L */ "001000011",
  1641. /**//* M */ "101000010",
  1642. /**//* N */ "000010011",
  1643. /**//* O */ "100010010",
  1644. /**//* P */ "001010010",
  1645. /**//* Q */ "000000111",
  1646. /**//* R */ "100000110",
  1647. /**//* S */ "001000110",
  1648. /**//* T */ "000010110",
  1649. /**//* U */ "110000001",
  1650. /**//* V */ "011000001",
  1651. /**//* W */ "111000000",
  1652. /**//* X */ "010010001",
  1653. /**//* Y */ "110010000",
  1654. /**//* Z */ "011010000",
  1655. /**//* - */ "010000101",
  1656. /**//* . */ "110000100",
  1657. /**//*' '*/ "011000100",
  1658. /**//* $ */ "010101000",
  1659. /**//* / */ "010100010",
  1660. /**//* + */ "010001010",
  1661. /**//* % */ "000101010",
  1662. /**//* * */ "010010100"
  1663. };
  1664. // 条码转成大写
  1665. barcode = barcode.ToUpper();
  1666. // 做成图片
  1667. Bitmap objBitmap = new Bitmap(
  1668. ((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),
  1669. BarCodeHeight + (y * 2));
  1670. Graphics objGraphics = Graphics.FromImage(objBitmap);
  1671. // 底色
  1672. objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
  1673. for (int i = 0; i < intSourceLength; i++)
  1674. {
  1675. // 检查是否有非法字符
  1676. if (AlphaBet.IndexOf(barcode[i]) == -1 || barcode[i] == '*')
  1677. {
  1678. objGraphics.DrawString("含有非法字符",
  1679. SystemFonts.DefaultFont, Brushes.Red, x, y);
  1680. return objBitmap;
  1681. }
  1682. // 查表编码
  1683. strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(barcode[i])]);
  1684. }
  1685. strEncode = string.Format("{0}0010010100", strEncode); // 补充结束符号 *
  1686. int intEncodeLength = strEncode.Length; // 编码后边的长度
  1687. int intBarWidth;
  1688. for (int i = 0; i < intEncodeLength; i++) // 根据码画出Code39 BarCode
  1689. {
  1690. intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
  1691. objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
  1692. x, y, intBarWidth, BarCodeHeight);
  1693. x += intBarWidth;
  1694. }
  1695. return objBitmap;
  1696. }
  1697. catch (Exception ex)
  1698. {
  1699. throw ex;
  1700. }
  1701. }
  1702. #endregion
  1703. #region INI文件读写
  1704. /// <summary>
  1705. /// 写入INI文件
  1706. /// </summary>
  1707. /// <param name="section">节点名称[如[TypeName]]</param>
  1708. /// <param name="key">键</param>
  1709. /// <param name="value">值</param>
  1710. /// <param name="filePath">文件路径</param>
  1711. /// <returns></returns>
  1712. public static long WriteIniFile(string section, string key, string value, string filePath)
  1713. {
  1714. try
  1715. {
  1716. return WritePrivateProfileString(section, key, value, filePath);
  1717. }
  1718. catch (Exception ex)
  1719. {
  1720. throw ex;
  1721. }
  1722. }
  1723. /// <summary>
  1724. /// 读取INI文件
  1725. /// </summary>
  1726. /// <param name="section">节点名称</param>
  1727. /// <param name="key">键</param>
  1728. /// <param name="filePath">文件路径</param>
  1729. /// <returns></returns>
  1730. public static string ReadIniFile(string section, string key, string filePath)
  1731. {
  1732. try
  1733. {
  1734. StringBuilder returnValue = new StringBuilder(1024);
  1735. GetPrivateProfileString(section, key, "", returnValue, 1024, filePath);
  1736. return returnValue.ToString();
  1737. }
  1738. catch (Exception ex)
  1739. {
  1740. throw ex;
  1741. }
  1742. }
  1743. #endregion
  1744. #region DataGridView限制
  1745. /// <summary>
  1746. /// 设定DataGridView的排序方式
  1747. /// </summary>
  1748. /// <param name="dataGridView">需要设置排序的DataGridView</param>
  1749. /// <param name="sortMode">排序方式</param>
  1750. public static void SetDataGridViewColumnsSortMode(DataGridView dataGridView,
  1751. DataGridViewColumnSortMode sortMode)
  1752. {
  1753. if (dataGridView == null)
  1754. {
  1755. return;
  1756. }
  1757. for (int i = 0; i < dataGridView.Columns.Count; i++)
  1758. {
  1759. dataGridView.Columns[i].SortMode = sortMode;
  1760. }
  1761. }
  1762. #endregion
  1763. #region 时间字符串转换成DateTime类型
  1764. /// <summary>
  1765. /// 时间字符串转换成DateTime类型
  1766. /// </summary>
  1767. /// <param name="serverTime">服务器时间</param>
  1768. /// <param name="timeStr">时间格式字符串</param>
  1769. /// <returns></returns>
  1770. public static DateTime ConvertTimeStr(DateTime serverTime, string timeStr)
  1771. {
  1772. try
  1773. {
  1774. DateTime dateTime = serverTime;
  1775. string[] times = timeStr.Split(':');
  1776. if (times.Length == 3)
  1777. {
  1778. dateTime = new DateTime(dateTime.Year, dateTime.Month,
  1779. dateTime.Day, Convert.ToInt16(times[0]), Convert.ToInt16(times[1]),
  1780. Convert.ToInt16(times[2]));
  1781. }
  1782. return dateTime;
  1783. }
  1784. catch (Exception ex)
  1785. {
  1786. throw ex;
  1787. }
  1788. }
  1789. #endregion
  1790. public static int? ToNullableInt32(this object obj)
  1791. {
  1792. if (obj == null || obj == DBNull.Value)
  1793. {
  1794. return null;
  1795. }
  1796. return Convert.ToInt32(obj);
  1797. }
  1798. #region JArray转DataTable
  1799. /// <summary>
  1800. /// JArray转DataTable
  1801. /// </summary>
  1802. /// <param name="dataArr"></param>
  1803. /// <returns></returns>
  1804. public static DataTable ConvertToDataTable(JArray dataArr)
  1805. {
  1806. if (dataArr == null || dataArr.Count <= 0)
  1807. return null;
  1808. DataTable result = new DataTable();
  1809. var colnames = ((JObject)(dataArr.First)).Properties();
  1810. List<string> columnNames = new List<string>();
  1811. if (colnames == null || colnames.Count() <= 0)
  1812. return null;
  1813. foreach (var item in colnames)
  1814. {
  1815. if (!columnNames.Contains(item.Name))
  1816. columnNames.Add(item.Name);
  1817. result.Columns.Add(item.Name, typeof(string));
  1818. }
  1819. foreach (JObject data in dataArr)
  1820. {
  1821. JObject jo = JObject.Parse(data.ToString());
  1822. DataRow row = result.NewRow();
  1823. foreach (var columnName in columnNames)
  1824. {
  1825. if (jo.Property(columnName) == null)
  1826. {
  1827. data.Add(columnName, "");
  1828. row[columnName] = data[columnName].ToString();
  1829. }
  1830. else
  1831. {
  1832. row[columnName] = data[columnName].ToString();
  1833. }
  1834. }
  1835. result.Rows.Add(row);
  1836. }
  1837. return result;
  1838. }
  1839. #endregion
  1840. }
  1841. }