简体   繁体   中英

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. The user wants to see the duplicated rows, so that means I cannot use SELECT DISTINCT * FROM [excel]

The empty rows could be anywhere. The largest excel I faced with so far have over 100,000 rows.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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