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; }
}
}
}