昨天向数据库中导入Excel数据时 由于空行 总是报错!下面附上两种去除空行的方法!
方法一、某行某列值为空时
DataView dv = dt.DefaultView;
dv.RowFilter = "F1<>'' or F1 is not null";//F1为列名
System.Data.DataTable dt = dv.ToTable();
方法二、整行为空时
protected void RemoveEmpty(DataTable dt) { List<DataRow> removelist = new List<DataRow>(); for (int i = 0; i < dt.Rows.Count; i++) { bool IsNull = true; for (int j = 0; j < dt.Columns.Count; j++) { if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim())) { IsNull = false; } } if (IsNull) { removelist.Add(dt.Rows[i]); } } for (int i = 0; i < removelist.Count; i++) { dt.Rows.Remove(removelist[i]); } }