|
Public Sub ExportToExcel()
'要引用microsoft excel x.x object library
'要引用microsoft activeX data object x.x library
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim Ex As Excel.Application '要引用microsoft excel x.x object library
Dim Wk As Excel.Workbook
Dim Sht As Excel.Worksheet
Dim sql As String
sql = "select * from orders"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open CurrentProject.Connection.ConnectionString
rs.Open sql, cn, adOpenKeyset, adLockOptimistic
Set Ex = CreateObject("EXCEL.application")
With Ex
.Visible = False
Set Wk = .Workbooks.Add
Set Sht = .Worksheets(1)
Sht.Name = "我的命名"
Sht.Range("a1").CopyFromRecordset rs
Sht.SaveAs "c:\我的文件.xls"
.Quit
End With
Set rs = Nothing
Set cn = Nothing
End Sub
|
|