导入单元格时在excel中检查null [英] Checking for null in excel when importing cells
问题描述
我将excel中的单元格导入数据表。
string CA =;
CA = objsheet.get_Range(B4)。get_Value()。ToString();
Datatable.Columns.Add(CaInfo,typeof(string));
Datatable.Rows.Add(CA);
它工作正常,但是当excel中的单元格为空时我有问题(Null异常错误)
我有试过这个:
if(!string.IsNullOrWhiteSpace(objsheet.get_Range(B4)。get_Value()。ToString())){CA = objsheet.get_Range 。( B4)的get_value()的ToString(); } else {CA =; }
和
if(!string.IsNullOrWhiteSpace(objsheet.get_Range(B4))。 Value2.ToString())){CA = objsheet.get_Range(B4)。get_Value()。ToString(); } else {CA =; }
和
if(!string.IsNullOrWhiteSpace(objsheet.get_Range(B4))。 Range.Value2 = null)){CA = objsheet.get_Range(B4)。get_Value()。ToString(); } else {CA =; }
但我仍然得到空例外。
可以任何人都帮我找到一种方法来检查单元格是否为null然后忽略它,或者如果它为null则将我的字符串CA更改为。
谢谢,
Hi,
I am importing cells from excel into a data table.
string CA = "" ;
CA = objsheet.get_Range("B4").get_Value().ToString();
Datatable.Columns.Add("CaInfo",typeof(string));
Datatable.Rows.Add(CA);
it works fine but I have a issue when the cell in excel is empty (Null Exception error)
I have tried this:
if (!string.IsNullOrWhiteSpace(objsheet.get_Range("B4").get_Value().ToString())) { CA = objsheet.get_Range("B4").get_Value().ToString(); } else { CA = ""; }
and
if (!string.IsNullOrWhiteSpace(objsheet.get_Range("B4").Value2.ToString())) { CA = objsheet.get_Range("B4").get_Value().ToString(); } else { CA = ""; }
and
if (!string.IsNullOrWhiteSpace(objsheet.get_Range("B4").Range.Value2 = null)) { CA = objsheet.get_Range("B4").get_Value().ToString(); } else { CA = ""; }
but I still get the null exception.
Can anyone help me to find a way to check if the cell is null and then ignore it or if it is null to change my string CA to "".
Thanks,
推荐答案
您好,
您获得null异常作为您的调用null对象的ToString()方法。
尝试测试对象的空值,而不是它的toString()方法。
......这样的事情:
Hi,
your getting the null exception as your calling the ToString() method of a null object.
Try testing for nulls on the object, rather than its toString() method.
... something like this:
if(
(objsheet.get_Range("B4")!=null)
&&
(objsheet.get_Range("B4").get_Value()!=null)
&&
(objsheet.get_Range("B4").get_Value.ToString().Length>0)
)
{
CA = objsheet.get_Range("B4").get_Value().ToString();
}
else
{
CA = "";
}
希望有所帮助。
Hope it helps.
你好,
用DBNull检查!!而不是null
Hello,
Check it with DBNull !! instead of null
这篇关于导入单元格时在excel中检查null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!