导出至Excel系列方法六QueryTables
时 间:2019-10-03 11:05:25
作 者:金宇 ID:43 城市:江阴
摘 要:此方法是借用Excel中QueryTables对象的Add方法实现将数据导出至Excel。
正 文:
关于此对象的add方法的使用我写了一个专门的函数,可以直接调用便于数据导出至Excel,调用方法如下:
ExportToExcelQueryTables "Products", "select [Supplier IDs],ID,[Product Code],[Product Name],[Description] from Products"
"Products" 就指工作薄的名称。
"select [Supplier IDs],ID,[Product Code],[Product Name],[Description] from Products" 是需要导出数据的SQL语句。
函数如下:
'=============================================================================== '函数名称: ExportToExcelQueryTables '功能描述: 将SQL语句创建的记录集对象中的内容复制到Excel工作表 '输入参数: WorkbookName 必需的,工作簿名称 ' strSQL 必需的,不能包含具有 OLE 对象的字段,否则该方法无效。 '返回参数: 无 '使用说明: ExportToExcelQueryTables("公司资料","select * from 表名称或者查询名称") '作 者: 金宇 '创建日期: 2013-11-1 '=============================================================================== Function ExportToExcelQueryTables(ByVal WorkbookName As String, ByVal strSQL As String) On Error GoTo Err_ExportToExcel Dim objExcel As Object Dim objBook As Object Dim objSheet As Object Dim objRange As Object Dim objExcelQuery As Object Dim rst As Object Dim cnn As Object Dim strFileName As String Dim strExtName As String Dim lngRow As Long Dim lngColumn As Long Dim FirstRange As String If Dir(WorkbookName) <> "" Then Kill WorkbookName '根据当前版本取得对应的文件扩展名 strExtName = ".xls" If Val(Application.Version) > 11 Then strExtName = ".xlsx" '取得另存为文件名 With Application.FileDialog(2) 'msoFileDialogSaveAs .InitialFileName = WorkbookName & strExtName If Not .Show Then Exit Function strFileName = .SelectedItems(1) If Not strFileName Like "*" & strExtName Then strFileName = strFileName & strExtName End If If Len(Dir(strFileName)) > 0 Then Kill strFileName End With DoCmd.Hourglass True 'ADO方式 Set rst = CreateObject("adodb.recordset") Set cnn = CurrentProject.Connection 'CursorLocation = 3 这段代码必须加否则会出错, '如果不加会在objSheet.QueryTables.Add那里会出现 "无效的过程调用或参数" rst.CursorLocation = 3 rst.Open strSQL, cnn, 1, 1 Set objExcel = CreateObject("Excel.Application") objExcel.Visible = False Set objBook = objExcel.Workbooks.Add Set objSheet = objBook.Worksheets.Add 'Set objSheet = objBook.Worksheets("Sheet1") objSheet.Name = WorkbookName '工作表名称 objSheet.Select Set objExcelQuery = objSheet.QueryTables.Add(rst, objSheet.Range("A1")) With objExcelQuery .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .Refresh BackgroundQuery:=False End With objExcel.Visible = True objBook.SaveAs strFileName Exit_ExportToExcel: Set rst = Nothing Set objSheet = Nothing Set objBook = Nothing Set objExcel = Nothing DoCmd.Hourglass False Exit Function Err_ExportToExcel: MsgBox Err.Description, vbCritical, "错误提示" Resume Exit_ExportToExcel End Function
测试示例下载:
Access软件网官方交流QQ群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 统计当月之前(不含当月)的记录...(03.11)
- 【Access Inputbo...(03.03)
- 按回车键后光标移动到下一条记录...(02.12)
- 【Access Dsum示例】...(02.07)
- Access对子窗体的数据进行...(02.05)
- 【Access高效办公】上月累...(01.09)
- 【Access高效办公】上月累...(01.06)
- 【Access Inputbo...(12.23)
- 【Access Dsum示例】...(12.16)

学习心得
最新文章
- Access控件美化之--美化按钮...(04.19)
- Access多行文本按指定字符筛选...(04.18)
- Microsoft Access数...(04.18)
- 仓库管理实战课程(12)-月度结存...(04.16)
- 仓库管理实战课程(11)-人性化操...(04.15)
- 32位的Access软件转化为64...(04.12)
- 【Access高效办公】如何让vb...(04.11)
- 仓库管理实战课程(10)-入库功能...(04.08)
- Access快速开发平台--Fun...(04.07)
- 仓库管理实战课程(9)-开发往来单...(04.02)