5,139
社区成员
发帖
与我相关
我的任务
分享
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim StartRange As Excel.Range
Dim f As Variant
……
With rst
.Open "Employees", strConn, _
adOpenKeyset, adLockOptimistic
End With
' declare a module-level object
' variable myExcel as Excel.Application
' at the top of the module
Set myExcel = New Excel.Application
' create a new Excel workbook
Set wbk = myExcel.Workbooks.Add
' set the reference to the ActiveSheet
Set wks = wbk.ActiveSheet
' make the Excel application window visible
myExcel.Visible = True
i = 1
' Create the column headings in cells
With rst
For Each f In .Fields
With wks
.Cells(1, i).Value = f.Name
i = i + 1
End With
Next
End With
' specify the cell range that will receive the data (A2)
Set StartRange = wks.Cells(2, 1)
' copy the records from the recordset
' and place in cell A2
StartRange.CopyFromrecordset rst
rst.Close
Set rst = Nothing
' autofit the columns to make the data fit
' wks.Columns("A:Z").AutoFit
wks.Columns.AutoFit
' close the workbook and save the file
wbk.Close SaveChanges:=True, _
FileName:="C:\ExcelDump.xls"
' quit the Excel application
myExcel.Quit
……