| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:Utility.cs
- * 2.功能描述:系统各种常用函数和方法类
- * 3.函数/方法列表:
- * A.IsNull:判断空值
- * B.EnableFormCloseBox:设定窗体的关闭按钮是否有效
- * C.IsValidDate:判断是否是合法的日期
- * D.ChineseToPinyinCap:根据输入汉字,转化成汉字首字母串
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 欧阳涛 2012/06/07 1.00 新建
- * 王文君 2012/07/10 1.00 修改 添加IsUnique方法
- * 欧阳涛 2012/07/19 1.00 增加了GetBytesByFilePath()方法,
- * 根据文件路径返回文件字节流
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using Dongke.IBOSS.PRD.Basics.BaseResources;
- using Newtonsoft.Json.Linq;
- namespace Dongke.IBOSS.PRD.Basics.Library
- {
- /// <summary>
- /// 系统各种常用函数和方法类
- /// </summary>
- public static class Utility
- {
- #region IsDirty
- public static bool IsDirty(DataSet dataSet)
- {
- if (dataSet == null)
- {
- return false;
- }
- foreach (DataTable table in dataSet.Tables)
- {
- foreach (DataRow row in table.Rows)
- {
- row.EndEdit();
- }
- }
- DataSet changes = dataSet.GetChanges();
- if (changes == null)
- {
- return false;
- }
- foreach (DataTable table in changes.Tables)
- {
- if (IsDirty(table))
- {
- return true;
- }
- }
- return false;
- }
- public static bool IsDirty(DataTable table)
- {
- if (table == null)
- {
- return false;
- }
- if (table.GetChanges(DataRowState.Added) != null)
- {
- return true;
- }
- if (table.GetChanges(DataRowState.Deleted) != null)
- {
- return true;
- }
- DataTable changes = table.GetChanges(DataRowState.Modified);
- if (changes != null)
- {
- foreach (DataRow row in changes.Rows)
- {
- if (IsDirty(row))
- {
- return true;
- }
- }
- }
- return false;
- }
- public static bool IsDirty(DataRow row)
- {
- if (row == null)
- {
- return false;
- }
- if (row.RowState == DataRowState.Added
- || row.RowState == DataRowState.Deleted)
- {
- return true;
- }
- if (row.RowState == DataRowState.Modified)
- {
- foreach (DataColumn column in row.Table.Columns)
- {
- if (column.ReadOnly)
- {
- continue;
- }
- if (!Utility.Compare(row[column, DataRowVersion.Original],
- row[column, DataRowVersion.Current]))
- {
- return true;
- }
- }
- }
- return false;
- }
- #endregion
- #region 申明API函数
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="section">节点名称[如[TypeName]]</param>
- /// <param name="key">键</param>
- /// <param name="val">值</param>
- /// <param name="filepath">文件路径</param>
- /// <returns></returns>
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="section">节点名称</param>
- /// <param name="key">键</param>
- /// <param name="def">值</param>
- /// <param name="retval">stringbulider对象</param>
- /// <param name="size">字节大小</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
- #endregion
- #region 成员变量
- // 获取屏幕DPI用
- private static readonly Control _control;
- // 图形
- private static readonly Graphics _graphics;
- /// <summary>
- /// 毫米和像素的转换系数
- /// </summary>
- private const float MILLIMETER_PER_INCH = 25.4f;
- /// <summary>
- /// 磅和像素的转换系数
- /// </summary>
- private const float POINT_PER_INCH = 72f;
- /// <summary>
- /// 屏幕的DPI
- /// </summary>
- private static float SCREEN_DPI;
- #endregion
- #region 构造函数
- static Utility()
- {
- _control = new Control();
- _graphics = _control.CreateGraphics();
- SCREEN_DPI = _graphics.DpiX;
- }
- #endregion
- #region FindWindow()
- [DllImport("user32.dll")]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- #endregion
- #region WakeupWindow()
- public static bool WakeupWindow(IntPtr hWnd)
- {
- if (hWnd == System.IntPtr.Zero)
- {
- return false;
- }
- if (Win32.IsIconic(hWnd) || !Win32.IsWindowVisible(hWnd))
- {
- Win32.ShowWindowAsync(hWnd, Win32.SW_RESTORE);
- }
- Win32.SetForegroundWindow(hWnd);
- return true;
- }
- #endregion
- #region 基本共通函数
- /// <summary>
- /// 空值判断
- /// </summary>
- /// <param name="value">判断对象</param>
- /// <returns>
- /// True:是空值
- /// False:不是空值
- /// </returns>
- public static bool IsNull(object value)
- {
- if (value == null || value == DBNull.Value)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 过滤sql语句中非法的字符,并替换
- /// </summary>
- /// <param name="value">替换前的sql语句</param>
- /// <returns>替换后的sql语句字符</returns>
- public static string SelectFilterLike(string value)
- {
- if (string.IsNullOrEmpty(value))
- {
- return string.Empty;
- }
- return value.Replace(@"'", @"''").Replace(@"[", @"[[]")
- .Replace(@"_", @"[_]").Replace(@"%", @"[%]").Replace(@"*", @"[*]");
- }
- /// <summary>
- /// 控制窗体的关闭按钮是否能用
- /// </summary>
- /// <param name="hWnd">窗体句柄</param>
- /// <param name="bEnable">True:可用 False:不可用</param>
- /// <returns>
- /// 设置成功与否
- /// </returns>
- public static bool EnableFormCloseBox(IntPtr hWnd, bool bEnable)
- {
- if (hWnd == System.IntPtr.Zero)
- {
- return false;
- }
- IntPtr hMenu = Win32.GetSystemMenu(hWnd, 0);
- if (hMenu == System.IntPtr.Zero)
- {
- return false;
- }
- if (bEnable)
- {
- Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE, Win32.MF_ENABLED);
- }
- else
- {
- Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE,
- Win32.MF_DISABLED | Win32.MF_GRAYED);
- }
- return true;
- }
- /// <summary>
- /// 判断是否是正确的日期格式
- /// </summary>
- /// <param name="strDateTime">被判断的日期</param>
- /// <param name="dateTime">返回被格式化的日期</param>
- /// <returns>
- /// True:日期格式
- /// False:不是日期格式
- /// </returns>
- public static bool IsValidDate(string strDateTime, out DateTime? dateTime)
- {
- DateTime returnDatetime;
- if (DateTime.TryParse(strDateTime, out returnDatetime))
- {
- dateTime = returnDatetime;
- return true;
- }
- else
- {
- dateTime = null;
- return false;
- }
- }
- /// <summary>
- /// 取得显示顺序的最大值
- /// </summary>
- /// <param name="table">数据表</param>
- /// <param name="maxValName">列名</param>
- /// <returns>
- /// 该数据表里的最大显示顺序
- /// </returns>
- public static int GetMaxValue(DataTable table, string maxValName)
- {
- if (table == null)
- {
- return -1;
- }
- if (table.Columns.Contains(maxValName))
- {
- DataRow[] rows = table.Select("0 <= " + maxValName, maxValName + " DESC");
- if (rows == null || rows.Length == 0)
- {
- return -1;
- }
- object value = rows[0][maxValName];
- if (value == null)
- {
- return -1;
- }
- int maxValue = int.Parse(value.ToString());
- if (maxValue == short.MaxValue)
- {
- maxValue--;
- }
- return maxValue;
- }
- else
- {
- return -1;
- //DataRow[] rows = table.Select("0 <= " + maxValName,maxValName + "DESC");
- //object value = rows[0][maxValName];
- //int maxValue = int.Parse(value.ToString());
- //return maxValue;
- }
- }
- /// <summary>
- /// 取得数据表里的最大编码值
- /// </summary>
- /// <param name="table">数据表</param>
- /// <param name="maxValName">列名</param>
- /// <returns>该数据表里的最大编码值(string类型)</returns>
- /// <remarks>
- /// 2012.07.10 余再伟 创建
- /// </remarks>
- public static string GetStringMaxValue(DataTable table, string maxValName)
- {
- // 表为空时候直接返回空值
- if (table == null)
- {
- return string.Empty;
- }
- // 表中包含该列名的时候返回该数据表的最大编码值
- if (table.Columns.Contains(maxValName))
- {
- DataRow[] rows = table.Select("'0' <= " + maxValName, maxValName + " DESC");
- if (rows == null || rows.Length == 0)
- {
- return string.Empty;
- }
- object value = rows[0][maxValName];
- if (value == null)
- {
- return string.Empty;
- }
- string maxValue = value.ToString();
- return maxValue;
- }
- else
- {
- return string.Empty;
- }
- }
- /// <summary>
- /// 校验值是否重复
- /// </summary>
- /// <param name="curCellValue">当前Column的值</param>
- /// <param name="curRow">当前行的行号</param>
- /// <param name="dataGridView">所在的DataGridView</param>
- /// <param name="uniqueValue">比较值</param>
- /// <returns>是否存在</returns>
- public static bool IsUnique(string curCellValue, int curRow, DataGridView dataGridView, string uniqueValue)
- {
- foreach (DataGridViewRow rowItem in dataGridView.Rows)
- {
- if (rowItem.Index == curRow)
- {
- continue;
- }
- // 字典名称
- if (rowItem.Cells[uniqueValue].Value != null)
- {
- if (rowItem.Cells[uniqueValue].Value.ToString().Trim().
- Equals(curCellValue.Trim()))
- {
- return false;
- }
- }
- }
- return true;
- }
- /// <summary>
- /// 根据输入的汉字,返回所有汉字拼音的首字母串
- /// </summary>
- /// <param name="chineseString">输入的汉字</param>
- /// <returns>汉字拼音的首字母串</returns>
- public static string ChineseToPinyinCap(string chineseString)
- {
- string pinyinCapString = ""; // 转换后的拼音首字母字符串
- // 每个汉字存储需要两个字节
- byte[] ZW = new byte[2];
- long ChineseStr_int;
- string CharStr;
- string ChinaStr = "";
- chineseString = ToDBC(chineseString);
- for (int i = 0; i <= chineseString.Length - 1; i++)
- {
- // 取出一个汉字
- CharStr = chineseString.Substring(i, 1).ToString();
- // 得到汉字的字节表示
- ZW = System.Text.Encoding.Default.GetBytes(CharStr);
- // 得到汉字符的字节数组
- if (ZW.Length == 2)
- {
- int i1 = (short)(ZW[0]);
- int i2 = (short)(ZW[1]);
- ChineseStr_int = i1 * 256 + i2;
- #region 参数
- // table of the constant list
- // 'A'; //45217..45252
- // 'B'; //45253..45760
- // 'C'; //45761..46317
- // 'D'; //46318..46825
- // 'E'; //46826..47009
- // 'F'; //47010..47296
- // 'G'; //47297..47613
- // 'H'; //47614..48118
- // 'J'; //48119..49061
- // 'K'; //49062..49323
- // 'L'; //49324..49895
- // 'M'; //49896..50370
- // 'N'; //50371..50613
- // 'O'; //50614..50621
- // 'P'; //50622..50905
- // 'Q'; //50906..51386
- // 'R'; //51387..51445
- // 'S'; //51446..52217
- // 'T'; //52218..52697
- //没有U,V
- // 'W'; //52698..52979
- // 'X'; //52980..53640
- // 'Y'; //53689..54480
- // 'Z'; //54481..55289
- #endregion
- if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
- {
- ChinaStr = "A";
- }
- else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
- {
- ChinaStr = "B";
- }
- else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
- {
- ChinaStr = "C";
- }
- else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
- {
- ChinaStr = "D";
- }
- else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
- {
- ChinaStr = "E";
- }
- else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
- {
- ChinaStr = "F";
- }
- else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
- {
- ChinaStr = "G";
- }
- else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
- {
- ChinaStr = "H";
- }
- else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
- {
- ChinaStr = "J";
- }
- else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
- {
- ChinaStr = "K";
- }
- else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
- {
- ChinaStr = "L";
- }
- else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
- {
- ChinaStr = "M";
- }
- else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
- {
- ChinaStr = "N";
- }
- else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
- {
- ChinaStr = "O";
- }
- else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
- {
- ChinaStr = "P";
- }
- else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
- {
- ChinaStr = "Q";
- }
- else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
- {
- ChinaStr = "R";
- }
- else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
- {
- ChinaStr = "S";
- }
- else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
- {
- ChinaStr = "T";
- }
- else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
- {
- ChinaStr = "W";
- }
- else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
- {
- ChinaStr = "X";
- }
- else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
- {
- ChinaStr = "Y";
- }
- else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
- {
- ChinaStr = "Z";
- }
- else
- {
- ChinaStr = CharStr;
- }
- pinyinCapString = pinyinCapString + ChinaStr;
- }
- else
- {
- pinyinCapString = pinyinCapString + CharStr.ToUpper();
- }
- }
- return pinyinCapString;
- }
- #endregion
- #region 半角全角转换
- /// <summary>
- /// 转半角的函数(DBC)
- /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// 全角空格为12288,半角空格为32
- /// </summary>
- /// <param name="inputString">任意字符串</param>
- /// <returns>半角字符串</returns>
- public static string ToDBC(string inputString)
- {
- char[] c = inputString.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- {
- c[i] = (char)(c[i] - 65248);
- }
- }
- return new String(c);
- }
- /// <summary>
- /// 转全角的函数(SBC)
- /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// 全角空格为12288,半角空格为32
- /// </summary>
- /// <param name="inputString">任意字符串</param>
- /// <returns>全角字符串</returns>
- public static string ToSBC(string inputString)
- {
- // 半角转全角:
- char[] c = inputString.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 32)
- {
- c[i] = (char)12288;
- continue;
- }
- if (c[i] < 127)
- c[i] = (char)(c[i] + 65248);
- }
- return new String(c);
- }
- #endregion
- #region 正则判断数据合法性
- /// <summary>
- /// 验证邮箱格式
- /// </summary>
- /// <param name="email">要验证的邮箱地址</param>
- /// <returns>
- /// True:邮箱格式正确
- /// False:邮箱格式错误
- /// </returns>
- public static bool IsValidEmail(string email)
- {
- if (!Regex.IsMatch(email,
- @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- ///// <summary>
- ///// 验证身份证输入是否正确
- ///// </summary>
- ///// <param name="IdCard">要验证的身份证号</param>
- ///// <returns>
- ///// True:身份证格式正确
- ///// False:身份证格式错误
- ///// </returns>
- //public static bool IsIDcard(string IdCard)
- //{
- // bool result = false;
- // if (IdCard.Length <= 15)
- // {
- // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING15);
- // }
- // else
- // {
- // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING);
- // }
- // return result;
- //}
- /// <summary>
- /// 判断身份证号是否合法(对外部公开)
- /// </summary>
- /// <param name="strCardID">身份证号(旧/新)</param>
- /// <returns>false:不合法 true:合法</returns>
- public static bool IsIDcard(string strCardID)
- {
- strCardID = strCardID.ToLower();
- // 验证身份证的位数
- string strRegex = @"(\d{15}$)|(\d{17}(\d|x)$)";
- if (!Regex.IsMatch(strCardID, strRegex))
- {
- return false;
- }
- // 身份证数据有效性验证
- string strNewCardID = strCardID;
- if (strCardID.Length == 15)
- {
- strNewCardID = Convert15To18(strCardID);
- }
- if (!CheckCardIDInfo(strNewCardID))
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 判断身份证号是否合法
- /// </summary>
- /// <param name="strCardID">新身份证号</param>
- /// <returns>false:不合法 true:合法</returns>
- private static bool CheckCardIDInfo(string strCardID)
- {
- string[] aCity = new string[] { null, null, null, null, null, null, null, null, null,
- null, null, "北京", "天津 ", "河北", "山西", "内蒙古", null, null, null, null,
- null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海",
- "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南",
- "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川",
- "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃",
- "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null,
- null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null,
- null, null, null, "国外" };
- strCardID = strCardID.ToLower();
- strCardID = strCardID.Replace("x", "a");
- try
- {
- if (aCity[int.Parse(strCardID.Substring(0, 2))] == null)
- {
- return false;
- }
- DateTime.Parse(strCardID.Substring(6, 4) + "-"
- + strCardID.Substring(10, 2) + "-"
- + strCardID.Substring(12, 2));
- }
- catch
- {
- return false;
- }
- double iSum = 0;
- for (int i = 17; i >= 0; i--)
- {
- iSum += (System.Math.Pow(2, i) % 11) * int.Parse(strCardID[17 - i].ToString(),
- System.Globalization.NumberStyles.HexNumber);
- }
- if (iSum % 11 != 1)
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 从老身份证升位到新身份证
- /// </summary>
- /// <param name="strCardID">老身份证号码</param>
- /// <returns>新身份证号码</returns>
- private static string Convert15To18(string strCardID)
- {
- int intN = 0;
- // 加权因子常数
- int[] intW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
- // 校验码常数
- string strLastCode = "10X98765432";
- // 新身份证号
- string strNewCardID;
- strNewCardID = strCardID.Substring(0, 6);
- // 填在第6位及第7位上填上‘1’,‘9’两个数字
- strNewCardID += "19";
- strNewCardID += strCardID.Substring(6, 9);
- // 进行加权求和
- for (int i = 0; i < 17; i++)
- {
- intN += int.Parse(strNewCardID.Substring(i, 1)) * intW[i];
- }
- // 取模运算,得到模值
- int intY = intN % 11;
- // 从strLastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
- strNewCardID += strLastCode.Substring(intY, 1);
- return strNewCardID;
- }
- /// <summary>
- /// 验证车牌号输入是否正确
- /// </summary>
- /// <param name="PlateNumber">要验证的车牌号</param>
- /// <returns>
- /// True:车牌号格式正确
- /// False:车牌号格式错误
- /// </returns>
- public static bool IsPlateNumber(string PlateNumber)
- {
- return Regex.IsMatch(PlateNumber, Constant.REGEX_PLATENUMBER_STRING);
- }
- /// <summary>
- /// 验证车架号输入是否正确
- /// </summary>
- /// <param name="PlateNumber">要验证的车架号</param>
- /// <returns>
- /// True:车架号格式正确
- /// False:车架号格式错误
- /// </returns>
- public static bool IsVehicleNumber(string VehicleNumber)
- {
- return Regex.IsMatch(VehicleNumber, Constant.REGEX_VEHICLENUM_STRING);
- }
- /// <summary>
- /// 验证公司网址输入格式是否正确
- /// </summary>
- /// <param name="http">要验证的网址</param>
- /// True:网址格式正确
- /// False:网址格式错误
- /// <returns></returns>
- public static bool IsValidHttp(string http)
- {
- if (!Regex.IsMatch(http, Constant.REGEX_HTTP_STRING))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- /// <summary>
- /// 验证输入字符串是否为数字类型(包括带小数的数字)
- /// </summary>
- /// <param name="value">要验证的字符串</param>
- /// <returns>
- /// True:字符串是数字
- /// False:字符串不是数字
- /// </returns>
- public static bool IsNumeric(string value)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
- }
- /// <summary>
- /// 验证输入字符串是否为合法时间(HH:mm或者HH:mm:ss)
- /// </summary>
- /// <param name="value">要验证的字符串</param>
- /// <returns>
- /// True:字符串是合法时间(HH:mm或者HH:mm:ss)
- /// False:字符串不是合法时间(HH:mm或者HH:mm:ss)
- /// </returns>
- public static bool IsTime(string value)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(value, @"^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$");
- }
- /// <summary>
- /// 验证输入字符串是否为合法日期(yyyy-MM-dd)
- /// </summary>
- /// <param name="value">要验证的字符串</param>
- /// <returns>
- /// True:字符串是合法日期(yyyy-MM-dd)
- /// False:字符串不是合法日期(yyyy-MM-dd)
- /// </returns>
- public static bool IsDate(string value)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(value,
- @"^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
- }
- #endregion
- #region 二进制与文件之间转换
- /// <summary>
- /// 将文件转换成字节流
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- /// <returns>
- /// 文件的字节流
- /// </returns>
- public static byte[] GetBytesByFilePath(string filePath)
- {
- // 传入的值非法的情况下,返回NULL
- if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
- {
- return null;
- }
- // 读取文件到字节流中
- FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- BinaryReader binaryReader = new BinaryReader(fileStream);
- byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
- fileStream.Flush();
- fileStream.Close();
- return fileBytes;
- }
- /// <summary>
- /// 将二进制转换成文件
- /// </summary>
- /// <param name="filePath">文件存放的全路径</param>
- /// <param name="fileByte">二进制文件</param>
- /// <returns>
- /// True:保存成功
- /// False:保存失败
- /// </returns>
- public static bool BinaryToFile(string filePath, byte[] fileByte)
- {
- if (string.IsNullOrEmpty(filePath) || fileByte == null)
- {
- return false;
- }
- try
- {
- FileStream filestream = File.Create(filePath, fileByte.Length);
- filestream.Write(fileByte, 0, fileByte.Length);
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 图片与二进制之间转换
- /// <summary>
- /// 将图片文件序列化成二进制流
- /// </summary>
- /// <param name="imagePath">文件路径</param>
- /// <returns>二进制流文件</returns>
- public static MemoryStream ImageToBinary(string imagePath)
- {
- if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
- {
- return null;
- }
- try
- {
- // 将图片文件序列化成二进制流文件
- Image image = new Bitmap(imagePath);
- MemoryStream memorystream = new MemoryStream();
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(memorystream, image);
- return memorystream;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 将二进制流反序列化成图片文件
- /// </summary>
- /// <param name="memoryStream">二进制流文件</param>
- /// <returns>图片文件</returns>
- public static Image StreamToImage(MemoryStream memoryStream)
- {
- if (memoryStream == null)
- {
- return null;
- }
- try
- {
- // 将流文件反序列化成图片文件
- memoryStream.Position = 0;
- BinaryFormatter formatter = new BinaryFormatter();
- return (Image)formatter.Deserialize(memoryStream);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 将二进制转换成图片文件
- /// </summary>
- /// <param name="imageBinary">二进制</param>
- /// <returns>image</returns>
- public static Image BinaryToImage(byte[] imageBinary)
- {
- if (imageBinary == null)
- {
- return null;
- }
- try
- {
- MemoryStream memoryStream = new MemoryStream(imageBinary);
- return Image.FromStream(memoryStream);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据Image对象转换成二进制
- /// </summary>
- /// <param name="image">image对象</param>
- /// <returns>
- /// 二进制数组
- /// </returns>
- public static byte[] ImageToByteArray(Image image)
- {
- if (image == null)
- {
- return null;
- }
- try
- {
- MemoryStream memoryStream = new MemoryStream();
- image.Save(memoryStream, ImageFormat.Jpeg);
- return memoryStream.ToArray();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region Compare比较函数
- [Flags]
- public enum CompareType
- {
- None = 0,
- // DBNull.Value值与null值视为同一值
- DBNullAsNull = 1,
- // string.Empty值null值视为同一值
- EmptyAsNull = 2,
- // 全部都视为同一值
- All = DBNullAsNull | EmptyAsNull
- }
- /// <summary>
- /// 两个值比较函数
- /// </summary>
- /// <param name="value1">值1</param>
- /// <param name="value2">值2</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- public static bool Compare(object value1, object value2)
- {
- return Compare(value1, value2, CompareType.None);
- }
- /// <summary>
- /// 两个值比较函数
- /// </summary>
- /// <param name="value1">值1</param>
- /// <param name="value2">值2</param>
- /// <param name="compareFlag">比较方式</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- public static bool Compare(object value1, object value2, CompareType compareFlag)
- {
- bool isNull1 = (value1 == null);
- bool isNull2 = (value2 == null);
- if ((compareFlag & CompareType.DBNullAsNull) == CompareType.DBNullAsNull)
- {
- if (value1 == DBNull.Value)
- {
- isNull1 = true;
- }
- if (value2 == DBNull.Value)
- {
- isNull2 = true;
- }
- }
- if ((compareFlag & CompareType.EmptyAsNull) == CompareType.EmptyAsNull)
- {
- if (string.Empty.Equals(value1))
- {
- isNull1 = true;
- }
- if (string.Empty.Equals(value2))
- {
- isNull2 = true;
- }
- }
- if (isNull1 && isNull2)
- {
- return true;
- }
- if (value1 == null || value2 == null)
- {
- return false;
- }
- else
- {
- return CompareSub(value1, value2);
- }
- }
- /// <summary>
- /// 两个值比较函数
- /// </summary>
- /// <param name="value1">值1</param>
- /// <param name="value2">值2</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- private static bool CompareSub(object value1, object value2)
- {
- // 数据类型不一样的情况下,返回false
- if (value1.GetType().Equals(value2.GetType()) == false)
- {
- return false;
- }
- // 枚举比较的情况,不能使用Equals,需单独写方法比较
- if (value1 is System.Collections.IEnumerable
- && value2 is System.Collections.IEnumerable)
- {
- return CompareSub((System.Collections.IEnumerable)value1,
- (System.Collections.IEnumerable)value2);
- }
- else
- {
- return value1.Equals(value2);
- }
- }
- /// <summary>
- /// 两个枚举类型的比较
- /// </summary>
- /// <param name="enumerable1">值1</param>
- /// <param name="enumerable2">值2</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- private static bool CompareSub(System.Collections.IEnumerable enumerable1,
- System.Collections.IEnumerable enumerable2)
- {
- System.Collections.IEnumerator e1 = enumerable1.GetEnumerator();
- System.Collections.IEnumerator e2 = enumerable2.GetEnumerator();
- while (e1.MoveNext())
- {
- if (!e2.MoveNext())
- {
- return false;
- }
- if (!e1.Current.Equals(e2.Current))
- {
- return false;
- }
- }
- return !e2.MoveNext();
- }
- #endregion
- #region 校验数字
- /// <summary>
- /// 校验数据是否符合要求
- /// </summary>
- /// <param name="number">待验证的参数</param>
- /// <param name="numberName">待验证的参数名称</param>
- /// <param name="type">校验的种类</param>
- /// <returns>错误信息</returns>
- /// <remarks>
- /// 2012.10.8 周兴 新建
- /// </remarks>
- public static string IsValidNumber(string number, string numberName, byte type)
- {
- try
- {
- // 如果第一位是-,表面可能是负号,去掉之后进行校验
- if (!string.IsNullOrEmpty(number))
- {
- if (number.IndexOf("-") == 0)
- {
- number = number.Substring(1);
- }
- }
- string errorMsg = string.Empty;
- string maxNumber = string.Empty;
- string regex = string.Empty;
- // 验证Numeric(16,6)
- if (type == 1)
- {
- regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,6})?$";
- maxNumber = "9999999999.999999";
- }
- // 验证Numeric(14,6)
- else if (type == 2)
- {
- regex = @"^(0|([1-9][0-9]{0,7}))(\.[0-9]{0,6})?$";
- maxNumber = "99999999.999999";
- }
- // 验证Numeric(12,6)
- else if (type == 3)
- {
- regex = @"^(0|([1-9][0-9]{0,5}))(\.[0-9]{0,6})?$";
- maxNumber = "999999.999999";
- }
- // 验证Numeric(5,2)
- else if (type == 4)
- {
- regex = @"^(0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?$";
- maxNumber = "999.99";
- }
- // 验证Numeric(5,2)(采购返利方式的返点率)
- else if (type == 5)
- {
- regex = @"^((0|([1-9][0-9]{0,1}))(\.[0-9]{0,2})?|(100(\.0{0,2})?))$";
- maxNumber = "100.00";
- }
- // 验证Numeric(6,2)(系统参数舍零金额限制最大值1000.00)
- else if (type == 6)
- {
- regex = @"^((0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?|(1000(\.0{0,2})?))$";
- maxNumber = "1000.00";
- }
- // 验证Numeric(12,2)(任务策略中任务金额限制)
- else if (type == 7)
- {
- regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,2})?$";
- maxNumber = "9999999999.99";
- }
- // 验证Numeric(6,2)(商品编码中费用分摊系数)
- else if (type == 8)
- {
- regex = @"^(0|([1-9][0-9]{0,3}))(\.[0-9]{0,2})?$";
- maxNumber = "9999.99";
- }
- // 验证Numeric(9,2)(CA员工业绩岗位考核工资基数)
- else if (type == 9)
- {
- regex = @"^(0|([1-9][0-9]{0,6}))(\.[0-9]{0,2})?$";
- maxNumber = "9999999.99";
- }
- // 验证Numeric(3,2)(CA员工业绩提成比率)
- else if (type == 10)
- {
- regex = @"^(0|([0-9]))(\.[0-9]{0,2})?$";
- maxNumber = "9.99";
- }
- // 验证Numeric(3,1)(CB员工业绩提成比率)
- else if (type == 11)
- {
- regex = @"^(0|([1-9][0-9]{0,1}))(\.[0-9]{0,1})?$";
- maxNumber = "99.9";
- }
- // 验证Numeric(7,2)(店长提成策略出库净额递增额)
- else if (type == 12)
- {
- regex = @"^(0|([1-9][0-9]{0,4}))(\.[0-9]{0,2})?$";
- maxNumber = "99999.99";
- }
- if (!Regex.IsMatch(number, regex))
- {
- //errorMsg = string.Format(Messages.MESSAGE_W004, numberName, maxNumber);
- }
- return errorMsg;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 校验电话格式
- /// <summary>
- /// 校验电话是否符合格式
- /// </summary>
- /// <param name="telephone"></param>
- /// <returns></returns>
- public static bool IsValidTelephone(string telephone)
- {
- try
- {
- /**
- * 手机号码
- * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
- * 联通:130,131,132,152,155,156,185,186
- * 电信:133,1349,153,180,181,189
- */
- string mobile = "^1(3[0-9]|5[0-35-9]|8[012345-9])\\d{8}$";
- /**
- * 大陆地区固话及小灵通 区号和电话之间没有-
- * 区号:010,020,021,022,023,024,025,027,028,029
- * 号码:七位或八位
- */
- string phs = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
- if (!Regex.IsMatch(telephone, mobile) && !Regex.IsMatch(telephone, phs))
- {
- return false;
- }
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 验证规格是否符合计算面积格式
- /// <summary>
- /// 验证规格是否符合计算面积格式
- /// </summary>
- /// <param name="specification">规格</param>
- /// <returns></returns>
- public static bool IsValidAcreage(string specification)
- {
- try
- {
- string regex = @"^\d+\*\d+$";
- if (Regex.IsMatch(specification, regex))
- {
- return true;
- }
- return false;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 验证规格是否符合计算体积格式
- /// <summary>
- /// 验证规格是否符合计算体积格式
- /// </summary>
- /// <param name="specification">规格</param>
- /// <returns></returns>
- public static bool IsValidVolume(string specification)
- {
- try
- {
- string regex = @"^\d+\*\d+\*\d+$";
- if (Regex.IsMatch(specification, regex))
- {
- return true;
- }
- return false;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 长度与像素之间转换
- /// <summary>
- /// 1/100 英寸转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">1/100 英寸</param>
- /// <returns>毫米</returns>
- public static float Inch100ToMillimeter(int inch100)
- {
- float mm = inch100 * MILLIMETER_PER_INCH / 100;
- return mm;
- }
- /// <summary>
- /// 1/100 英寸转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">1/100 英寸</param>
- /// <returns>毫米</returns>
- public static float Inch100ToMillimeter(float inch100)
- {
- float mm = inch100 * MILLIMETER_PER_INCH / 100;
- return mm;
- }
- /// <summary>
- /// 磅转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="point">磅</param>
- /// <returns>毫米</returns>
- public static float PointToMillimeter(float point)
- {
- float mm = point / POINT_PER_INCH * MILLIMETER_PER_INCH;
- return mm;
- }
- /// <summary>
- /// 磅转换为像素
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="point">磅</param>
- /// <returns>像素</returns>
- public static int PointToPixel(float point)
- {
- decimal pixel = System.Convert.ToDecimal(point) /
- System.Convert.ToDecimal(POINT_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为1/100 英寸
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>1/100 英寸</returns>
- public static int MillimeterToInch100(float mm)
- {
- float inch100 = mm / MILLIMETER_PER_INCH * 100;
- return System.Convert.ToInt32(Math.Round(inch100, 0));
- }
- /// <summary>
- /// 毫米转换为1/100 英寸
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>1/100 英寸</returns>
- public static float MillimeterToInch100Single(float mm)
- {
- float inch100 = mm / MILLIMETER_PER_INCH * 100;
- return inch100;
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI) *
- System.Convert.ToDecimal(paperZoom) / 100m;
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm, double paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI) *
- System.Convert.ToDecimal(paperZoom);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static float MillimeterToPixelSingle(float mm, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI) *
- System.Convert.ToDecimal(paperZoom) / 100m;
- return System.Convert.ToSingle(pixel);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">像素</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(SCREEN_DPI);
- return System.Convert.ToSingle(mm);
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(SCREEN_DPI) /
- System.Convert.ToDecimal(paperZoom) * 100m;
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <param name="dpi">DPI</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(dpi) /
- System.Convert.ToDecimal(paperZoom) * 100m;
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, double paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(SCREEN_DPI) /
- System.Convert.ToDecimal(paperZoom);
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 截断到指定的精度。
- /// </summary>
- /// <param name="value">截断的目标</param>
- /// <param name="digits">精度</param>
- /// <returns>截断后的数值</returns>
- public static float Truncate(float value, int digits)
- {
- double v = Math.Pow(10, digits);
- return Convert.ToSingle(Math.Truncate((double)value * v) / v);
- }
- /// <summary>
- /// 截断到指定的精度。
- /// </summary>
- /// <param name="value">截断的目标</param>
- /// <param name="digits">精度</param>
- /// <returns>截断后的数值</returns>
- public static decimal Truncate(decimal value, int digits)
- {
- decimal v = (decimal)Math.Pow(10, digits);
- return Math.Truncate(value * v) / v;
- }
- #endregion
- #region 字符转成条码图片
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>条码图片的Stream</returns>
- public static Image GetCode39Image(string barcode)
- {
- try
- {
- byte[] bytes = GetCode39Byte(barcode);
- MemoryStream ms = new MemoryStream(bytes);
- Image returnImage = Image.FromStream(ms);
- return returnImage;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>条码图片的Stream</returns>
- public static Stream GetCode39Stream(string barcode)
- {
- try
- {
- byte[] bytes = GetCode39Byte(barcode);
- Stream stream = new MemoryStream(bytes);
- return stream;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>条码图片的byte[]</returns>
- public static byte[] GetCode39Byte(string barcode)
- {
- try
- {
- MemoryStream memoryStream = new MemoryStream();
- Bitmap barcodeBitmap = GetCode39Bitmap(barcode);
- barcodeBitmap.Save(memoryStream, ImageFormat.Png);
- byte[] byteImage = new Byte[memoryStream.Length];
- byteImage = memoryStream.ToArray();
- return byteImage;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>
- /// Bitmap条码图片
- /// </returns>
- public static Bitmap GetCode39Bitmap(string barcode)
- {
- try
- {
- int x = 5; // 左边界
- int y = 0; // 上边界
- int WidLength = 2; // 粗BarCode长度
- int NarrowLength = 1; // 细BarCode长度
- int BarCodeHeight = 50; // BarCode高度
- int intSourceLength = barcode.Length;
- string strEncode = "010010100"; // 编码字符串·初始值为 起始符号 *
- string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; // Code39的字母
- string[] Code39 = //Code39的各字母对应码
- {
- /**//* 0 */ "000110100",
- /**//* 1 */ "100100001",
- /**//* 2 */ "001100001",
- /**//* 3 */ "101100000",
- /**//* 4 */ "000110001",
- /**//* 5 */ "100110000",
- /**//* 6 */ "001110000",
- /**//* 7 */ "000100101",
- /**//* 8 */ "100100100",
- /**//* 9 */ "001100100",
- /**//* A */ "100001001",
- /**//* B */ "001001001",
- /**//* C */ "101001000",
- /**//* D */ "000011001",
- /**//* E */ "100011000",
- /**//* F */ "001011000",
- /**//* G */ "000001101",
- /**//* H */ "100001100",
- /**//* I */ "001001100",
- /**//* J */ "000011100",
- /**//* K */ "100000011",
- /**//* L */ "001000011",
- /**//* M */ "101000010",
- /**//* N */ "000010011",
- /**//* O */ "100010010",
- /**//* P */ "001010010",
- /**//* Q */ "000000111",
- /**//* R */ "100000110",
- /**//* S */ "001000110",
- /**//* T */ "000010110",
- /**//* U */ "110000001",
- /**//* V */ "011000001",
- /**//* W */ "111000000",
- /**//* X */ "010010001",
- /**//* Y */ "110010000",
- /**//* Z */ "011010000",
- /**//* - */ "010000101",
- /**//* . */ "110000100",
- /**//*' '*/ "011000100",
- /**//* $ */ "010101000",
- /**//* / */ "010100010",
- /**//* + */ "010001010",
- /**//* % */ "000101010",
- /**//* * */ "010010100"
- };
- // 条码转成大写
- barcode = barcode.ToUpper();
- // 做成图片
- Bitmap objBitmap = new Bitmap(
- ((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),
- BarCodeHeight + (y * 2));
- Graphics objGraphics = Graphics.FromImage(objBitmap);
- // 底色
- objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
- for (int i = 0; i < intSourceLength; i++)
- {
- // 检查是否有非法字符
- if (AlphaBet.IndexOf(barcode[i]) == -1 || barcode[i] == '*')
- {
- objGraphics.DrawString("含有非法字符",
- SystemFonts.DefaultFont, Brushes.Red, x, y);
- return objBitmap;
- }
- // 查表编码
- strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(barcode[i])]);
- }
- strEncode = string.Format("{0}0010010100", strEncode); // 补充结束符号 *
- int intEncodeLength = strEncode.Length; // 编码后边的长度
- int intBarWidth;
- for (int i = 0; i < intEncodeLength; i++) // 根据码画出Code39 BarCode
- {
- intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
- objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
- x, y, intBarWidth, BarCodeHeight);
- x += intBarWidth;
- }
- return objBitmap;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region INI文件读写
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="section">节点名称[如[TypeName]]</param>
- /// <param name="key">键</param>
- /// <param name="value">值</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static long WriteIniFile(string section, string key, string value, string filePath)
- {
- try
- {
- return WritePrivateProfileString(section, key, value, filePath);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="section">节点名称</param>
- /// <param name="key">键</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static string ReadIniFile(string section, string key, string filePath)
- {
- try
- {
- StringBuilder returnValue = new StringBuilder(1024);
- GetPrivateProfileString(section, key, "", returnValue, 1024, filePath);
- return returnValue.ToString();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region DataGridView限制
- /// <summary>
- /// 设定DataGridView的排序方式
- /// </summary>
- /// <param name="dataGridView">需要设置排序的DataGridView</param>
- /// <param name="sortMode">排序方式</param>
- public static void SetDataGridViewColumnsSortMode(DataGridView dataGridView,
- DataGridViewColumnSortMode sortMode)
- {
- if (dataGridView == null)
- {
- return;
- }
- for (int i = 0; i < dataGridView.Columns.Count; i++)
- {
- dataGridView.Columns[i].SortMode = sortMode;
- }
- }
- #endregion
- #region 时间字符串转换成DateTime类型
- /// <summary>
- /// 时间字符串转换成DateTime类型
- /// </summary>
- /// <param name="serverTime">服务器时间</param>
- /// <param name="timeStr">时间格式字符串</param>
- /// <returns></returns>
- public static DateTime ConvertTimeStr(DateTime serverTime, string timeStr)
- {
- try
- {
- DateTime dateTime = serverTime;
- string[] times = timeStr.Split(':');
- if (times.Length == 3)
- {
- dateTime = new DateTime(dateTime.Year, dateTime.Month,
- dateTime.Day, Convert.ToInt16(times[0]), Convert.ToInt16(times[1]),
- Convert.ToInt16(times[2]));
- }
- return dateTime;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- public static int? ToNullableInt32(this object obj)
- {
- if (obj == null || obj == DBNull.Value)
- {
- return null;
- }
- return Convert.ToInt32(obj);
- }
- #region JArray转DataTable
- /// <summary>
- /// JArray转DataTable
- /// </summary>
- /// <param name="dataArr"></param>
- /// <returns></returns>
- public static DataTable ConvertToDataTable(JArray dataArr)
- {
- if (dataArr == null || dataArr.Count <= 0)
- return null;
- DataTable result = new DataTable();
- var colnames = ((JObject)(dataArr.First)).Properties();
- List<string> columnNames = new List<string>();
- if (colnames == null || colnames.Count() <= 0)
- return null;
- foreach (var item in colnames)
- {
- if (!columnNames.Contains(item.Name))
- columnNames.Add(item.Name);
- result.Columns.Add(item.Name, typeof(string));
- }
- foreach (JObject data in dataArr)
- {
- JObject jo = JObject.Parse(data.ToString());
- DataRow row = result.NewRow();
- foreach (var columnName in columnNames)
- {
- if (jo.Property(columnName) == null)
- {
- data.Add(columnName, "");
- row[columnName] = data[columnName].ToString();
- }
- else
- {
- row[columnName] = data[columnName].ToString();
- }
- }
- result.Rows.Add(row);
- }
- return result;
- }
- #endregion
- }
- }
|