简体   繁体   English

如何从 Excel 的 DataTable 中删除空行?

[英]How can i remove empty rows from DataTable from Excel?

I have the same problem.我也有同样的问题。 But the differences is that my empty rows are in the middle and I have over 50 columns.但不同之处在于我的空行在中间,我有 50 多列。 The user wants to see the duplicated rows, so that means I cannot use SELECT DISTINCT * FROM [excel]用户想要查看重复的行,这意味着我不能使用 SELECT DISTINCT * FROM [excel]

The empty rows could be anywhere.空行可以在任何地方。 The largest excel I faced with so far have over 100,000 rows.迄今为止我遇到的最大的 excel 有超过 100,000 行。

Is there a more efficient way to REMOVE the empty rows or MUST I loop through and check all columns in each row??有没有更有效的方法来删除空行,或者我必须循环检查每行中的所有列?

   void SelectDataFromExcel()
        {

            string connectionString = ConfigurationSettings.AppSettings["ExcelConn"].ToString();
            OleDbConnection excelConnection = new OleDbConnection(connectionString);
            excelConnection.Open();   
            OleDbCommand dbCommand;
            OleDbDataAdapter dataAdapter;
             foreach (var sheet in Sheets)
            {
                dbCommand = new OleDbCommand("select DISTINCT* From[" + sheet + "$]", excelConnection);
                System.Threading.Thread.Sleep(1000);
                this.Invoke((MethodInvoker)delegate
                {
                    listBox1.Items.Add("Tablo ismi: " + sheet.ToUpper(CultureInfo.InvariantCulture) + " Tablo Satır Sayısı: "+ dSet.Tables[sheet].Rows[0][0].ToString());
                });
                dataAdapter = new OleDbDataAdapter(dbCommand);
                dTable = new DataTable();
                dataAdapter.Fill(dTable);
                dTable.TableName = sheet.ToUpper(CultureInfo.InvariantCulture);;

                ArrangedDataList(dTable);
                FillSqlTable(dTable, dTable.TableName);
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
            }

            excelConnection.Close();
            excelConnection.Dispose();
            t1.Abort();
        }

EDIT编辑

This will remove all rows that which each of it's columns contain either nothing or white space:这将删除其每一列不包含任何内容或空白的所有行:

dataTable = dataTable.Rows.Cast<DataRow>().
    Where(row => !row.ItemArray.All(field => field is System.DBNull ||   
          string.Compare((field as string).Trim(), string.Empty) ==
                                                      0)).CopyToDataTable();

Pre

one way to achive is to check for empty value实现的一种方法是检查空值

DataView dv = null;
dv = new DataView();

{
    dv.Table = myDatatable;
    dv.AllowDelete = true;
    dv.AllowEdit = true;
    dv.AllowNew = true;
    dv.RowFilter = "myField = ' '";
}

Try This尝试这个

for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
    if (dt.Rows[i][0].ToString() == String.Empty )
    {
        dt.Rows.RemoveAt(i);
    }
}

这也对我有用。

 DataTable dt = table.Rows.Cast<DataRow>().Where(r => string.Join("",  r.ItemArray).Trim() != string.Empty).CopyToDataTable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM