using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Text.RegularExpressions; using System.Reflection; using System.Collections; using System.IO; namespace DataCommon { /// /// 静态方法处理类 /// 主要一些公共方法处理 /// public class SystemCommon { /// /// 根据当前年获取字符串 /// /// /// public static string GetNowYear (string year) { string CodeYear = ""; if(int.Parse(year) <=2009) { CodeYear = year; } Dictionary systemData = new Dictionary(); int a = 2010; for (char c = 'A'; c <= 'Z'; c++) { systemData.Add(a.ToString(),c); a++; if(a>=2031) { break; } } int i = 1; for(int j=2031;j<2039;j++ ) { systemData.Add(j.ToString(), Convert.ToChar(i)); i++; } return systemData[year].ToString(); } /// /// 获取月份 /// /// /// public static string GetNowMonth(string month) { string noInt = "1"; if(int.Parse(month)>10) { if(noInt=="10") { noInt = "A"; } if(month=="11") { noInt = "B"; } if (month == "12") { noInt = "C"; } } return noInt; } private const int MAX_NUMBER = 10000; //#region 单件模式实例化类 返回当前子类 ///// /////实例话窗体类。返回一个窗体类 ///// ///// ///// //public object Instance(string nameForm, string className, string FormId, ref int FrmIndex, string strAppPath) //{ // DevExpress.XtraTab.XtraTabControl tab_list = ((DevExpress.XtraTab.XtraTabControl)(Application.OpenForms[FormId].Controls["FunRibbon"])); // object objType = new object(); // for (int i = 0; i < tab_list.TabPages.Count; i++) // { // if (className == tab_list.TabPages[i].Name.ToString()) // { // FrmIndex = i; // return tab_list.TabPages[i]; // } // } // objType = (Form)System.Reflection.Assembly.LoadFile(strAppPath + "\\" + nameForm + ".dll").GetType(nameForm + "." + className).GetConstructor(System.Type.EmptyTypes).Invoke(null); // return objType; //} //#endregion /// /// 写文件 /// /// /// public static void WriteFile(string fileName,string str) { //检测目录是否存在,不存在则创建 string directory = fileName.Substring(0,fileName.LastIndexOf("\\")); if (!Directory.Exists(directory)) { DirectoryInfo info = Directory.CreateDirectory(directory); } //创建一个文件(如果有进行覆盖),并且返回一个文件流(进行写入的)。 FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); //写入一个字符串 //讲一个字符串转换成字节数组(并且指名了编码方式和操作系统的方式是一样的)。 byte[] bytes = System.Text.Encoding.Default.GetBytes(str); //将byte数组写入文件流。 fs.Write(bytes, 0, bytes.Length); //关闭流 fs.Flush(); fs.Close(); } /// /// 字符串格式化 1,2,3----'1','2','3' /// /// /// public static string StringFormat(string str) { string strValue = ""; string[] arr = str.Split(','); for (int i = 0; i < arr.Length; i++) { strValue = strValue + "'" + arr[i] + "',"; } if (strValue != "") strValue = strValue.Substring(0, strValue.Length - 1); return strValue; } /// /// 控制窗体控件是否可用 /// /// 窗体ID /// 控件ID /// True /// 功能菜单结果集 /// 菜单ID /// 功能名称 //public static void FunControlEnabled(string FormId, bool Enabled // , DataTable dtFun, string Menu_Id, string FunName) //{ // DataRow[] dr = dtFun.Select("MENU_ID = '" + Menu_Id + "'"); // for (int i = 0; i < dr.Length; i++) // { // string[] arr = FunName.Split(':'); // for (int j = 0; j < arr.Length; j++) // { // if (dr[i]["FUN_NAME"].ToString().Trim() == arr[j].Trim()) // { // ControlEnabled("FrmSysFrame", dr[i]["FUN_ID"].ToString(), Enabled); // } // } // } //} ///// ///// 控制窗体控件是否可用 ///// ///// 窗体ID ///// 控件ID ///// True //public static void ControlEnabled(string FormId, string ControlId, bool Enabled) //{ // DevExpress.XtraBars.Ribbon.RibbonControl FunRibbon = ((DevExpress.XtraBars.Ribbon.RibbonControl)(Application.OpenForms[FormId].Controls["FunRibbon"])); // for (int i = 0; i < FunRibbon.Pages.Count; i++) // { // for (int j = 0; j < FunRibbon.Pages[i].Groups.Count; j++) // { // for (int k = 0; k < FunRibbon.Pages[i].Groups[j].ItemLinks.Count; k++) // { // for (int h = 0; h < FunRibbon.Pages[i].Groups[j].ItemLinks[k].Links.Count; h++) // { // BarButtonItem btn = ((DevExpress.XtraBars.BarButtonItemLink)(FunRibbon.Pages[i].Groups[j].ItemLinks[k].Links[h])).Item; // if (btn.Name == ControlId) // { // btn.Enabled = Enabled; // return; // } // } // } // } // } //} ///// ///// 弹出错误提示信息,标题为“错误” ///// ///// 错误信息 //public static void ShowErrorMessageBox(string errorInfoString) //{ // XtraMessageBox.Show(errorInfoString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); // // MessageBox.Show(errorInfoString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); //} /// /// 弹出提示信息,标题为“提示” /// /// 提示信息 //public static void ShowInfoMessageBox(string infoString) //{ // XtraMessageBox.Show(infoString, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // //MessageBox.Show(infoString, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); //} ///// ///// 弹出返回是与否信息,标题为“确认” OKCancel ///// ///// 提示信息 //public static DialogResult ShowMessageBoxResult(string infoString) //{ // return XtraMessageBox.Show(infoString, "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2); // //MessageBox.Show(infoString, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); //} /// /// 弹出返回是否以及取消信息,标题为“确认” /// /// 提示信息 // public static DialogResult ShowMessageBoxResultCancel(string infoString) //{ // return XtraMessageBox.Show(infoString, "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2); // //MessageBox.Show(infoString, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); //} /// /// 判断当前输入是否为正整数 /// /// Input /// public static bool IsPositiveInteger(string input) { return Regex.IsMatch(input, @"^\d{1,9}$"); } /* /// /// 判断当前输入是否为整数(可以为负数) /// /// Input /// public static bool IsInteger(string input) { return Regex.IsMatch(input, @"^-?\d{1,9}$"); }*/ /// /// 判断当前输入是否为正整数或正浮点数 /// /// Input /// public static bool IsPositiveFloat(string input) { return Regex.IsMatch(input, @"^(0|[1-9]\d{0,8})(.\d{0,4})?$"); // 可以是 1.00 // @"^(\+|-)?(0|[1-9]\d*)(.\d*)?$" : 任意实数 // @"^(0|[1-9]\d*)(.\d*[1-9])?$" : 不可以是1.00 } #region 判断当前输入是否为正确的出版年月(格式:yyyy.MM) /// /// 判断当前输入是否为正确的出版年月(格式:yyyy.MM) /// /// Input /// public static bool IsRightPublishDate(string input) { return Regex.IsMatch(input, @"^[1-9]\d{3}.(0[1-9]|1[0-2])$"); } #endregion #region 合并相同结构的两个DataTable /// /// 合并相同结构的两个DataTable /// /// 原DataTable /// 目的DataTable /// public static System.Data.DataTable DataTableAdd(System.Data.DataTable originalDt, System.Data.DataTable aimDt) { foreach (DataRow dr in originalDt.Rows) { aimDt.ImportRow(dr); } aimDt.AcceptChanges(); return aimDt; } #endregion #region 将10位ISBN转换为13位ISBN /// /// 将10位ISBN转换为13位ISBN /// /// 10位ISBN /// public static string ConvertIsbn(string inputValue) { string outValue = inputValue.Trim().Replace("X", "x"); if (!Regex.IsMatch(outValue, @"^[7]\d{8}([x]|\d{1})$")) { return inputValue; } int flag1; if (outValue.EndsWith("x")) { flag1 = 10; } else if (outValue.EndsWith("0")) { flag1 = 11; } else { flag1 = int.Parse(outValue.Substring(9, 1)); } char[] valueChar = outValue.Substring(0, 9).ToCharArray(); int flag2 = 0; int i = 0; foreach (char a in valueChar) { i++; flag2 += int.Parse(a.ToString()) * (11 - i); } if (flag1 != (11 - (flag2 % 11))) { return inputValue; } //重算校验码 int bit1 = 0; int bit2 = 0; outValue = "978" + outValue.Substring(0, 9); valueChar = outValue.ToCharArray(); i = 0; foreach (char a in valueChar) { if ((i % 2) == 0) { //奇数位 bit2 += int.Parse(a.ToString()); } else { //偶数位 bit1 += int.Parse(a.ToString()); } i++; } int bit = bit1 * 3 + bit2; bit = 10 - (bit % 10); if (bit == 10) { bit = 0; } outValue += bit; return outValue; } #endregion #region 判断输入的数字是否大于MAX_NUMBER public static bool IsMoreThanMaxNumber(int number) { return number > MAX_NUMBER; } #endregion #region DataTable 分页 public static System.Data.DataTable GetDataTableFT(int from, int length, System.Data.DataTable dt, out bool flag) { flag = false; DataTable newdt = new DataTable(); newdt = dt.Clone(); if (from < dt.Rows.Count) { int i; for (i = from; i < from + length; i++) { if (i < dt.Rows.Count) { newdt.ImportRow(dt.Rows[i]); } else { flag = true; break; } } if (i == dt.Rows.Count) { flag = true; } } return newdt; } #endregion #region 校验isbn是否正确 1-12位的数值,其中偶数、奇数各自相加,根据偶数、奇数的和 /// /// 校验isbn是否正确 1-12位的数值,其中偶数、奇数各自相加,根据偶数、奇数的和 /// /// /// public static bool of_checkisbn(string as_isbn) { // 判断校验位是否正确 string ls_temp_isbn = as_isbn.Trim(); if (ls_temp_isbn.Trim().Length != 13 || ls_temp_isbn.Trim().Substring(0, 3) != "978") { return false; } int li_sum_js = 0; // 奇数和 int li_sum_os = 0; // 偶数和 int li_avg_ys = 0; // 余数 int li_bit_isbn = 0; // UNIT的isbn int li_bit_check = 0; // 检测位 for (int i = 1; i <= 12; i++) { li_bit_isbn = int.Parse(ls_temp_isbn.Substring(i - 1, 1)); if (i % 2 == 0) { li_sum_os = li_sum_os + li_bit_isbn; // 偶数位相加 } else { li_sum_js = li_sum_js + li_bit_isbn; // 奇数位相加 } } li_avg_ys = (li_sum_js + li_sum_os * 3) % 10; li_bit_check = 10 - li_avg_ys; // 用10 - 所求余数,得到校验位 if (li_bit_check == 10) { li_bit_check = 0; } // 如果等于10的话则视为0 string ls_bit_right = ls_temp_isbn.Substring(12, 1); // 取最后一位 if (li_bit_check.ToString() != ls_bit_right) { return false; } else { return true; } } #endregion /// /// MisCtrlTextBox回车验证 /// /// /// //public static void TextBoxKeyDown(TextBox textBox) //{ // if (GetValResult(textBox)) // { // SendKeys.Send("{TAB}"); // } //} ///// ///// MisCtrlTextBox 离开验证 ///// ///// //public static void TextBoxLeave(TextBox textBox) //{ // GetValResult(textBox); //} //private static bool GetValResult(TextBox textBox) //{ // if (textBox.Text.Length != 0 && textBox.Text.Length > textBox.Text.Length) // { // ShowErrorMessageBox("文本长度不能大于" + textBox.Text.Length.ToString() + "!"); // //MessageBox.Show("文本长度不能大于" + textBox.StrLen.ToString() + "!"); // return false; // } // string msg = ""; // //if (!Check(textBox.Text, ref msg)) // //{ // // ShowErrorMessageBox(textBox.Text.ToString() + "输入错误!"); // // textBox.Text = ""; // // textBox.Focus(); // // return false; // //} // return true; //} //#region 正则验证 ////string pattern; //public static bool Check(TextBox textBox, ref string msg) //{ // string pattern; // switch (textBox.Text.ToString()) // { // //case "无://无 // // return true; // // break; // case "整数"://整数 // msg = "整数"; // pattern = @"^-?[1-9]\d*$"; // break; // case "正整数"://正整数 // msg = "正整数"; // pattern = "^[0-9]*[1-9][0-9]*$"; // break; // case "负整数"://负整数 // msg = "负整数"; // pattern = "^-[0-9]*[1-9][0-9]*$"; // break; // case "数字"://数字 // msg = "数字"; // pattern = "^([+-]?)\\d*\\.?\\d+$"; // break; // case "电话"://电话 // msg = "电话"; // //在做项目时常常用到判断电话号码的正则表达式,写了一个,可验证如下27种格式: // //110 // //8888888 // //88888888 // //8888888-123 // //88888888-23435 // //0871-8888888-123 // //023-88888888-23435 // //86-0871-8888888-123 // //8888888_123 // //88888888_23435 // //0871_8888888_123 // //023_88888888_23435 // //86_0871_8888888_123 // //8888888-123 // //88888888-23435 // //0871-8888888-123 // //023-88888888-23435 // //86-0871-8888888-123 // //8888888—123 // //88888888—23435 // //0871—8888888—123 // //023—88888888—23435 // //86—0871—8888888—123 // //13588888888 // //15988888888 // //013588888888 // //015988888888 // //(0315)7663551 // pattern = @"((^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$)z)|(^(\([0-9]+\))?[0-9]{7,8}$)"; // break; // case "正数"://正数(正整数+ 0) // msg = "正数(正整数+ 0)"; // pattern = @"^\d+$"; // break; // case "负数"://负数(负整数+ 0) // msg = "负数(负整数+ 0)"; // pattern = @"^((-\d+)|(0+))$"; // break; // case "浮点数"://浮点数 // msg = "浮点数"; // pattern = @"^(-?\d+)(\.\d+)?$"; // break; // case "正浮点数"://正浮点数 // msg = "正浮点数"; // pattern = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"; // break; // case "负浮点数"://负浮点数 // msg = "负浮点数"; // pattern = @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"; // break; // case "浮点数2"://浮点数 // msg = "浮点数"; // pattern = "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$"; // break; // case "非负浮点数"://非负浮点数(正浮点数+ 0) // msg = "非负浮点数(正浮点数+ 0)"; // pattern = @"^\d+(\.\d+)?$"; // break; // case "非正浮点数"://非正浮点数(负浮点数+ 0) // msg = "非正浮点数(负浮点数+ 0)"; // pattern = @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"; // break; // case "邮件"://邮件 //正确 // msg = "邮件"; // pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; // break; // case "颜色"://颜色 // msg = "颜色"; // pattern = "^[a-fA-F0-9]{6}$"; // break; // case "url"://url(http格式的) // msg = "url(http格式的)"; // pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; // break; // case "中文"://仅中文 // msg = "中文"; // pattern = @"[\u4e00-\u9fa5]"; // break; // case "ACSII字符"://仅ACSII字符 // msg = "ACSII字符"; // pattern = "^[\\x00-\\xFF]+$"; // break; // case "邮编"://邮编 // msg = "邮编"; // pattern = "^\\d{6}$"; // break; // case "手机"://手机 // msg = "手机"; // pattern = "^[1]+[3,5,8,4]+\\d{9}"; // break; // case "IP地址"://ip地址 // msg = "IP地址"; // pattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"; // break; // case "非空"://非空 // msg = "非空"; // pattern = "^\\S+$"; // break; // case "图片"://图片 // msg = "图片"; // pattern = @"(.*)\.(jpg|gif|png|bmp)$"; // break; // case "压缩文件"://压缩文件 // msg = "压缩文件"; // pattern = "(.*)\\.(rar|zip|7zip|tgz)$"; // break; // case "日期"://日期 // msg = "日期"; // //这个日期正则表达式支持 // //YYYY-MM-DD // //YYYY/MM/DD // //YYYY_MM_DD // //YYYY.MM.DD的形式 // pattern = @"((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))"; // break; // case "QQ号码"://QQ号码 // msg = "QQ号码"; // pattern = "[1-9][0-9]{4,}"; // break; // case "用户名"://用来用户注册。匹配由数字、个英文字母或者下划线组成的字符串 // msg = "用户名匹配由数字、个英文字母或者下划线组成的字符串"; // pattern = "^(?!\\d)[a-zA-Z0-9_\\u4e00-\\u9fa5]+$"; // break; // case "字母"://字母 // msg = "字母"; // pattern = "^[A-Za-z]+$"; // break; // case "大写字母"://大写字母 // msg = "大写字母"; // pattern = "^[A-Z]+$"; // break; // case "小写字母"://小写字母 // msg = "小写字母"; // pattern = "^[a-z]+$"; // break; // case "身份证"://身份证 // msg = "身份证"; // //pattern = @"^[1-9]([0-9]{16}|[0-9]{13})[xX0-9]$"; // pattern = "(^\\d{18}$)|(^\\d{15}$)"; // break; // default: // pattern = string.Empty; // break; // } // return Regex.IsMatch(textBox.Text, pattern); //} //#endregion //public static void GridColumnWidth(DevExpress.XtraGrid.Views.Grid.GridView gridView) //{ // for (int i = 0; i < gridView.Columns.Count; i++) // { // gridView.Columns[i].BestFit(); // } //} /// /// 提取字符串中的数字字符串 /// /// /// public static string GetNumber(ref string str) { string returnStr = string.Empty; returnStr = Regex.Replace(str, "[a-z,A-Z]", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "[0-9]", "", RegexOptions.IgnoreCase); return returnStr; //string returnStr = string.Empty; //for (int i = 0; i < str.Length; i++) //{ // if (Char.IsNumber(str, i) == true) // { // string sdfsdfsd = str.Substring(i, 1); // returnStr += str.Substring(i, 1); // } // else // { // string sss = str.Substring(i, 1); // if (str.Substring(i, 1) == "A" || str.Substring(i, 1) == " ") // { // returnStr += str.Substring(i, 1); // } // } //} //return returnStr; } } public class ConvertHelper where T :class, new() { /// /// 利用反射和泛型 /// /// /// public static List ConvertToList(DataTable dt) { // 定义集合 List ts = new List(); try { // 获得此模型的类型 Type type = typeof(T); //定义一个临时变量 string tempName = string.Empty; ///列的属性Status是否改变 bool typeChange = false; DataTable resutTable = dt.Clone(); foreach (DataColumn clmn in resutTable.Columns) { PropertyInfo[] propertys = type.GetProperties(); //遍历该对象的所有属性 foreach (PropertyInfo pi in propertys) { if (pi.Name.ToUpper() == clmn.ColumnName.ToUpper()) { if (pi.PropertyType != clmn.DataType) { clmn.DataType = pi.PropertyType; typeChange = true; continue; } } } } if (typeChange) { foreach (DataRow dr in dt.Rows) { DataRow row = resutTable.NewRow(); try { foreach (DataColumn clm in dt.Columns) { row[clm.ColumnName] = dr[clm.ColumnName]; } } catch (Exception ex) { } resutTable.Rows.Add(row); resutTable.AcceptChanges(); } } else { resutTable = dt.Copy(); } //遍历DataTable中所有的数据行 foreach (DataRow dr in resutTable.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); //遍历该对象的所有属性 foreach (PropertyInfo pi in propertys) { tempName = pi.Name;//将属性名称赋值给临时变量 //检查DataTable是否包含此列(列名==对象的属性名) if (dt.Columns.Contains(tempName)) { Type v = pi.PropertyType; // 判断此属性是否有Setter if (!pi.CanWrite) continue;//该属性不可写,直接跳出 //取值 object value = dr[tempName]; if (v != dr[tempName].GetType()) { } //如果非空,则赋给对象的属性 if (value != DBNull.Value) pi.SetValue(t, value, null); } } //对象添加到泛型集合中 ts.Add(t); } } catch (Exception ex) { } return ts; } /// /// 将集合类转换成DataTable /// /// 集合 /// public static DataTable ToDataTable(IList list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } } }