• ImportCommon


    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WLYD.Utility.File
    {
        public class ImportCommon
        {
    
            public static string TitleRequiredValidate(PropertyInfo[] properties, Dictionary<int, string> titleDic)
            {
                foreach (var property in properties)
                {
                    var attr = (DisplayAttribute)property.GetCustomAttribute(typeof(DisplayAttribute), false);
                    if (attr != null && !string.IsNullOrEmpty(attr.Name))
                    {
                        if (!titleDic.Values.Any(h => h == attr.Name))
                        {
                            return attr.Name;
                        }
                    }
                }
                return null;
            }
    
            public static object StringToPropertyValue(PropertyInfo property, string src)
            {
                object obj = null;
                switch (property.PropertyType.Name)
                {
                    case "String":
                        return src;
                    case "Decimal":
                        return decimal.Parse(src);
                    case "Int32":
                        return int.Parse(src);
                    case "DateTime":
                        return DateTime.Parse(src);
                    case "Boolean":
                        return bool.Parse(src);
                }
                return obj;
            }
    
            public static string GetPropertyByExcelName(PropertyInfo[] properties, string excelTitleName)
            {
                foreach (var property in properties)
                {
                    var attr = (DisplayAttribute)property.GetCustomAttribute(typeof(DisplayAttribute), false);
                    if (attr != null && attr.Name == excelTitleName)
                    {
                        return property.Name;
                    }
                }
                return null;
            }
    
            /// <summary>
            /// 获取流编码
            /// </summary>
            /// <param name="fs"></param>
            /// <returns></returns>
            public static Encoding GetStreamEncode(Stream fs)
            {
                System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
    
                Byte[] buffer = br.ReadBytes(2);
    
                if (buffer[0] >= 0xEF)
    
                {
    
                    if (buffer[0] == 0xEF && buffer[1] == 0xBB)
    
                    {
    
                        return System.Text.Encoding.UTF8;
    
                    }
    
                    else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
    
                    {
    
                        return System.Text.Encoding.BigEndianUnicode;
    
                    }
    
                    else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
    
                    {
    
                        return System.Text.Encoding.Unicode;
    
                    }
    
                    else
    
                    {
    
                        return System.Text.Encoding.Default;
    
                    }
    
                }
    
                else
    
                {
    
                    return System.Text.Encoding.Default;
    
                }
            }
        }
    }
    

      

  • 相关阅读:
    2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) B
    openJudge C17K:Lying Island
    拓扑排序
    cdoj1638 红藕香残玉簟秋,轻解罗裳,独上兰舟。
    poj3159 Candies
    poj1364 King
    Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
    Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
    Codeforces Round #416 (Div. 2) B. Vladik and Complicated Book
    hdu4417 Super Mario
  • 原文地址:https://www.cnblogs.com/wangpd/p/6867378.html
Copyright © 2020-2023  润新知