以下是我用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
  

解决方案 »

  1.   

    如果不想弹出选择对话框
    把with快里的代码去掉
    直接指定 sFile 的路径即可
      

  2.   

    Dim oConn As New ADODB.Connection
    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