Hi,
I encountered some issues to check if a cell is empty. I noticed that Checkcell can return null but also a Cell of IsNull type
So it seems that to check if a cell is empty, it necessary to have a double check
public static bool IsCellNull(Worksheet ws, int r, int c) {
bool res = true;
Cell cellnull = ws.Cells.CheckCell(r,c);
if (cellnull != null) {
if (cellnull.Type != CellValueType.IsNull) {
res = false;
}
}
return res;
}
Am I right ?
Is there another way to check if a cell is empty ?
Thanks
Olivier
@olivier57
Cells.CheckCell(int row, int column): Return Cell object if a Cell object exists. Return null if the cell does not exist.
If there is a cell object and its value is null, then Cell.Type will get in CellValueType.IsNull enumeration type.
So if you need to determine whether a cell is empty or a cell value is empty, you can use the following sample code.
public static bool IsCellNull(Worksheet ws, int r, int c)
{
Cell cellnull = ws.Cells.CheckCell(r, c);
if (cellnull == null || cellnull.Type == CellValueType.IsNull)
{
return true;
}
return false;
}
Hope helps a bit.
Thnaks for this answer. Problem is that if checkcell returns null, cellnull.Type will throw an error. That’s why I did a two steps checking. In my opinion, checkcell should never return null but only a cell with IsNull Type
@olivier57
For performance and memory considerations, we will not build non-existent cells. If you need to have a cell object every time you visit, you can use the Cells[string cellName] and Cells[int row, int column] methods to get the cell object. But if you obtain cell objects on a large scale through the two methods, it will build many cell objects which are non-existent before , which will also affect performance.