下面的代码打开一个EXCEL文档,并读取Sheet1表的内容,仅供参考。
Dim MyDS As DataSet
Dim MyCmd As OleDbDataAdapter
Dim MyCon As OleDbConnection
Dim OFD1 As New OpenFileDialog
OFD1.Filter = "Excel Files|*.Xls"
OFD1.Title = "连接 Excel 数据"
If OFD1.ShowDialog() = DialogResult.OK Then
If OFD1.FileName <> "" Then
MyCon = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" & OFD1.FileName & ";Extended Properties=Excel 8.0;")
MyCmd = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyCon)
MyDS = New System.Data.DataSet
MyCmd.Fill(MyDS, "EXL")
Me.DataGridView1.DataSource = MyDS.Tables("EXL")
End If
End If