Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.TextBox1.Text.ToString.Trim() & ";Extended Properties=Excel 5.0"
'定义连接字符串
Dim conn As OleDbConnection
'定义连接命令
conn = New OleDbConnection(str)
Dim adapter As New OleDbDataAdapter("select * from [sheet1$] ", conn)
'显示命令
Dim ds As New DataSet
'定义记录集
conn.Open()
'打开连接
adapter.Fill(ds, "[sheet1$]")
'填充ds
Me.DataGridView1.DataSource = ds
'连接数据源
Me.DataGridView1.DataMember = "[sheet1$]"
Me.TextBox1.Text = ""
Dim i, j '定义行和列
For i = 0 To Me.DataGridView1.Rows.Count - 1 Step 1
For j = 0 To Me.DataGridView1.Columns.Count - 1 Step 1
If (j + 1) Mod Me.DataGridView1.Columns.Count = 0 Then '换行条件
Me.TextBox1.Text = Me.TextBox1.Text & DataGridView1.Rows(i).Cells(j).Value.ToString & " " & Chr(13) + Chr(10)
Else
Me.TextBox1.Text = Me.TextBox1.Text & DataGridView1.Rows(i).Cells(j).Value.ToString & " "
'显示 excel 表里的所有内容
End If
Next
Next
'显示表中的sheet1页
conn.Close()
Catch ex As Exception '排除异常
MsgBox(ex.Message)
End Try
End Sub