以下是我用MSHFlexGrid把excel里面的数据显示出来,能不能直接设定路径不用CommonDialog控件弹出一个窗口来选择我的excel文件呢?帮帮忙吧,看怎么修改?我自己尝试了一下没有成功,还请高手出手Dim oConn As New ADODB.Connection
Dim oRS As New ADODB.Recordset
Dim sFile As StringWith CommonDialog1
.DialogTitle = "打开"
.CancelError = False
'ToDo: 设置 common dialog 控件的标志和属性
.Filter = "所有文件 (*.*)|*.xls"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
End With
'设置导入的数据
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & sFile & "';Extended Properties=""Excel 8.0;"""
oRS.Open "Select * from [Sheet1$]", oConn, adOpenKeyset, adLockReadOnly, adCmdText '打开Sheet1,也可以是定义的名称,或一个Range。
Set MSHFlexGrid1.DataSource = oRS oRS.Close
oConn.Close
Set oRS = Nothing
Dim oRS As New ADODB.Recordset
Dim sFile As StringWith CommonDialog1
.DialogTitle = "打开"
.CancelError = False
'ToDo: 设置 common dialog 控件的标志和属性
.Filter = "所有文件 (*.*)|*.xls"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
End With
'设置导入的数据
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & sFile & "';Extended Properties=""Excel 8.0;"""
oRS.Open "Select * from [Sheet1$]", oConn, adOpenKeyset, adLockReadOnly, adCmdText '打开Sheet1,也可以是定义的名称,或一个Range。
Set MSHFlexGrid1.DataSource = oRS oRS.Close
oConn.Close
Set oRS = Nothing
解决方案 »
- listview显示数据时出现380无效的属性值
- 根据权限设置按纽的enabled属性 为什么第二次单击时就失效了
- 判断当前用户是否是超级用户
- 免费发短信,不能发的请班主扣分http://www.m2p.com.cn/download.asp?no=m2px011m02030f
- 用VB如何把一个程序关联到.txt文件呢?
- 关于DCOM的问题————很急!!!!!!
- 一个关于SHELL函数的问题。
- 拜托看一下,这段代码怎么老是出问题?!是关于数据库查询的。(二)
- 菜问题,急急!!用picturebox将文字转换成BMP急急!!急急!!
- 如何用VB打开ACCESS数据库中的图片?
- 怎样激活指定窗体。
- 下载了个RESIZE的类不知道在窗体上怎么调用?
把with快里的代码去掉
直接指定 sFile 的路径即可
Dim oRS As New ADODB.Recordset
Dim sFile As StringsFile="c:\myfile.xls"
'设置导入的数据
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & sFile & "';Extended Properties=""Excel 8.0;"""
oRS.Open "Select * from [Sheet1$]", oConn, adOpenKeyset, adLockReadOnly, adCmdText '打开Sheet1,也可以是定义的名称,或一个Range。
Set MSHFlexGrid1.DataSource = oRS oRS.Close
oConn.Close
Set oRS = Nothing