.NET 下 ILIST 转 DATATABLE(原创)
public DataTable ToDataTable(IList list) {
DataTable dt = new DataTable();
if (list.Count > 0) {
foreach (IList ll in list) {
for (int c = 0; c < ll.Count; c++) {
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
}
break;
}
foreach (IList ll in list) {
DataRow dr = dt.NewRow();
for (int j = 0; j < ll.Count; j++) {
dr[j] = ll[j];
}
dt.Rows.Add(dr);
}
}
return dt;
}
这里只考虑全部是STRING类型,如果有其他类型,请用 反射 PropertyInfo[] propertys = list[0].GetType().GetProperties(); 获得原始类型!
DataTable dt = new DataTable();
if (list.Count > 0) {
foreach (IList ll in list) {
for (int c = 0; c < ll.Count; c++) {
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
}
break;
}
foreach (IList ll in list) {
DataRow dr = dt.NewRow();
for (int j = 0; j < ll.Count; j++) {
dr[j] = ll[j];
}
dt.Rows.Add(dr);
}
}
return dt;
}
这里只考虑全部是STRING类型,如果有其他类型,请用 反射 PropertyInfo[] propertys = list[0].GetType().GetProperties(); 获得原始类型!