[英]Get Cell Value in Excel from Range
如何从范围对象中提取单元格的值? 看起来应该很简单。 这个Stackoverflow问题和答案并没有真正的帮助。 这是我想做的,但是每次都失败,但row.columns(0,0)上出现异常。
Dim rdr = oFindings.Range
For Each row As Excel.Range In rdr.Rows
Dim Account = row.Columns(0,0).value2.tostring
' Do other stuff
Next
为了使其工作,我必须实现以下代码:
For Each row As Excel.Range In rdr.Rows
ndx = 0
For Each cell As Excel.Range In row.Columns
If ndx = 0 Then Account = NS(cell.Value2)
' Do other stuff
ndx += 1
next
Next
这似乎太过分了。 有什么秘诀?
已经提到了大多数问题,但这是代码的答案。
Dim rdr As Excel.Range = oFindings.Range
For Each row As Excel.Range In rdr.Rows
'explicitly get the first cell as a range
Dim aCell As Excel.Range = row.Cells(1, 1)
'explicity convert the value to String but don't use .String
Dim Account as string = CStr(aCell.Value2)
If Not String.IsNullOrEmpty(Account) Then
' Do other stuff
End If
Next
如果单元格为空, Value2
使用aCell.Value2.toString
将失败,因为Value2
将为Nothing
。 而是使用不会失败的CStr(aCell.Value2)
。 但是随后您需要在使用值之前测试字符串是否为null或为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.