|
哦对,这样比较好:
Private Sub OutputExcel_Click()
On Error GoTo Err_OutputExcel_Click
Dim xlApp As Excel.Application
Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim Myrecordset As ADODB.Recordset
Dim strSQL As String
Dim c As Integer
Dim i As Integer
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Sheets(1)
xlApp.DisplayAlerts = False
Set Myrecordset = New ADODB.Recordset
strSQL = "SELECT * from 表1"
Myrecordset.Open strSQL, CurrentProject.Connection, adOpenStatic
xlSheet.Range("B7").CopyFromRecordset Myrecordset
c = 2
For i = 0 To Myrecordset.Fields.Count - 1
xlApp.ActiveSheet.Cells(6, c).Value = Myrecordset.Fields(i).Name
c = c + 1
Next i
xlBook.SaveAs "c:\TEST1.xlsx"
MsgBox "完成导出"
Exit_OutputExcel_Click:
On Error Resume Next
Set xlSheet = Nothing
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
Myrecordset.Close
Set Myrecordset = Nothing
Exit Sub
Err_OutputExcel_Click:
MsgBox Err.Description
Resume Exit_OutputExcel_Click
End Sub |
|