reading a empty cell, gives object reference error

2020-07-07 10:42发布

问题:

When I read a cell by doing Worksheets.Cells[2,5].value.ToString();

I get a error "System.NullReferenceException: Object reference not set to an instance of an object."

What would be a good way to check for null and then assign the value, without having to have a "if" statement.

回答1:

 string strValue = Worksheets.Cells[2,5].value==null ? string.Empty : Worksheets.Cells[2,5].value.ToString();

or

object objValue = Worksheets.Cells[2,5].value ?? string.Empty


标签: c# linq epplus