How to upload only non-empty rows of Excel spreadsheet using oledb in C#?

前端 未结 4 1956
遥遥无期
遥遥无期 2021-01-02 07:59

I am importing excel sheet to DataTable using oledb connection as below.

private static DataTable UploadExcelSheet(string fileName)
    {
        DataTable u         


        
相关标签:
4条回答
  • 2021-01-02 08:35

    How about filtering the rows after the query has executed using Linq to object:

    var filteredRows = uploadDataTable.Rows.Cast<DataRow>().Where(
      row => row.ItemArray.Any(field => !(field is System.DBNull)));
    
    0 讨论(0)
  • 2021-01-02 08:37

    Expanding on the previous answers, this worked for me. Delete rows where all fields are null.

    Dim deleteRows = From row In result.AsEnumerable
                     Where row.ItemArray.All(Function(field) Equals(field, DBNull.Value))
    
    For Each deleteRow In deleteRows
        deleteRow.Delete()
    Next
    
    0 讨论(0)
  • 2021-01-02 08:40

    Expanding on vc's answer, 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();
    
    0 讨论(0)
  • 2021-01-02 08:57

    Use

    ".. WHERE NOT ([Lastname] = '' OR [DOB*] IS NULL OR ... )
    
    0 讨论(0)
提交回复
热议问题