LinqToExcel with Generic Type not return blank rows

138 Views Asked by At

I am using LinqToExcel to extract all rows to a list, but in this case i dont want any empty rows. I have searched for any solution here in stack but with no success to my problem.

var excelSheetLst = excel.Worksheet<T>(sheetName).Where(s => s.GetPropertyValue("RegisterNumber").ToString() != "").ToList();

With this code example i have the following error:

System.Data.OleDb.OleDbException: 'External table is not in the expected format.'

Note that the type T as allways a property name RegisterNumber (string), and sheetName is variable with the name of the sheet.

EDIT:

Minimal code to produce this error

var fileNamePath = "excel.xlsx"; // This excel have empty rows
var sheetName = "Client"; // Sheet name in excel

public class Client
{
    public string RegisterNumber { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

var excel = new ExcelQueryFactory(fileNamePath);
excel.DatabaseEngine = DatabaseEngine.Ace;

var excelSheetLst = excel.Worksheet<T>(sheetName).Where(s => s.GetPropertyValue("RegisterNumber").ToString() != "").ToList();

JP.

1

There are 1 best solutions below

0
Pl4tinum On

I have resolved this with two line codes, dont know if there is a better answer to this:

var excelSheetLst = excel.Worksheet<T>(sheetName).ToList();
excelSheetLst.RemoveAll(s => s.GetPropertyValue("RegisterNumber") == null);