设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1826|回复: 3
打印 上一主题 下一主题

[查询] ACCESS数据导出到指定的工作表中

[复制链接]
1#
发表于 2016-11-10 15:23:24 | 显示全部楼层
可以的吧
你把你的例子传上来
2#
发表于 2016-11-10 15:49:44 | 显示全部楼层
  1. '---------------------------------------------------------------------------------------
  2. ' Procedure : QueryToExcel
  3. ' DateTime  : 2008-12-2 00:02
  4. ' Author    : Henry D. Sy
  5. ' Purpose   : QueryToExcel "查询名称", "工作簿名称", "工作表名称"
  6. '             xls文件要与Access文件放在同一个文件夹
  7. '---------------------------------------------------------------------------------------
  8. '
  9. Sub QueryToExcel(ByVal strQueryName As String, ByVal XlName As String, ByVal _
  10.                  XlShtName As String)
  11. ' Send the Query results to Excel
  12. ' for further analysis

  13.     Dim rs As New ADODB.Recordset
  14.     Dim XlApp As New Excel.Application
  15.     Dim XlWb As Excel.Workbook
  16.     Dim fld As ADODB.Field
  17.     Dim intCol As Integer
  18.     Dim intRow As Integer

  19.     ' Get the desired data into a recordset

  20.     On Error GoTo QueryToExcel_Error

  21.     rs.Open strQueryName, CurrentProject.Connection


  22.     ' Open a  worksheet
  23.     Set XlWb = XlApp.Workbooks.Open(CurrentProject.Path & "" & XlName & _
  24.                                     ".xls")
  25.     XlWb.Worksheets(XlShtName).Activate

  26.     ' Copy the data
  27.     ' First the field names
  28.     For intCol = 0 To rs.Fields.Count - 1
  29.         Set fld = rs.Fields(intCol)
  30.         XlWb.Worksheets(XlShtName).Cells(1, intCol + 1) = fld.Name
  31.     Next intCol
  32.     ' Now the actual data
  33.     intRow = 2
  34.     Do Until rs.EOF
  35.         For intCol = 0 To rs.Fields.Count - 1
  36.             XlWb.Worksheets(XlShtName).Cells(intRow, intCol + 1) = _
  37.             rs.Fields(intCol).Value
  38.         Next intCol
  39.         rs.MoveNext
  40.         intRow = intRow + 1
  41.     Loop

  42.     ' Make the worksheet visible
  43.     XlApp.Visible = True
  44.     rs.Close
  45.     Set rs = Nothing

  46.     On Error GoTo 0
  47.     Exit Sub

  48. QueryToExcel_Error:

  49.     MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
  50. End Sub

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-14 20:51 , Processed in 0.112343 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表