Search code examples
c#-4.0openxml-sdk

Reading excel file using OpnenXML -Reads rows which are blank


Am using OpenXML to read Excel file, but getting some problem that it reads row which did not contain data. bellow is the code which reads the row.

 var firstOrDefault = sourceWorksheetPart.Worksheet.Elements<SheetData>().FirstOrDefault();
            if (firstOrDefault != null)
            {
                var sourceWorksheetRows = firstOrDefault.Elements<Row>();}

here when I count the sourceWorksheetRows it gives more count then records present in excel sheet. I tried to check null but its not working. How to implement the check.


Solution

  • To check if the row is an empty record, just check the first cell (or any cell which is mandatory for the record) of the row, then check if CellValue is null or if "Trim(CellValue.Text).length == 0".

    This is the solution I advise.