回复 楼主 lin7516
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OPEN As New OpenFileDialog : Dim filea As String
Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.Rows.Clear()
Me.DataGridView1.Columns.Clear()
'OPEN.InitialDirectory =
OPEN.Filter = "excel files (*.xls)|*.xls"
OPEN.FilterIndex = 1
OPEN.RestoreDirectory = True
If OPEN.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fileName As String
fileName = OPEN.FileName
'建立EXCEL连接,读入数据
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
Dim myDataset As New DataSet
If ComboBox2.SelectedIndex > 0 Then
filea = Trim(ComboBox2.Text)
Else
filea = Mid(fileName, InStrRev(fileName, "\", -1, 1) + 1, Len(fileName))
Dim aa As Integer = InStr(1, filea, ".")
filea = Mid(filea, 1, aa - 1)
End If
Dim fileB As String = "SELECT * FROM [" & filea & "$]"
Dim da As New OleDb.OleDbDataAdapter(fileB, strConn)
Try
da.Fill(myDataset)
Me.DataGridView1.DataSource = myDataset.Tables(0)
Me.Button2.Enabled = True
Catch ex As Exception
MsgBox(ex.Message.ToString)
Me.Button2.Enabled = False
End Try
End If
End Sub
图片附件: 游客没有浏览图片的权限,请
登录 或
注册