[英]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.