如何在从datagridview vb.net加载时检查excel文件单元格是否具有特定值



我希望找到一种方法来检查excel文件中的某个单元格是否包含某些值
现在我正在将excel数据拉入vb.net中的数据网格视图中。
例如,如果我的单元格中只允许数字,但有一个字母,我希望程序自动将无效条目更改为默认值。此外,我希望有一个整数变量,如果发现无效条目,它会增加一。

伪码:

First I need to define some type of integer variable named invalidcell,  
and initialize to 0.  
For each worksheet  
    For each cell in each worksheet 
        If an invalid cell is read then:
             the invalid cell value is replaced by the default value,  
             and the invalidcell is incremented by 1.
        End If
    End loop
End loop

然后,导入所有单元格后,如果invalidcell大于0,则显示一条错误消息,说明找到了多少个无效单元格。

我非常感谢任何人能提供的帮助或建议。

您可以使用IsNumeric来确定它是否是一个数字。然后只需声明一个变量来计算无效的单元格值。

Private Sub FillGridView()
'..Declare Excel application, workbook, range etc
Dim myValue as Integer = 0
Dim iCounter as Integer = 0
If IsNumeric(cell.Value2) Then
     'this is a number
     myValue = cell.Value2
Else
     'this is not a number - increment counter and set default value
     iCounter += 1
     myValue = 0
End If
If iCounter > 0 Then MsgBox(iCounter & " invalid cells were found.")
End Sub

只需使用您正在使用的任何过程将myValue添加到您的网格视图中。

相关内容

最新更新