How to determine whether a cell is empty or not?

Can you please advise me how I can determine whether a cell relates to an empty cell in the input spreadsheet? By empty cell, I mean that it has no value and no formatting or style.

@workshare,

Thanks for your query.

I think you may try to use Worksheet.Cells.CheckCell() method to check if the cell exists or not. CheckCell method will not instantiate the cell if it does not exist. CheckCell can return null (if a cell is not initialized) or cell object (if it exists and initialized).

This method appears to NOT work.

Let me explain what I’m trying to do. In Excel, if we have a long text value in non-wrapped cell A1 and cells B1, C1, D1, E1 and F1 are empty, the text will leak out of A1 and flow across the cells to the right until there is a value in a cell in the top row, at which point the leaked text will stop.

I need to know how to detect the empty cells B1 to F1. Your method doesn’t seem to do that. It returns a Cell object for each empty cell, even though there is no text value, no formatting and the cells have never been edited in any way.

@workshare,
First regarding the formatting in a cell, each cell in the worksheet has some default style, so that cannot be used to identify an empty cell. You may please try the Row.GetCellOrNull() function which returns either the valid cell or it returns a cell with value type IsNull. Please give it a try in your scenario and share thee feedback.

Following is a sample code which can be used to test your sample file.

Workbook wb = new Workbook("Book1.xlsx");
var val = wb.Worksheets[0].Cells.Rows[0].GetCellOrNull(0);
if (val == null)
    Console.WriteLine("Its empty cell");
else
    Console.WriteLine("Its not an empty cell");
val = wb.Worksheets[0].Cells.Rows[0].GetCellOrNull(1);
if(val == null)
    Console.WriteLine("Its empty cell");
else
    Console.WriteLine("Its not an empty cell");