代码如下
Public conn As ADODB.Connection
Public rs As ADODB.Recordset
==============================
Private Sub Command1_Click()
Dim mypath, mystr As String
Dim mypos As Integer
Dim SQL As String
With cd1
.FilterIndex = 3
.ShowSave
On Err GoTo err5
mystr = StrReverse(.FileName)
'Debug.Print mystr
mypos = InStr(mystr, "\")
'Debug.Print mypos
mypath = StrReverse(Mid(mystr, mypos))
'Debug.Print mypath
mystr = StrReverse(Left(mystr, mypos - 1))
'Debug.Print mystr
SQL = "select * into [excel 8.0;database=" & .FileName & "].sheet1 from class_info"
.DefaultExt = "*.XLS"
End With
If rs.State = 1 Then
   rs.Close
End Iferr5:
If Err.Number = 5 Then
   MsgBox ("没有选择文件")
End If
On Error Resume Next
End Sub
===========================
Private Sub Command2_Click()
Unload Me
End Sub
===========================
Private Sub Form_Load()Dim SQL As String
Dim connstring As String
connstring = "provider=sqloledb.1;persist security info=false;user id=sa;password=;Initial Catalog=student;Data Source=ybyb"
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient '加客户端游标
conn.Open connstring
Set rs = New ADODB.Recordset
rs.Open "select * from class_info", conn, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
With cd1
.Filter = "FoxBase/FoxPro (*.DBF)|*.DBF|Access 8.0(*.MDB)|*.MDB|Excel 8.0(*.XLS)|*.XLS|Paradox 4.x(*.DB)|*.DB"
.DialogTitle = "导出文件为"
.CancelError = False
End WithEnd Sub
为什么在通用对话框选择文件后,文件里没数据啊
谁有报盘的例子施舍一个吧@^@!

解决方案 »

  1.   

    to alt982001,俺就是想把数据库表里的查询结果导出到一个execl表里去,可是俺那么做了,excel却没结果为什么?
    cd1,是个通用对话框,剩下没用什么
      

  2.   

    你导出数据的语句应该为
    SQL = "select * into [excel 8.0;database=" & .FileName & "].sheet1 from class_info"
    Conn.Execute SQL
      

  3.   

    to zlt982000,旧问题没了,新问题出现了,为什么选择完文件后,弹出错误提示说文件不存在或者没有权限?
      

  4.   

    哈哈,如果加了conn.Execute SQL呢,就在这出错,如果没加呢,就没有错误提示,但是没数据,错误提示是没有文件或没有权限。不知道怎么回事,晕