• DataUtil


      1 public static class DataUtil
      2     {
      3         #region dataConvert
      4         /// <summary>
      5         /// 从数据行返回数据实体对象
      6         /// </summary>
      7         /// <typeparam name="T">数据实体类型</typeparam>
      8         /// <param name="row">数据行</param>
      9         /// <returns>数据实体对象</returns>
     10         public static T GetModel<T>(DataRow row) where T : class, new()
     11         {
     12             if (row == null) return null;
     13 
     14             T result = new T();
     15 
     16             Type type = typeof(T);
     17 
     18             System.Reflection.PropertyInfo[] ps = type.GetProperties();
     19 
     20             foreach (System.Reflection.PropertyInfo p in ps)
     21             {
     22                 if (row.Table.Columns.Contains(p.Name))
     23                 {
     24                     object value = row[p.Name];
     25 
     26                     if (!(value is DBNull))
     27                     {
     28                         try
     29                         {
     30                             p.SetValue(result, value, null);
     31                         }
     32                         catch
     33                         {
     34 
     35                         }
     36                     }
     37                 }
     38             }
     39 
     40             return result;
     41         }
     42         /// <summary>
     43         /// 从数据表返回数据实体列表对象
     44         /// </summary>
     45         /// <typeparam name="T">数据实体类型</typeparam>
     46         /// <param name="dt">数据表</param>
     47         /// <returns>数据实体列表对象</returns>
     48         public static List<T> GetModelList<T>(DataTable dt) where T : class, new()
     49         {
     50             List<T> list = new List<T>();
     51             foreach (DataRow row in dt.Rows)
     52             {
     53                 list.Add(GetModel<T>(row));
     54             }
     55             return list;
     56         }
     57 
     58         /// <summary>
     59         /// 从数据行集合返回数据实体列表对象
     60         /// </summary>
     61         /// <typeparam name="T">数据实体类型</typeparam>
     62         /// <param name="rows">数据行集合</param>
     63         /// <returns>数据实体列表对象</returns>
     64         public static List<T> GetModelList<T>(IEnumerable<DataRow> rows) where T : class, new()
     65         {
     66             List<T> list = new List<T>();
     67             foreach (DataRow row in rows)
     68             {
     69                 list.Add(GetModel<T>(row));
     70             }
     71             return list;
     72         }
     73         #endregion
     74     }
     75     public static class CommonUtil
     76     {
     77         public static string GetData(string url)
     78         {
     79             string sReturn;
     80             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
     81             req.ContentType = "multipart/form-data";
     82             WebResponse resp = req.GetResponse();
     83             StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
     84             sReturn = sr.ReadToEnd().Trim();
     85             resp.Close();
     86             sr.Close();
     87             return sReturn;
     88         }
     89         /// <summary>
     90         /// 时间戳转为C#格式时间
     91         /// </summary>
     92         /// <param name=”timeStamp”></param>
     93         /// <returns></returns>
     94         public static DateTime GetTime(string timeStamp)
     95         {
     96             DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
     97             long lTime = long.Parse(timeStamp + "0000000");
     98             TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
     99         }
    100 
    101         /// <summary>
    102         /// DateTime时间格式转换为Unix时间戳格式
    103         /// </summary>
    104         /// <param name=”time”></param>
    105         /// <returns></returns>
    106         public static int ConvertDateTimeInt(System.DateTime time)
    107         {
    108             System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
    109             return (int)(time - startTime).TotalSeconds;
    110         }
    111         public static string SubStr(string content, int length)
    112         {
    113             return NoHTML(content).Length > length ? NoHTML(content).Trim().Substring(0, length) + "..." : NoHTML(content);
    114         }
    115         public static string NoHTML(string Htmlstring)
    116         {
    117             //删除脚本   
    118             Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
    119             //删除HTML   
    120             Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
    121             Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
    122             Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
    123             Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
    124             Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/", RegexOptions.IgnoreCase);
    125             Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
    126             Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
    127             Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
    128             Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
    129             Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
    130             Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
    131             Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
    132             Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
    133             Htmlstring = Regex.Replace(Htmlstring, @"&#(/d+);", "", RegexOptions.IgnoreCase);
    134             //替换掉 < 和 > 标记
    135             Htmlstring.Replace("<", "");
    136             Htmlstring.Replace(">", "");
    137             Htmlstring.Replace("/r/n", "");
    138             //返回去掉html标记的字符串
    139             return Htmlstring;
    140         }
    141         public static string GetCharABC(int index)
    142         {
    143             if (index > 25) { 
    144                 return  (index+1).ToString(); }
    145             string[] abcStr = new string[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    146             return abcStr[index];
    147         }
    148         public static string GetShortTime(DateTime time)
    149         {
    150             try
    151             {
    152                 if (time.ToShortDateString() == DateTime.Now.ToShortDateString())
    153                 {
    154                     return time.ToString("HH:mm");
    155                 }
    156                 else if (time.Year != DateTime.Now.Year) {
    157                     return time.ToString("yyyy-MM-dd");
    158                 }
    159                 else
    160                 {
    161                     return time.ToString("MM-dd");
    162                 }
    163             }
    164             catch (Exception)
    165             {
    166                 return DateTime.Now.ToString("MM-dd");
    167             }
    168 
    169          
    170         }
    171         public static string ContentTypConvert(string ptype)
    172         {
    173             switch (ptype)
    174             {
    175                 case "application/msword":
    176                     ptype = ".doc";
    177                     break;
    178                 case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
    179                     ptype = ".docx";
    180                     break;
    181                 case "application/pdf":
    182                     ptype = ".pdf";
    183                     break;
    184                 case "application/vnd.ms-excel":
    185                     ptype = ".xls";
    186                     break;
    187                 case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
    188                     ptype = ".xlsx";
    189                     break;//application/octet-stream 和 application/x-zip-compressed
    190                 case "application/octet-stream":
    191                     ptype = ".rar";
    192                     break;
    193                 case "application/x-zip-compressed":
    194                     ptype = ".zip";
    195                     break;
    196                 case "image/jpeg":
    197                     ptype = ".jpg";
    198                     break;
    199                 case "image/png":
    200                     ptype = ".png";
    201                     break;
    202                 default:
    203                     ptype = "";
    204                     break;
    205             } 
    206             return ptype;
    207         }
    208         /// <summary>
    209         /// 特殊字符清空
    210         /// </summary>
    211         /// <param name="strMessage"></param>
    212         /// <returns></returns>
    213         public static string StringClear(string strMessage)
    214         {
    215             string[] aryReg = { "!", "@", "#", "$", "^", "*", "'", "(", ")", "<", ">", "%", """", ",", ">=", "=<", "-", "_", ";", "||", "[", "]", "&", "/", "-", "|", "?", ":", " ", };
    216             for (int i = 0; i < aryReg.Length; i++)
    217             {
    218                 strMessage = strMessage.Replace(aryReg[i], string.Empty);
    219             }
    220             return strMessage;
    221         }
    222        
    223         public static List<string> GetStrImgList(string html)
    224         {
    225             List<string> list = new List<string>();
    226             Regex regImg = new Regex(@"<img[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^s	
    ""'<>]*)[^<>]*?/?[s	
    ]*>", RegexOptions.IgnoreCase);
    227             // 搜索匹配的字符串 
    228             MatchCollection matches = regImg.Matches(html);
    229             // 取得匹配项列表 
    230             foreach (Match match in matches) {
    231                 list.Add(match.Groups["imgUrl"].Value);
    232             }
    233             return list;
    234         }
    235     }
    View Code
    收藏
    关注
    评论
  • 相关阅读:
    Jackson 框架,轻易转换JSON
    Java Thread 多线程 操作线程
    Oracle笔记 四、增删改、事务
    Oracle笔记 六、PL/SQL简单语句块、变量定义
    Java 的JSON、XML转换方法——目录索引
    SpringMVC 中整合JSON、XML视图一
    FreeMarker整合Spring 3
    Java Thread 多线程 介绍
    Jaxb2 转换XML文档
    Java Thread 多线程同步、锁、通信
  • 原文地址:https://www.cnblogs.com/yidengbone/p/6600795.html
Copyright © 2020-2023  润新知