Office中国论坛/Access中国论坛

标题: ADO记录集如何导出到EXCEL [打印本页]

作者: 你是坏蛋    时间: 2012-12-28 22:20
标题: ADO记录集如何导出到EXCEL
如题。
已有一个ADO记录集RS,如何新建一个EXCEL文件,并将它导出?

作者: roych    时间: 2012-12-28 22:33
用CreateObject来创建一个Excel组件(如有引用Excel库的话,则更方便些),然后将记录逐行写入。由于条件所限,暂不做实例。大体如下(【由于条件所限,暂未验证程序是否正确】):
Dim RowData As Variant
Dim ColData As Variant
Dim RowTmp As Variant
Dim ColTmp As Variant
Dim R As Long
Dim C As Long

On Error GoTo Eh
Set MyXls = CreateObject("Excel.Application")
MyXls.Visible = True
MyXls.Workbooks.Add
R = 0
For RowTmp = 0 To rs.RecordCount-1
     C = 64
     R = R + 1
   For ColTmp = 0 To rs.Fields.Count-1
         C = C + 1
     MyXlsp.Range(Chr(C) & R).Select
     MyXls.ActiveCell.Value = rs.fields(coltmp)
   Next
作者: 你是坏蛋    时间: 2012-12-29 08:10
谢谢roych
问题解决了。
作者: 你是坏蛋    时间: 2012-12-29 08:16
我调试成功的代码如下:
  Dim rs As New ADODB.Recordset
  rs.CursorLocation = adUseClient
  rs.Open "select * from 订单明细", CurrentProject.Connection, adOpenKeyset, adLockOptimistic

Dim RowData As Variant
Dim ColData As Variant
Dim RowTmp As Variant
Dim ColTmp As Variant
Dim R As Long
Dim C As Long
Dim aa As Excel.Workbook
Set MyXls = CreateObject("Excel.Application")
MyXls.Visible = True
Set aa = MyXls.Workbooks.Add
aa.Activate
aa.Sheets("sheet1").Cells(1, 1).CopyFromRecordset rs
      rs.Close
  Set rs = Nothing
作者: zhuyiwen    时间: 2012-12-29 11:23
aa.Sheets("sheet1").Cells(1, 1).CopyFromRecordset rs


这样更快捷!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3