Public Sub RsToXls(RsSrc As ADODB.Recordset)
Dim MyXlsApp As New Excel.Application
Dim MyXlsWbk As New Excel.Workbook
Dim MyXlsSht As New Excel.Worksheet
Dim i, j, k, m, n As Integer
Set MyXlsApp = CreateObject("Excel.Application")
Set MyXlsWbk = MyXlsApp.Workbooks.Add
Set MyXlsSht = MyXlsWbk.Worksheets(1)
MyXlsApp.Visible = True
With RsSrc
If RsSrc Is Nothing Then
MsgBox "没有数据,无法导出", vbExclamation
Exit Sub
ElseIf .RecordCount = 0 Then
MsgBox "没有数据,无法导出", vbExclamation
Exit Sub
End If
.MoveFirst
j = 1
'MyXlsSht.Cells(1, 1).Value = "序号"
For m = 0 To .Fields.Count - 1
MyXlsSht.Cells(1, m + 1).Value = .Fields(m).Name
Next
Do While Not .EOF
j = j + 1
MyXlsSht.Cells(j, 1) = j - 1
For i = 0 To RsSrc.Fields.Count - 1
Select Case RsSrc.Fields(i).Type
Case 7 '如果是日期类型
MyXlsSht.Cells(j, i + 1).NumberFormatLocal = "yyyy-m-d"
End Select
MyXlsSht.Cells(j, i + 1) = .Fields(i)
Next
.MoveNext
Loop
End With
'MyXlsApp.Visible = True
Set MyXlsApp = Nothing
Set MyXlsWbk = Nothing
Set MyXlsSht = Nothing