1.去除datatable 里面的空行数据。
public static void RemoveNullOrEmptyRows(this DataTable table)
{
List
for (int i = 0; i < table.Rows.Count; i++)
{
bool rowdataisnull = true;
for (int j = 0; j < table.Columns.Count; j++)
{
if (!string.IsNullOrEmpty(table.Rows[i][j].ToString().Trim()))
{
rowdataisnull = false;
}
}
if (rowdataisnull)
{
removelist.Add(table.Rows[i]);
}
}
for (int i = 0; i < removelist.Count; i++)
{
table.Rows.Remove(removelist[i]);
}
}
2.去除datatable 里面column 为英文的列
public static void RemoveEnglishUseListColumns(this DataTable table)
{
List
foreach (DataColumn item in table.Columns)
{
if (Regex.Matches(item.ColumnName, "[a-zA-Z]").Count > 0)
{
removeNames.Add(item.ColumnName);
}
}
foreach (string removeName in removeNames)
{
table.Columns.Remove(removeName);
}
}
3.datatable column 中英文对照
public static DataTable ColumnMapping(this DataTable table,IDictionary
{
if(exceptColumns != null)
{
foreach (string item in exceptColumns)
{
table.Columns.Remove(item);
}
}
foreach (var item in columnMappings)
{
if (table.Columns.Contains(item.Key))
{
table.Columns[item.Key].ColumnName = item.Value;
}
}
return table;
}
具体使用方式。
table.ColumnMapping(FieldConfiguration.FieldDescription()).RemoveEnglishUseListColumns()