我正在阅读有两个部分作为输入和输出部分的excel表,并将其导入两个Datagridview一个用于输入,另一个用于VB.net 2008 Windows应用程序中的输出部分。
如果输入部分有10行10列,那么在我的第11行中,我有一个类似于输入数据结束的文本'和我在第11栏中的情况一样。
所以通过检查这个,如果我得到这个字符串的行号和列号,我可以将数据导入到两个数据网格视图中。我只能在输入gridview中导入这些行和列数据。 以下是阅读excel表的代码。我不知道如何在Datatable中找到字符串。或者还有其他方法吗?
Private Sub ImpGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpGrid.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim conStr As String, sheetName As String
Dim filePath As String = "C:\SIG.XLS"
Dim extension As String = ".xls"
conStr = String.Empty
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"""
Using con As New OleDbConnection(conStr)
Using cmd As New OleDbCommand()
Using oda As New OleDbDataAdapter()
Dim dt As New DataTable()
' cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
cmd.CommandText = "select * from [Sheet1$]"
cmd.Connection = con
con.Open()
oda.SelectCommand = cmd
oda.Fill(dt)
con.Close()
'Populate DataGridView.
Loggridview.DataSource = dt
End Using
End Using
End Using
End Sub
答案 0 :(得分:0)
这是一个返回命中列表的函数。每个结果都是一个元组,其中Item1是rowId,Item2是columnId
Public Structure SearchHit
Public ReadOnly RowIndex As Integer
Public ReadOnly CellIndex As Integer
Public Sub New(rowIndex As Integer, cellIndex As Integer)
Me.RowIndex = rowIndex
Me.CellIndex = cellIndex
End Sub
End Structure
Public Function FindData(ByVal value As String, table As DataTable) As List(Of SearchHit)
Dim results As New List(Of SearchHit)
For rowId = 0 To table.Rows.Count - 1
Dim row = table.Rows(rowId)
For colId = 0 To table.Columns.Count - 1
Dim cellValue = row(colId)
If value.Equals(cellValue) Then
results.Add(New SearchHit(rowId, colId))
End If
Next
Next
Return results
End Function