list转换成DataTable
list转换成DataTable类如下:
public static DataTable ToDataTable<T>(this IList<T> datas)
{
DataTable dt = new DataTable();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
List<KeyValuePair<PropertyInfo, ExportFieldAttribute>> propertyInfos = new List<KeyValuePair<PropertyInfo, ExportFieldAttribute>>(); //获取需要导出列信息
foreach (PropertyInfo propertyInfo in properties)
{
ExportFieldAttribute attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ExportFieldAttribute)) as ExportFieldAttribute;
if (attribute != null)
{
propertyInfos.Add(new KeyValuePair<PropertyInfo, ExportFieldAttribute>(propertyInfo, attribute));
}
} propertyInfos= propertyInfos.OrderBy(p => p.Value.Sort).ToList();//列排序 //添加列
propertyInfos.ForEach(p =>
{
DataColumn dataColumn = new DataColumn(p.Key.Name, p.Key.PropertyType);
dt.Columns.Add(dataColumn);
} ); datas.ToList().ForEach(data =>
{
DataRow dtRow = dt.NewRow();
foreach (PropertyInfo propertyInfo in propertyInfos.Select(p => p.Key))
{
dtRow[propertyInfo.Name] = propertyInfo.GetValue(data, null);
}
dt.Rows.Add(dtRow);
}
);
////填充数据
//foreach (T data in datas)
//{
// DataRow dtRow = dt.NewRow();
// foreach (PropertyInfo propertyInfo in propertyInfos.Select(p=>p.Key))
// {
// dtRow[propertyInfo.Name]=propertyInfo.GetValue(data, null);
// }
// dt.Rows.Add(dtRow);
//} return dt;
}
属性类如下:
public class ExportFieldAttribute : Attribute
{
public ExportFieldAttribute()
{
} public ExportFieldAttribute(int sort)
{
Sort = sort;
}
public int Sort { get; set; }//排序
}
属性标记的类如下:
public class DiscardStatics
{
private string _sn; [ExportFieldAttribute(Sort=)]
public string Sn
{
get { return _sn; }
set { _sn = value; }
} private string _propertyName; [ExportFieldAttribute()]
public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
private string aa { get; set; } private string _type;
[ExportFieldAttribute()]
public string Type
{
get { return _type; }
set { _type = value; }
} private DateTime _discardTime;
[ExportFieldAttribute()]
public DateTime DiscardTime
{
get { return _discardTime; }
set { _discardTime = value; }
} private double residuals;
[ExportFieldAttribute()]
public double Residuals
{
get { return residuals; }
set { residuals = value; }
} private string _propertyId;
public string PropertyId
{
get { return _propertyId; }
set { _propertyId = value; }
} private string _brand;
public string Brand
{
get { return _brand; }
set { _brand = value; }
} private string _supplier;
public string Supplier
{
get { return _supplier; }
set { _supplier = value; }
} private string _contactInfo;
public string ContactInfo
{
get { return _contactInfo; }
set { _contactInfo = value; }
} private string _childPtyId;
public string ChildPtyId
{
get { return _childPtyId; }
set { _childPtyId = value; }
} private string _childPtyName;
public string ChildPtyName
{
get { return _childPtyName; }
set { _childPtyName = value; }
}
}
list转换成DataTable的更多相关文章
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...
- C#_List转换成DataTable
/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list" ...
- 将List转换成DataTable
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据
领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其 ...
- C# DataTable转换成实体列表 与 实体列表转换成DataTable
/// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T"&g ...
- 获取报告 Stream转string,利用字符串分割转换成DataTable
protected void Button1_Click(object sender, EventArgs e) { MemoryStream stream = new MemoryStream(); ...
- C#:CsvReader读取.CSV文件并转换成DataTable
原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...
- 将泛类型集合List类转换成DataTable
/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...
- C#:CsvReader读取.CSV文件(转换成DataTable)
原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...
随机推荐
- 数据对象ajax学习篇9
题记:写这篇博客要主是加深自己对数据对象的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 对ajax工作道理: 下面这段是来自一个网友的blog: Ajax的道理简单来说通过Xml ...
- 一位Erlang程序猿的自白
12.00 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style De ...
- Linux 可执行文件 ELF结构 及程序载入执行
Linux下ELF文件类型分为以下几种: 1.可重定位文件,比如SimpleSection.o: 2.可运行文件,比如/bin/bash. 3.共享目标文件,比如/lib/libc.so. 在Linu ...
- sql server常用知识点
--删除表 use [20130823_Recource] go drop table my_table1,my_table2,My_table3 --创建表 use [20130823_Recour ...
- 内存管理和@property的属性
内存管理和@property的属性 目录 对内存管理的理解 Objective C内存管理方式 内存的管理 对象的所有权和内存管理原则 合理解决内存管理带来的问题 自动释放池 @property的属性 ...
- android Unable to instantiate application
最近一段时间在做项目时候遇到一个错误老是解决不了,log如下: FATAL EXCEPTION: main12-11 09:08:53.922 E/AndroidRuntime( 1227): jav ...
- 激活office 2010
在激活office 2010版本时有的同学可能会用到“mini-KMS_Activator”这个工具,其实这个工具激活成功率并不算高,我就没激活成功.然后就是了另一款自动激活工具“Office 201 ...
- MySQL 5.7 深度解析: JSON数据类型使用
http://www.actionsky.com/docs/archives/156 2015年12月25日 杨涛涛 JSON (JavaScriptObject Notation) 是一种轻量级 ...
- Linux下杀僵尸进程办法
1) 检查当前僵尸进程信息 # ps -ef | grep defunct | grep -v grep | wc -l 175 # top | head -2 top - 15:05:54 up 9 ...
- 创建MS Office 和 WPS 兼容插件
在工作中我们要实现一个功能,需要创建MS Office 和 WPS 兼容插件,也就是创建一个DLL,可以同时兼容office和wps.这样带来的好处就是只需要维护同一份代码,大大降低维护的工作! 1. ...