VB.net 2008打开EXCEL导入DataGridView显示

来源:百度知道 编辑:UC知道 时间:2024/05/06 00:33:09
要这样一个程序源码,界面是一个DataGridView控件,2个按钮,一个是用来打开EXCEL文件所在的文件夹,另一个按钮是在打开EXCEL文件夹之后,点击就能把EXCEL文件显示在DataGridView控件上了,求源码,复制的就不要了,谢谢....

Dim fileName As String
Me.OpenFileDialog1.Filter = “Excle文件(*.xls)|*.xls”
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = Me.OpenFileDialog1.FileName
‘Use below code to retrieve data from an Excel file.
‘Code to open the file here.
Dim strConn As String = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=’” & fileName & “‘;Extended Properties=Excel 8.0;”

‘Now using the OledbDataAdapter you can query the excel sheet.
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter(”SELECT * FROM [Sheet1$]“, strConn)
‘da.TableMappings.Add(”Table”, “ExcelTest”)
Try
da.Fill(myDataset)
Me.DataGrid1.DataSource = myDataset.Tables(0)

Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If