using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace CommonHelper.Format
|
{
|
public class ModelHandler<T> where T : new()
|
{
|
/// <summary>
|
/// 根据实体类得到表结构
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
private static DataTable CreateData(T model)
|
{
|
DataTable dataTable = new DataTable(typeof(NPOI.SS.Formula.Functions.T).Name);
|
foreach (System.Reflection.PropertyInfo propertyInfo in typeof(NPOI.SS.Formula.Functions.T).GetProperties())
|
{
|
dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
|
}
|
|
return dataTable;
|
}
|
|
/// <summary>
|
/// 实体类转换成DataTable
|
/// </summary>
|
/// <param name="modelList">实体类列表</param>
|
/// <returns></returns>
|
public static DataTable FillDataTable(List<T> modelList)
|
{
|
if (modelList == null || modelList.Count == 0)
|
{
|
return null;
|
}
|
|
DataTable dt = CreateData(modelList[0]);
|
|
foreach (T model in modelList)
|
{
|
DataRow dataRow = dt.NewRow();
|
foreach (System.Reflection.PropertyInfo propertyInfo in typeof(NPOI.SS.Formula.Functions.T).GetProperties())
|
{
|
dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
|
}
|
|
dt.Rows.Add(dataRow);
|
}
|
|
return dt;
|
}
|
}
|
}
|