前几天帮本论坛的会员写了一个软件,因报表要求太高只能把数据输出到EXCEL 中再打印报表。特写了如下代码,现贴出来供需要把ACCESS 中的数据输出到EXCEL 的会员参考。 Private Sub Command0_Click()
Dim rs As ADODB.Recordset
Dim I As Integer
Dim xlApp As Excel.Application
Dim xlBook As Workbook, xlSheet As Worksheet Set rs = New ADODB.Recordset
Set xlApp = CreateObject("Excel.Application")
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(CurrentProject.Path & "\acc.XLS") '打开要输出的EXECL文件
Set xlSheet = xlBook.Worksheets(1) '打开工作表,这里是第一个,可以设其他
xlApp.Visible = True '显示 rs.Open "TBL_现金日报表", CurrentProject.Connection, 3, 3 If Not rs.EOF Then rs.MoveFirst Do While Not rs.EOF
I = I + 1
For x = 0 To 2 xlSheet.Cells(I, x + 1) = rs(x) 'CELLS(行,列) 单元格
Next x
rs.MoveNext
Loop
rs.Close
'xlSheet.PrintPreview '如果是要打印,只要把 PrintPreview 改为 PrintOut
'xlBook.Close SaveChanges:=False
'xlApp.Quit '退出 End Sub |