using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Globalization;
|
using System.Security.Cryptography;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
using System.Threading;
|
using System.Web;
|
|
namespace HH.WCS.ZCQTJ.util
|
{
|
public class Util
|
{
|
|
#region 其他
|
/// <summary>
|
/// 获取客户端IP
|
/// </summary>
|
/// <returns></returns>
|
public static string ClientIp()
|
{
|
if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
|
{
|
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
|
}
|
return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
|
}
|
|
/// <summary>
|
/// 把object格式化成字符串
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <returns>格式化后字符串</returns>
|
/// <history>
|
/// [hanhe(姜新军)] 2010/08/23 Created
|
/// </history>
|
public static string ToString(object value)
|
{
|
if (value == null || value == DBNull.Value)
|
return string.Empty;
|
|
if (value is DateTime)
|
return ToString(value, "yyyy-MM-dd");
|
|
if (value is Decimal)
|
return ToString(value, "n2");
|
|
return value.ToString();
|
}
|
|
public static string ToMongoLike(string value)
|
{
|
value = value ?? "";
|
value = value
|
.Replace("*", @"\*")
|
.Replace("(", @"\(")
|
.Replace(")", @"\)");
|
|
return "/" + value + "/";
|
}
|
|
public static string ToStringInput(object value)
|
{
|
if (value == null || value == DBNull.Value)
|
return string.Empty;
|
if (value is DateTime)
|
return ToString(value, "yyyy-MM-dd");
|
if (value is Decimal)
|
return ToString(value, "n2");
|
if (value.ToString().IndexOf('\'') >= 0)
|
return value.ToString().Replace("\'", "");
|
return value.ToString();
|
}
|
|
/// <summary>
|
/// like
|
/// </summary>
|
/// <param name="value"></param>
|
/// <returns></returns>
|
public static string ToLike(object value)
|
{
|
return "%" + ToStringInput(value) + "%";
|
}
|
|
/// <summary>
|
/// 把object格式化成字符串
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <returns>格式化后字符串</returns>
|
/// <history>
|
/// [hanhe(姜新军)] 2010/08/23 Created
|
/// </history>
|
public static string ToDateString(object value)
|
{
|
if (value == null || value == DBNull.Value)
|
return string.Empty;
|
|
if (value is DateTime)
|
return ToString(value, "yyyy-MM-dd");
|
|
if (value is Decimal)
|
return ToString(value, "n2");
|
|
return value.ToString();
|
}
|
|
|
/// <summary>
|
/// 把object格式化成字符串
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <returns>格式化后字符串</returns>
|
/// <history>
|
/// [hanhe(姜新军)] 2010/08/23 Created
|
/// </history>
|
public static bool IsDateTime(object value)
|
{
|
bool flag = false;
|
DateTime dtDate;
|
try
|
{
|
DateTime.TryParse(value.ToString(), out dtDate);
|
|
// Convert.ToDateTime(value.ToString());
|
|
flag = true;
|
|
}
|
catch
|
{
|
flag = false;
|
}
|
|
return flag;
|
}
|
|
|
/// <summary>
|
/// 判断用户输入是否为日期
|
/// </summary>
|
/// <param ></param>
|
/// <returns></returns>
|
/// <remarks>
|
/// 可判断格式如下(其中-可替换为/,不影响验证)
|
/// YYYY-MM-DD | YYYY-MM-DD HH:MM:SS | YYYY-MM-DD HH:MM:SS.FFF
|
/// </remarks>
|
public static bool IsTDateTime(object value)
|
{
|
|
if (null == value)
|
{
|
return false;
|
}
|
|
DateTime dts = DateTime.Now;
|
bool code = value.ToString().StartsWith("00");
|
if (code) return false;
|
//return DateTime.TryParse(value.ToString(), out dts);
|
|
|
string strValue = Util.ToString(value);
|
DateTime dt;
|
// string regexDate = @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$";
|
// Regex.IsMatch(strValue, regexDate)
|
|
bool flag = DateTime.TryParseExact(strValue, "yyyy-MM-dd", null, DateTimeStyles.None, out dt);
|
|
if (flag)
|
{
|
try
|
{
|
strValue = Util.ToString(Convert.ToDateTime(strValue), "yyyy-MM-dd HH:mm:ss");
|
}
|
catch
|
{
|
return false;
|
}
|
|
//以下各月份日期验证,保证验证的完整性
|
int _IndexY = -1;
|
int _IndexM = -1;
|
int _IndexD = -1;
|
|
if (-1 != (_IndexY = strValue.IndexOf("-")))
|
{
|
_IndexM = strValue.IndexOf("-", _IndexY + 1);
|
_IndexD = strValue.IndexOf(":");
|
}
|
else
|
{
|
_IndexY = strValue.IndexOf("/");
|
_IndexM = strValue.IndexOf("/", _IndexY + 1);
|
_IndexD = strValue.IndexOf(":");
|
}
|
|
//不包含日期部分,直接返回true
|
if (-1 == _IndexM)
|
return true;
|
|
if (-1 == _IndexD)
|
{
|
_IndexD = strValue.Length + 3;
|
}
|
|
int iYear = Convert.ToInt32(strValue.Substring(0, _IndexY));
|
int iMonth = Convert.ToInt32(strValue.Substring(_IndexY + 1, _IndexM - _IndexY - 1));
|
|
int iv = _IndexD == 11 ? 3 : 4;
|
int iDate = Convert.ToInt32(strValue.Substring(_IndexM + 1, _IndexD - _IndexM - iv));
|
|
//判断月份日期
|
if ((iMonth < 8 && 1 == iMonth % 2) || (iMonth > 8 && 0 == iMonth % 2))
|
{
|
if (iDate < 32)
|
return true;
|
}
|
else
|
{
|
if (iMonth != 2)
|
{
|
if (iDate < 31)
|
return true;
|
}
|
else
|
{
|
//闰年
|
if ((0 == iYear % 400) || (0 == iYear % 4 && 0 < iYear % 100))
|
{
|
if (iDate < 30)
|
return true;
|
}
|
else
|
{
|
if (iDate < 29)
|
return true;
|
}
|
}
|
}
|
}
|
|
return false;
|
}
|
|
/// <summary>
|
/// 把object格式化成字符串
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <param name="format">格式化引擎字符串</param>
|
/// <returns>格式化后字符串</returns>
|
/// <history>
|
/// [hanhe(姜新军)] 2010/08/23 Created
|
/// </history>
|
public static string ToString(object value, string format)
|
{
|
if (value == null || value == DBNull.Value)
|
return string.Empty;
|
|
if (value is DateTime)
|
return ((DateTime)value).ToString(format);
|
|
if (value is Decimal)
|
return ((Decimal)value).ToString(format);
|
|
return value.ToString();
|
}
|
/// <summary>
|
/// 把object格式化成日期类型
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <returns>格式化引擎日期类型</returns>
|
/// <history>
|
/// [hanhe(姜新军)] 2010/08/23 Created
|
/// </history>
|
public static DateTime ToDateTime(object value)
|
{
|
if (value == null || value == DBNull.Value)
|
{
|
return DateTime.MinValue;
|
}
|
else
|
return Convert.ToDateTime(value);
|
}
|
/// <summary>
|
/// 把object格式化成整形
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <returns>格式化引擎整形</returns>
|
/// <history>
|
/// [hanhe(姜新军)] 2010/08/23 Created
|
/// </history>
|
public static int ToInt(object value)
|
{
|
if (value == null || value == DBNull.Value || (value.ToString() == "undefined"))
|
{
|
return 0;
|
}
|
else
|
return Convert.ToInt32(value);
|
}
|
|
/// <summary>
|
/// 获取父路径
|
/// </summary>
|
/// <param name="path"></param>
|
/// <returns></returns>
|
public static string GetParentPath(string path)
|
{
|
if (String.IsNullOrEmpty(path) || path[0] != '/')
|
return null;
|
|
int index = path.LastIndexOf('/');
|
if (index < 0)
|
return null;
|
|
// parent for the root app is machine.config (null)
|
if (path == "/")
|
return null;
|
|
|
string returnPath = path.Substring(0, index);
|
if (returnPath.Length == 0 || returnPath == "/")
|
{
|
// for cassini, if returning /
|
// then return null instead.
|
returnPath = null;
|
}
|
|
return returnPath;
|
}
|
|
///<remarks>
|
///随机字符串生成器的主要功能如下:
|
///<list type="bullet">
|
///<item><description>支持自定义字符串长度</description></item>
|
///<item><description>支持自定义是否包含数字</description></item>
|
///<item><description>支持自定义是否包含小写字母</description></item>
|
///<item><description>支持自定义是否包含大写字母</description></item>
|
///<item><description>支持自定义是否包含特殊符号</description></item>
|
///<item><description>支持自定义字符集</description></item>
|
/// </list>
|
/// </remarks>
|
public static string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
|
{
|
byte[] b = new byte[4];
|
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
|
Random r = new Random(BitConverter.ToInt32(b, 0));
|
string s = null, str = custom ?? "";
|
if (useNum == true) { str += "0123456789"; }
|
if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
|
if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
|
if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
|
for (int i = 0; i < length; i++)
|
{
|
s += str.Substring(r.Next(0, str.Length - 1), 1);
|
}
|
return s;
|
}
|
|
/// <summary>
|
/// MD5加密
|
/// </summary>
|
/// <param name="text"></param>
|
/// <returns></returns>
|
public static string MD5Encrypt(string text)
|
{
|
//74e164017d6499cc68e1f5e123ca4a62 - aaa
|
|
// Create a new instance of the MD5CryptoServiceProvider object.
|
MD5 md5Hasher = MD5.Create();
|
|
// Convert the input string to a byte array and compute the hash.
|
byte[] data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(text));
|
|
// Create a new Stringbuilder to collect the bytes
|
// and create a string.
|
StringBuilder sBuilder = new StringBuilder();
|
|
// Loop through each byte of the hashed data
|
// and format each one as a hexadecimal string.
|
for (int i = 0; i < data.Length; i++)
|
{
|
sBuilder.Append(data[i].ToString("x2"));
|
}
|
|
// Return the hexadecimal string.
|
return sBuilder.ToString();
|
}
|
|
/// <summary>
|
/// 清楚数字的零并格式化千分号
|
/// </summary>
|
/// <param name="number"></param>
|
/// <returns></returns>
|
public static string CleanSuffixalZeroAndFormat(object number)
|
{
|
if (number == null)
|
{
|
return "";
|
}
|
return FormatNumber(CleanSuffixalZero(number.ToString()));
|
}
|
|
/// <summary>
|
/// 清楚数字的零并格式化千分号
|
/// </summary>
|
/// <param name="number"></param>
|
/// <returns></returns>
|
public static string CleanSuffixalZeroAndFormat(string number)
|
{
|
return FormatNumber(CleanSuffixalZero(number));
|
}
|
|
/// <summary>
|
/// 清除数字后面的零
|
/// </summary>
|
/// <param name="number"></param>
|
/// <returns></returns>
|
public static string CleanSuffixalZero(string number)
|
{
|
number = number ?? "";
|
decimal temp = 0;
|
|
if (string.IsNullOrEmpty(number) ||
|
!decimal.TryParse(number, out temp))
|
{
|
return "";
|
}
|
|
number = temp.ToString();
|
|
//查看是否有小数位
|
if (number.IndexOf(".") < 0)
|
{
|
//如果没有小数位,直接输出
|
return number.ToString();
|
}
|
//先清除小数的后面的零
|
return number.TrimEnd('0').TrimEnd('.');
|
}
|
|
/// <summary>
|
/// 格式化数字,添加千分号
|
/// </summary>
|
/// <param name="d"></param>
|
/// <returns></returns>
|
public static string FormatNumber(decimal d)
|
{
|
string str = d.ToString();
|
int digital = 0;
|
if (str.Contains("."))
|
{
|
digital = str.Length - str.IndexOf('.') - 1;
|
}
|
str = d.ToString("N" + digital);
|
return str;
|
}
|
|
/// <summary>
|
/// 格式化数字,添加千分号
|
/// </summary>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static string FormatNumber(object obj)
|
{
|
decimal d;
|
|
if (obj == null)
|
{
|
return "";
|
}
|
else if (decimal.TryParse(obj.ToString(), out d))
|
{
|
string str = d.ToString();
|
int digital = 0;
|
if (str.Contains("."))
|
{
|
digital = str.Length - str.IndexOf('.') - 1;
|
}
|
str = d.ToString("N" + digital);
|
return str;
|
}
|
else
|
{
|
return obj.ToString();
|
}
|
}
|
#endregion
|
|
#region 截取字符列表,传递对象,主要用于数据绑定时防止出现空值
|
|
/// <summary>
|
/// 截取字符列表,传递对象,主要用于数据绑定时防止出现空值
|
/// </summary>
|
/// <param name="source">传递对象</param>
|
/// <param name="length">截取长度</param>
|
/// <returns>返回</returns>
|
public static string GetLeftSubString(object source, int length)
|
{
|
if (source != null && (!source.ToString().Trim().Equals("")))
|
{
|
String s = source.ToString().Trim();
|
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
|
int n = 0; // 表示当前的字节数
|
int i = 0; // 要截取的字节数
|
for (; i < bytes.GetLength(0) && n < length; i++)
|
{
|
//偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节
|
if (i % 2 == 0)
|
{
|
n++; // 在UCS2第一个字节时n加1
|
}
|
else
|
{
|
//当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节
|
if (bytes[i] > 0)
|
{
|
n++;
|
}
|
}
|
}
|
|
//如果i为奇数时,处理成偶数
|
if (i % 2 == 1)
|
{
|
//该UCS2字符是汉字时,去掉这个截一半的汉字
|
if (bytes[i] > 0)
|
i = i - 1;
|
//该UCS2字符是字母或数字,则保留该字符
|
else
|
i = i + 1;
|
}
|
|
return bytes.Length > i ? "<span title=\"" + s + "\">" + System.Text.Encoding.Unicode.GetString(bytes, 0, i) + "...</span>" : System.Text.Encoding.Unicode.GetString(bytes, 0, i);
|
//return bytes.Length > i ? System.Text.Encoding.Unicode.GetString(bytes, 0, i) : System.Text.Encoding.Unicode.GetString(bytes, 0, i);
|
}
|
|
return ""; //如果传入的对象是Null或空字符串,返回--
|
}
|
#endregion
|
|
#region 截取字符列表,传递对象,主要用于数据绑定时防止出现空值
|
|
/// <summary>
|
/// 截取字符列表,传递对象,主要用于数据绑定时防止出现空值
|
/// </summary>
|
/// <param name="source">传递对象</param>
|
/// <param name="length">截取长度</param>
|
/// <returns>返回</returns>
|
public static string GetSubString(object source, int length)
|
{
|
if (source != null && (!source.ToString().Trim().Equals("")))
|
{
|
String s = source.ToString().Trim();
|
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
|
int n = 0; // 表示当前的字节数
|
int i = 0; // 要截取的字节数
|
for (; i < bytes.GetLength(0) && n < length; i++)
|
{
|
//偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节
|
if (i % 2 == 0)
|
{
|
n++; // 在UCS2第一个字节时n加1
|
}
|
else
|
{
|
//当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节
|
if (bytes[i] > 0)
|
{
|
n++;
|
}
|
}
|
}
|
|
//如果i为奇数时,处理成偶数
|
if (i % 2 == 1)
|
{
|
//该UCS2字符是汉字时,去掉这个截一半的汉字
|
if (bytes[i] > 0)
|
i = i - 1;
|
//该UCS2字符是字母或数字,则保留该字符
|
else
|
i = i + 1;
|
}
|
|
return bytes.Length > i ? "" + System.Text.Encoding.Unicode.GetString(bytes, 0, i) + "..." : System.Text.Encoding.Unicode.GetString(bytes, 0, i);
|
//return bytes.Length > i ? System.Text.Encoding.Unicode.GetString(bytes, 0, i) : System.Text.Encoding.Unicode.GetString(bytes, 0, i);
|
}
|
|
return ""; //如果传入的对象是Null或空字符串,返回--
|
}
|
#endregion
|
|
#region Html Table字符串转换为DataTable
|
//分析HTML 数据
|
public static DataTable HtmlTableToDataTable(string[] arrHead, string htmlTable)
|
{
|
System.Data.DataRow dr;
|
|
DataTable dt = new DataTable();
|
|
string fileConent = string.Empty;
|
string tableContent = string.Empty;
|
string rowContent = string.Empty;
|
string columnConent = string.Empty;
|
// string titleConent = string.Empty;
|
|
string rowPatterm = @"<tr[^>]*>[\s\S]*?<\/tr>";
|
string columnPattern = @"<td[^>]*>[\s\S]*?<\/td>";
|
// string titlePattern = @"<th[^>]*>[\s\S]*?<\/th>";
|
|
|
MatchCollection rowCollection = Regex.Matches(htmlTable, rowPatterm, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); //对tr进行筛选
|
/* if (rowCollection.Count > 0)
|
{// 获取表头
|
rowContent = rowCollection[0].Value;
|
MatchCollection titleCollection = Regex.Matches(rowContent, titlePattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); //对th进行筛选
|
for (int j = 0; j < titleCollection.Count; j++)
|
{
|
titleConent = titleCollection[j].Value;
|
int iBodyStart = titleConent.IndexOf(">", 0);
|
int iTableEnd = titleConent.IndexOf("</th>", iBodyStart);
|
string strWeb = titleConent.Substring(iBodyStart + 1, iTableEnd - iBodyStart - 1); //获取最终数据
|
dt.Columns.Add(new System.Data.DataColumn(strWeb, typeof(System.String)));
|
}
|
|
}*/
|
foreach (string head in arrHead)
|
{
|
dt.Columns.Add(new System.Data.DataColumn(head, typeof(System.String)));
|
}
|
|
for (int i = 0; i < rowCollection.Count; i++)
|
{
|
dr = dt.NewRow();
|
rowContent = rowCollection[i].Value;
|
MatchCollection columnCollection = Regex.Matches(rowContent, columnPattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); //对td进行筛选
|
|
for (int j = 0; j < columnCollection.Count; j++)
|
{
|
columnConent = columnCollection[j].Value;
|
int iBodyStart = columnConent.IndexOf(">", 0);
|
int iTableEnd = columnConent.IndexOf("</td>", iBodyStart);
|
string strWeb = columnConent.Substring(iBodyStart + 1, iTableEnd - iBodyStart - 1); //获取最终数据
|
dr[j] = strWeb;
|
}
|
dt.Rows.Add(dr);
|
}
|
|
return dt;
|
}
|
#endregion
|
|
#region C#与js通用的16位编码
|
|
/// <summary>
|
/// C#与js通用的16位编码
|
/// </summary>
|
/// <param name="s">字符串</param>
|
/// <returns>返回16位编码字字符串</returns>
|
public static string escape(string s)
|
{
|
StringBuilder sb = new StringBuilder();
|
byte[] ba = System.Text.Encoding.Unicode.GetBytes(s);
|
for (int i = 0; i < ba.Length; i += 2)
|
{
|
sb.Append("%u");
|
sb.Append(ba[i + 1].ToString("X2"));
|
|
sb.Append(ba[i].ToString("X2"));
|
}
|
return sb.ToString();
|
|
}
|
#endregion
|
|
#region 把JavaScript的escape()转换过去的字符串解释回来
|
//些方法支持汉字
|
public static string unescape(string s)
|
{
|
|
string str = s.Remove(0, 2);//删除最前面两个"%u"
|
string[] strArr = str.Split(new string[] { "%u" }, StringSplitOptions.None);//以子字符串"%u"分隔
|
byte[] byteArr = new byte[strArr.Length * 2];
|
for (int i = 0, j = 0; i < strArr.Length; i++, j += 2)
|
{
|
byteArr[j + 1] = Convert.ToByte(strArr[i].Substring(0, 2), 16); //把十六进制形式的字串符串转换为二进制字节
|
byteArr[j] = Convert.ToByte(strArr[i].Substring(2, 2), 16);
|
}
|
str = System.Text.Encoding.Unicode.GetString(byteArr); //把字节转为unicode编码
|
return str;
|
|
}
|
#endregion
|
|
#region MD5混淆
|
/// <summary>
|
/// MD5混淆
|
/// </summary>
|
/// <param name="str">要加密的字符串</param>
|
/// <returns>MD5混淆</returns>
|
public static string MD5EnCode(string str)
|
{
|
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
|
byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(str));
|
StringBuilder sBuilder = new StringBuilder();
|
for (int i = 0; i < data.Length; i++)
|
sBuilder.Append(data[i].ToString("x2"));
|
return sBuilder.ToString();
|
}
|
#endregion
|
|
#region 判断是否为汉字
|
/// <summary>
|
/// 判断是否为汉字
|
/// </summary>
|
/// <param name="chrStr">待检测字符串</param>
|
/// <returns>是汉字返回true</returns>
|
public static bool IsChineseCharacters(string chrStr)
|
{
|
Regex CheckStr = new Regex("[\u4e00-\u9fa5]");
|
return CheckStr.IsMatch(chrStr);
|
}
|
#endregion
|
|
#region 得到每个汉字的字首拼音码字母(大写)
|
/// <summary>
|
/// 得到每个汉字的字首拼音码字母(大写)
|
/// </summary>
|
/// <param name="chrStr">输入字符串</param>
|
/// <returns>返回结果</returns>
|
public static string GetHeadCharacters(string chrStr)
|
{
|
string strHeadString = string.Empty;
|
|
Encoding gb = System.Text.Encoding.GetEncoding("gb2312");
|
for (int i = 0; i < chrStr.Length; i++)
|
{
|
//检测该字符是否为汉字
|
if (!IsChineseCharacters(chrStr.Substring(i, 1)))
|
{
|
strHeadString += chrStr.Substring(i, 1);
|
continue;
|
}
|
|
byte[] bytes = gb.GetBytes(chrStr.Substring(i, 1));
|
string lowCode = System.Convert.ToString(bytes[0] - 0xA0, 16);
|
string hightCode = System.Convert.ToString(bytes[1] - 0xA0, 16);
|
int nCode = Convert.ToUInt16(lowCode, 16) * 100 + Convert.ToUInt16(hightCode, 16); //得到区位码
|
strHeadString += FirstLetter(nCode);
|
}
|
return strHeadString;
|
}
|
#endregion
|
|
#region 通过汉字区位码得到其首字母(大写)
|
/// <summary>
|
/// 通过汉字区位码得到其首字母(大写)
|
/// </summary>
|
/// <param name="nCode">汉字编码</param>
|
/// <returns></returns>
|
public static string FirstLetter(int nCode)
|
{
|
if (nCode >= 1601 && nCode < 1637) return "A";
|
if (nCode >= 1637 && nCode < 1833) return "B";
|
if (nCode >= 1833 && nCode < 2078) return "C";
|
if (nCode >= 2078 && nCode < 2274) return "D";
|
if (nCode >= 2274 && nCode < 2302) return "E";
|
if (nCode >= 2302 && nCode < 2433) return "F";
|
if (nCode >= 2433 && nCode < 2594) return "G";
|
if (nCode >= 2594 && nCode < 2787) return "H";
|
if (nCode >= 2787 && nCode < 3106) return "J";
|
if (nCode >= 3106 && nCode < 3212) return "K";
|
if (nCode >= 3212 && nCode < 3472) return "L";
|
if (nCode >= 3472 && nCode < 3635) return "M";
|
if (nCode >= 3635 && nCode < 3722) return "N";
|
if (nCode >= 3722 && nCode < 3730) return "O";
|
if (nCode >= 3730 && nCode < 3858) return "P";
|
if (nCode >= 3858 && nCode < 4027) return "Q";
|
if (nCode >= 4027 && nCode < 4086) return "R";
|
if (nCode >= 4086 && nCode < 4390) return "S";
|
if (nCode >= 4390 && nCode < 4558) return "T";
|
if (nCode >= 4558 && nCode < 4684) return "W";
|
if (nCode >= 4684 && nCode < 4925) return "X";
|
if (nCode >= 4925 && nCode < 5249) return "Y";
|
if (nCode >= 5249 && nCode < 5590) return "Z";
|
return "";
|
}
|
#endregion
|
|
#region 格式化显示数字
|
/// <summary>
|
/// 格式化显示数字
|
/// </summary>
|
/// <param name="objValue">要转换的值</param>
|
/// <param name="num">小数位数</param>
|
/// <returns>格式化后的字符串</returns>
|
/// <history>[HanHe(张蒙蒙)] ADD 2014/08/11</history>
|
public static string FormatDecimal(object objValue, int num)
|
{
|
if (objValue != null && (!objValue.ToString().Equals("")))
|
{
|
if (decimal.Parse(objValue.ToString()) == 0)
|
{
|
return "0.00";
|
}
|
else
|
{
|
string formatstr = "";
|
for (int i = 0; i < num; i++)
|
{
|
formatstr += "0";
|
}
|
return (decimal.Parse(objValue.ToString())).ToString("N" + num.ToString());
|
}
|
}
|
return "暂无报价";
|
}
|
#endregion 格式化显示数字
|
|
#region 格式化显示百分比数字
|
/// <summary>
|
/// 格式化显示百分比数字
|
/// </summary>
|
/// <param name="objValue">要转换的值</param>
|
/// <param name="num">小数位数</param>
|
/// <param name="nullStr">为空值或空串时显示的值</param>
|
/// <param name="zeroStr">为0时显示的值</param>
|
/// <returns></returns>
|
public static string FormatDecimalPercent(object objValue, int num, string nullStr, string zeroStr)
|
{
|
if (objValue != null && (!objValue.ToString().Equals("")))
|
{
|
if (((decimal)objValue) == 0)
|
{
|
return zeroStr;
|
}
|
else
|
{
|
string formatstr = "";
|
for (int i = 0; i < num; i++)
|
{
|
formatstr += "0";
|
}
|
return ((decimal)objValue).ToString("F" + num.ToString()) + "%";
|
}
|
}
|
return nullStr;
|
}
|
#endregion
|
|
#region 更新业务日期添加时分秒
|
/// <summary>
|
/// 更新业务日期添加时分秒
|
/// </summary>
|
/// <param name="dtOp">业务日期</param>
|
/// <returns></returns>
|
public static DateTime AddOPTime(DateTime dtOp)
|
{
|
DateTime dtDate;
|
string sTime = DateTime.Now.TimeOfDay.ToString();
|
string sDate = dtOp.ToString("yyyy-MM-dd");
|
|
sDate = sDate + " " + sTime;
|
|
dtDate = DateTime.Parse(sDate);
|
return dtDate;
|
}
|
#endregion
|
|
#region 获得随机码
|
/// <summary>
|
/// 获得随机码
|
/// </summary>
|
/// <returns></returns>
|
public static string CreateCode(int length)
|
{
|
string Code = string.Empty;
|
|
length = length - 1;
|
string[] selectChar = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
|
for (int i = 0; i < length; i++)
|
{
|
Thread.Sleep(10);
|
int index = new Random().Next(0, selectChar.Length); //生成随机下标
|
Code += selectChar[index];
|
}
|
return Code;
|
}
|
#endregion
|
|
#region 反射赋值
|
/// <summary>
|
/// 反射赋值
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="S"></typeparam>
|
/// <param name="target"></param>
|
/// <param name="start"></param>
|
/// <param name="filterProptyName"></param>
|
public static void ConvertMappingData<T, S>(T target, S start, List<string> filterProptyName = null)
|
{
|
var targetProp = target.GetType().GetProperties();
|
var startProp = start.GetType().GetProperties();
|
foreach (var sp in startProp)
|
{
|
foreach (var tp in targetProp)
|
{
|
if (filterProptyName != null && filterProptyName.Contains(sp.Name))
|
{
|
break;
|
}
|
if (sp.Name.Equals(tp.Name))
|
{
|
tp.SetValue(target, sp.GetValue(start));
|
}
|
}
|
}
|
}
|
#endregion
|
|
#region 获取dynamic值
|
/// <summary>
|
/// 获取dynamic值
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="data"></param>
|
/// <returns></returns>
|
public T DynamicData<T>(dynamic data)
|
{
|
return JsonConvert.DeserializeObject<T>(Util.ToString(data));
|
}
|
#endregion
|
|
/// <summary>
|
/// 是否为数字
|
/// </summary>
|
/// <param name="value"></param>
|
/// <returns></returns>
|
public static bool IsNumber(object value)
|
{
|
try
|
{
|
Convert.ToDouble(value);
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 把object格式化成字符串,去除引号防止sql注入
|
/// </summary>
|
/// <param name="value">数据对象</param>
|
/// <returns>格式化后字符串</returns>
|
/// <history>
|
/// [hanhe(祝焕)] 2010/10/18 Created
|
/// </history>
|
public static string ToReplaceSymbolStr(object value)
|
{
|
if (value == null || value == DBNull.Value)
|
return string.Empty;
|
|
if (value is DateTime)
|
return ToString(value, "yyyy-MM-dd");
|
|
if (value is Decimal)
|
return ToString(value, "n2");
|
|
string result = value.ToString();
|
return result.Replace("'", "");
|
}
|
}
|
}
|