545 lines
19 KiB
C#
545 lines
19 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Data;
|
||
using System.Xml;
|
||
using System.Xml.Serialization;
|
||
using System.Collections;
|
||
using System.Reflection;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace EShang.Common
|
||
{
|
||
public class DataTableHelper
|
||
{
|
||
/// <summary>
|
||
/// 复制单元格内容
|
||
/// </summary>
|
||
/// <param name="sourceRow">原单元格</param>
|
||
/// <param name="newRow">新单元格</param>
|
||
/// <param name="fieldNames">字段集合</param>
|
||
/// <returns></returns>
|
||
private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
|
||
{
|
||
foreach (string field in fieldNames)
|
||
{
|
||
newRow[field] = sourceRow[field];
|
||
}
|
||
return newRow;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable转Hashtable【传入DataRow】
|
||
/// </summary>
|
||
/// <param name="dr"></param>
|
||
/// <returns></returns>
|
||
public static Hashtable DataRowToHashTable(DataRow dr)
|
||
{
|
||
Hashtable htReturn = new Hashtable(dr.ItemArray.Length);
|
||
foreach (DataColumn dc in dr.Table.Columns)
|
||
{
|
||
htReturn.Add(dc.ColumnName, dr[dc.ColumnName]);
|
||
}
|
||
return htReturn;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable转Hashtable【传入DataTable】
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public static Hashtable DataTableToHashtable(DataTable dt)
|
||
{
|
||
Hashtable ht = new Hashtable();
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
for (int i = 0; i < dt.Columns.Count; i++)
|
||
{
|
||
string key = dt.Columns[i].ColumnName;
|
||
ht[key.ToUpper()] = dr[key];
|
||
}
|
||
}
|
||
return ht;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable转IList(Hashtable)
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public static IList<Hashtable> DataTableToArrayList(DataTable dt)
|
||
{
|
||
if (dt == null)
|
||
{
|
||
return new List<Hashtable>();
|
||
}
|
||
IList<Hashtable> datas = new List<Hashtable>();
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
Hashtable ht = DataRowToHashTable(dr);
|
||
datas.Add(ht);
|
||
}
|
||
return datas;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable转Hashtable【键值对】
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <param name="keyField"></param>
|
||
/// <param name="valFiled"></param>
|
||
/// <returns></returns>
|
||
public static Hashtable DataTableToHashtableByKeyValue(DataTable dt, string keyField, string valFiled)
|
||
{
|
||
Hashtable ht = new Hashtable();
|
||
if (dt != null)
|
||
{
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
string key = dr[keyField].ToString();
|
||
ht[key] = dr[valFiled];
|
||
}
|
||
}
|
||
return ht;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 数据行对象转化成泛型IList
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public static IList DataTableToIList<T>(DataTable dt)
|
||
{
|
||
IList list = new List<T>();
|
||
string tempName = "";
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
T obj = Activator.CreateInstance<T>();
|
||
PropertyInfo[] propertys = obj.GetType().GetProperties();
|
||
foreach (PropertyInfo pi in propertys)
|
||
{
|
||
tempName = pi.Name;
|
||
if (dt.Columns.Contains(tempName) && pi.CanWrite)
|
||
{
|
||
object value = dr[tempName];
|
||
if (value != DBNull.Value)
|
||
{
|
||
pi.SetValue(obj, value, null);
|
||
}
|
||
}
|
||
}
|
||
list.Add(obj);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 数据行对象转化成泛型List
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public static List<T> DataTableToList<T>(DataTable dt)
|
||
{
|
||
List<T> list = new List<T>();
|
||
DateTime date= Convert.ToDateTime("1900-01-01");
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
T obj = Activator.CreateInstance<T>();
|
||
PropertyInfo[] propertys = obj.GetType().GetProperties();
|
||
foreach (PropertyInfo pi in propertys)
|
||
{
|
||
string tempName = pi.Name;
|
||
if (dt.Columns.Contains(tempName) && pi.CanWrite)
|
||
{
|
||
object value = dr[tempName];
|
||
if (value != DBNull.Value)
|
||
{
|
||
try
|
||
{
|
||
if (pi.PropertyType.FullName.Contains("System.String"))
|
||
{
|
||
pi.SetValue(obj, value, null);
|
||
}
|
||
else if (pi.PropertyType.FullName.Contains("System.Int32"))
|
||
{
|
||
int ivalue = 0;
|
||
int.TryParse(value.ToString(), out ivalue);
|
||
pi.SetValue(obj, ivalue, null);
|
||
}
|
||
else if (pi.PropertyType.FullName.Contains("System.Int16"))
|
||
{
|
||
short ivalue = 0;
|
||
short.TryParse(value.ToString(), out ivalue);
|
||
pi.SetValue(obj, ivalue, null);
|
||
}
|
||
else if (pi.PropertyType.FullName.Contains("System.Decimal"))
|
||
{
|
||
decimal ivalue = 0;
|
||
decimal.TryParse(value.ToString(), out ivalue);
|
||
pi.SetValue(obj, ivalue, null);
|
||
}
|
||
else if (pi.PropertyType.FullName.Contains("System.Double"))
|
||
{
|
||
double ivalue = 0;
|
||
double.TryParse(value.ToString(), out ivalue);
|
||
pi.SetValue(obj, ivalue, null);
|
||
}
|
||
else if (pi.PropertyType.FullName.Contains("System.DateTime"))
|
||
{
|
||
DateTime ivalue = date;
|
||
DateTime.TryParse(value.ToString(), out ivalue);
|
||
pi.SetValue(obj, ivalue, null);
|
||
}
|
||
else
|
||
{
|
||
pi.SetValue(obj, value, null);
|
||
}
|
||
}
|
||
catch{ }
|
||
}
|
||
}
|
||
}
|
||
list.Add(obj);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable高效转化成List
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="dt"></param>
|
||
/// <param name="formatList">时间格式化字段参数类型("属性字段",("原格式","新的格式"))
|
||
/// 使用参考:var formatlist = new Dictionary[string,KeyValueModel]();
|
||
/// formatlist.Add("字段", new KeyValueModel() { Key = "yyyyMMdd", Value = "yyyy-MM-dd HH:mm" });
|
||
/// </param>
|
||
/// <returns></returns>
|
||
public static List<T> TableToList<T>(DataTable dt,Dictionary<string, KeyValueModel> formatList=null)
|
||
{
|
||
List<T> list = new List<T>();
|
||
string strJson = JsonHelper.DataTableToJson(dt, formatList);
|
||
list = JsonConvert.DeserializeObject<List<T>>(strJson);
|
||
return list;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// DataTable 转化成xml
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public static string DataTableToXML(DataTable dt)
|
||
{
|
||
if (dt != null)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
XmlWriter writer = XmlWriter.Create(sb);
|
||
new XmlSerializer(typeof(DataTable)).Serialize(writer, dt);
|
||
writer.Close();
|
||
return sb.ToString();
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 比较对象是否一致
|
||
/// </summary>
|
||
/// <param name="lastValues"></param>
|
||
/// <param name="currentRow"></param>
|
||
/// <param name="fieldNames"></param>
|
||
/// <returns></returns>
|
||
private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
|
||
{
|
||
for (int i = 0; i < fieldNames.Length; i++)
|
||
{
|
||
if (!((lastValues[i] != null) && lastValues[i].Equals(currentRow[fieldNames[i]])))
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 克隆DataTable
|
||
/// </summary>
|
||
/// <param name="dt">DataTable</param>
|
||
/// <param name="condition">查询条件</param>
|
||
/// <returns></returns>
|
||
public static DataTable GetNewDataTable(DataTable dt, string condition)
|
||
{
|
||
if (IsExistRows(dt))
|
||
{
|
||
if (condition.Trim() == "")
|
||
{
|
||
return dt;
|
||
}
|
||
DataTable newdt = new DataTable();
|
||
newdt = dt.Clone();
|
||
DataRow[] dr = dt.Select(condition);
|
||
for (int i = 0; i < dr.Length; i++)
|
||
{
|
||
newdt.ImportRow(dr[i]);
|
||
}
|
||
return newdt;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable 分页
|
||
/// </summary>
|
||
/// <param name="dt">DataTable</param>
|
||
/// <param name="PageIndex">当前页</param>
|
||
/// <param name="PageSize">页码</param>
|
||
/// <returns></returns>
|
||
public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
|
||
{
|
||
if (PageIndex == 0)
|
||
{
|
||
return dt;
|
||
}
|
||
DataTable newdt = dt.Copy();
|
||
newdt.Clear();
|
||
int rowbegin = (PageIndex - 1) * PageSize;
|
||
int rowend = PageIndex * PageSize;
|
||
if (rowbegin < dt.Rows.Count)
|
||
{
|
||
if (rowend > dt.Rows.Count)
|
||
{
|
||
rowend = dt.Rows.Count;
|
||
}
|
||
for (int i = rowbegin; i <= (rowend - 1); i++)
|
||
{
|
||
DataRow newdr = newdt.NewRow();
|
||
DataRow dr = dt.Rows[i];
|
||
foreach (DataColumn column in dt.Columns)
|
||
{
|
||
newdr[column.ColumnName] = dr[column.ColumnName];
|
||
}
|
||
newdt.Rows.Add(newdr);
|
||
}
|
||
}
|
||
return newdt;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断DataTable是否有数据
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public static bool IsExistRows(DataTable dt)
|
||
{
|
||
return ((dt != null) && (dt.Rows.Count > 0));
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable数据去重
|
||
/// </summary>
|
||
/// <param name="SourceTable"></param>
|
||
/// <param name="FieldNames"></param>
|
||
/// <returns></returns>
|
||
public static DataTable SelectDistinct(DataTable SourceTable, string[] FieldNames)
|
||
{
|
||
if ((FieldNames == null) || (FieldNames.Length == 0))
|
||
{
|
||
throw new ArgumentNullException("FieldNames");
|
||
}
|
||
object[] lastValues = new object[FieldNames.Length];
|
||
DataTable newTable = new DataTable();
|
||
foreach (string fieldName in FieldNames)
|
||
{
|
||
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
|
||
}
|
||
DataRow[] orderedRows = SourceTable.Select("", string.Join(",", FieldNames));
|
||
foreach (DataRow row in orderedRows)
|
||
{
|
||
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
|
||
{
|
||
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
|
||
setLastValues(lastValues, row, FieldNames);
|
||
}
|
||
}
|
||
return newTable;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable赋值
|
||
/// </summary>
|
||
/// <param name="lastValues"></param>
|
||
/// <param name="sourceRow"></param>
|
||
/// <param name="fieldNames"></param>
|
||
private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
|
||
{
|
||
for (int i = 0; i < fieldNames.Length; i++)
|
||
{
|
||
lastValues[i] = sourceRow[fieldNames[i]];
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable排序
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <param name="sorts"></param>
|
||
/// <returns></returns>
|
||
public static DataTable SortedTable(DataTable dt, params string[] sorts)
|
||
{
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
string tmp = "";
|
||
for (int i = 0; i < sorts.Length; i++)
|
||
{
|
||
tmp = tmp + sorts[i] + ",";
|
||
}
|
||
dt.DefaultView.Sort = tmp.TrimEnd(new char[] { ',' });
|
||
}
|
||
return dt;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据枚举转化对应的DataTable
|
||
/// </summary>
|
||
public static void DataTableTanslate(DataTable dataTable, Dictionary<string, Dictionary<string, string>> dictionary)
|
||
{
|
||
if (dictionary == null)
|
||
{
|
||
return;
|
||
}
|
||
string str = "_" + Guid.NewGuid().ToString().Substring(0, 10).Replace("-", "_");
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
if (dataTable.Columns.IndexOf(current) >= 0)
|
||
{
|
||
if (dataTable.Columns.IndexOf(current + str) < 0)
|
||
{
|
||
dataTable.Columns[current].ColumnName = current + str;
|
||
dataTable.Columns.Add(current, typeof(string));
|
||
}
|
||
string text = "";
|
||
if (dictionary[current].TryGetValue(dataRow[current + str].ToString(), out text))
|
||
{
|
||
dataRow[current] = text;
|
||
}
|
||
else
|
||
{
|
||
string[] array = dataRow[current + str].ToString().Split(new char[]
|
||
{
|
||
','
|
||
});
|
||
if (array.Length > 1)
|
||
{
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
if (dictionary[current].TryGetValue(array[i].ToString(), out text))
|
||
{
|
||
array[i] = text;
|
||
}
|
||
}
|
||
}
|
||
dataRow[current] = string.Join(",", array);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataRow转DataTable
|
||
/// </summary>
|
||
/// <param name="rows"></param>
|
||
/// <returns></returns>
|
||
public static DataTable ToDataTable(DataRow[] rows)
|
||
{
|
||
if (rows == null || rows.Length == 0) return null;
|
||
DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
|
||
foreach (DataRow row in rows)
|
||
tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
|
||
return tmp;
|
||
}
|
||
|
||
#region 方法 -> List转换 DataTable
|
||
/// <summary>
|
||
/// List转DataTable
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="items"></param>
|
||
/// <returns></returns>
|
||
public static DataTable ListToDataTable<T>(List<T> items)
|
||
{
|
||
var tb = new DataTable(typeof(T).Name);
|
||
|
||
System.Reflection.PropertyInfo[] props = typeof(T).GetProperties(
|
||
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
|
||
|
||
foreach (System.Reflection.PropertyInfo prop in props)
|
||
{
|
||
Type t = GetCoreType(prop.PropertyType);
|
||
tb.Columns.Add(prop.Name, t);
|
||
}
|
||
|
||
foreach (T item in items)
|
||
{
|
||
var values = new object[props.Length];
|
||
|
||
for (int i = 0; i < props.Length; i++)
|
||
{
|
||
try
|
||
{
|
||
values[i] = props[i].GetValue(item, null);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex);
|
||
}
|
||
}
|
||
|
||
tb.Rows.Add(values);
|
||
}
|
||
return tb;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取数据类型
|
||
/// </summary>
|
||
/// <param name="t"></param>
|
||
/// <returns></returns>
|
||
public static Type GetCoreType(Type t)
|
||
{
|
||
|
||
if (t != null && IsNullable(t))
|
||
{
|
||
if (!t.IsValueType)
|
||
{
|
||
return t;
|
||
}
|
||
else
|
||
{
|
||
return Nullable.GetUnderlyingType(t);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return t;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断是否为空值
|
||
/// </summary>
|
||
/// <param name="t"></param>
|
||
/// <returns></returns>
|
||
public static bool IsNullable(Type t)
|
||
{
|
||
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
|
||
}
|