技術(shù) 點
- 技術(shù)
- 點
- V幣
- 點
- 積分
- 3705

|
本帖最后由 盜夢 于 2015-10-29 15:35 編輯
Access vba有各種方法可以導(dǎo)出到Excel,大致如下:
方法 | 優(yōu)點 | 缺點 | 查詢導(dǎo)出 | 可以根據(jù)查詢設(shè)計(直觀) | 格式固定 | ADO逐條遍歷 | 寫入位置可以靈活控制 | 速度較慢 | CopyFromRecordset | 速度極快 | 格式固定 | Excel插入QueryTable | 速度較快,可以匯總 | | 復(fù)制粘貼 | 標(biāo)題、格式和子窗體一致 | 只能導(dǎo)出數(shù)據(jù)表顯示的子窗體數(shù)據(jù) |
1、利用查詢導(dǎo)出
- DoCmd.OutputTo acOutputQuery, "具體的查詢名稱", acFormatXLS, , True
復(fù)制代碼
執(zhí)行這條語句,即可把對應(yīng)的查詢導(dǎo)出到Excel文件
拓展:
1)、當(dāng)然,你也可以根據(jù)SQL語句自動創(chuàng)建查詢,再導(dǎo)出。
CurrentDb.CreateQueryDef "新的查詢名稱", "SQL語句" '創(chuàng)建查詢
2)、然后,導(dǎo)出之后,你可以刪除掉這個查詢
DoCmd.DeleteObject acQuery, "查詢名稱" '刪除查詢
3)、當(dāng)然,你可以修改當(dāng)前查詢的SQL語句之后,再導(dǎo)出
Dim qdf As Object 'DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("查詢名稱")
qdf.SQL = strSQL '設(shè)置新的SQL語句
2、ADO逐條遍歷
這種方法是最傳統(tǒng)和最典型的方法,也是最靈活的。
打開一個記錄集,然后遍歷數(shù)據(jù)對Excel操作即可。重點在操作Excel。
- Dim rs As New ADODB.Recordset
- Dim xlApp As Object 'Excel.Application
- Dim xlBook As Object 'Excel.Workbook
- Dim xlSheet As Object 'Excel.Worksheet
- Set xlApp = CreateObject("Excel.Application")
- Set xlBook = xlApp.Workbooks.Add '添加一個新的Book
- Set xlSheet = xlApp.ActiveSheet '使用當(dāng)前的Sheet
- Dim strSql As String
- Dim i As Long
- strSql="Select * from 表1 where ID<10"
- rs.Open strSql, CurrentProject.Connection, 1, 1
- Do While Not rs.EOF
- xlSheet.Cells(2 + i,1)=rs("ID") '從第2行開始寫數(shù)據(jù)
- xlSheet.Cells(2 + i,2)=rs("FName")
- rs.MoveNext
- i=i+1
- Loop
- rs.Close
- xlApp.Visible=True
復(fù)制代碼
3、CopyFromRecordset導(dǎo)出數(shù)據(jù)
CopyFromRecordset是Excel vba的方法,可以快速把一個記錄集的數(shù)據(jù)填充到Excel單元格中。
- '標(biāo)題:根據(jù)SQL語句,快速導(dǎo)出到Excel文件
- '作者:阿航
- '創(chuàng)建日期:2015-01-10
- '說明:
- ' - 會將SQL語句的字段名作為標(biāo)題?梢杂肁s的方式設(shè)置對應(yīng)字段的標(biāo)題,如果是關(guān)鍵字,要加中括。
- ' - 示例:ExportToExcel "select FID as [ID], FText as 文本 from 表1"
- '更新日期:2015-09-05
- ' - 添加一個長度可變的參數(shù),用于傳遞標(biāo)題
- ' - 示例:ExportToExcel "select FID,FText from 表1","主鍵","文本"
- Public Function ExportToExcel(strSql As String, ParamArray VarExpr() As Variant) As Boolean
- Dim rs As Object 'DAO.Recordset(用ADO也行)
- Dim xlApp As Object 'Excel.Application
- Dim xlBook As Object 'Excel.Workbook
- Dim xlSheet As Object 'Excel.Worksheet
- Dim i As Integer
-
- '創(chuàng)建Excel文件
- On Error GoTo Err_Show
- Set xlApp = CreateObject("Excel.Application")
- Set xlBook = xlApp.Workbooks.Add '添加一個新的Book
- Set xlSheet = xlApp.ActiveSheet '使用當(dāng)前的Sheet
-
- Set rs = CurrentDb.OpenRecordset(strSql)
- '先寫入標(biāo)題(可以考慮用DAO的字段標(biāo)題屬性 rs(i-1).Properties("Caption"))
- ' For i = 1 To rs.Fields.Count
- ' xlSheet.cells(1, i) = rs(i - 1).Name
- ' Next
- '更新部分(2015-09-05)長度可變的參數(shù),相當(dāng)于一個數(shù)組
- For i = 0 To UBound(VarExpr)
- xlSheet.cells(1, i + 1) = VarExpr(i)
- Next
-
- '再寫入數(shù)據(jù)
- xlSheet.Range("A2").CopyFromRecordset rs
- rs.Close
-
- '調(diào)整列寬
- xlSheet.Columns.EntireColumn.AutoFit
- xlApp.Visible = True
- xlBook.Activate
- ExportToExcel = True
-
- Err_Exit:
- Set xlSheet = Nothing
- Set xlBook = Nothing
- Set xlApp = Nothing
- Set rs = Nothing
- Exit Function
- Err_Show:
- MsgBox "導(dǎo)出出錯,請重新嘗試" & vbCrLf & Err.Description, "導(dǎo)出出錯"
- On Error Resume Next
- '出錯則清掉文件,避免有多個Excel進程
- xlBook.Close False
- If xlApp.Workbooks.Count = 0 Then xlApp.Quit
- GoTo Err_Exit
- End Function
復(fù)制代碼
4、Excel插入QueryTable
QueryTable是Excel的一種表格對象,可以插入一個DAO記錄集
- '---用記錄填充Excel表格
- '輸入?yún)?shù): RS,需要填充的記錄集
- ' InsertSheet, 需要填充的Excel工作表
- ' InsertSheet, 需要開始填充的單元格
- '返回參數(shù), 填充完畢的range
- Public Function FillRS(ByRef rsInsert As DAO.Recordset, ByRef sheetInsert As Excel.Worksheet, rangeInsert As Excel.Range) As Excel.Range
- Dim qtTable As Excel.QueryTable
- Dim loListObject As Excel.ListObject
- '根據(jù)記錄集生成一個querytable
- rsInsert.MoveFirst
- Set qtTable = sheetInsert.QueryTables.Add(Connection:=rsInsert, Destination:=rangeInsert)
- With qtTable
- .FieldNames = True
- .AdjustColumnWidth = True
- .Refresh BackgroundQuery:=False
- End With
- ' 把QueryTable ListObject
- Set loListObject = sheetInsert.ListObjects.Add(xlSrcRange, qtTable.ResultRange, , xlYes)
- With loListObject
- .ShowTotals = True '顯示匯總列
- .ShowAutoFilter = True
- '顯示匯總數(shù)據(jù)
- Dim fld As DAO.Field
- For Each fld In rsInsert.Fields
- Select Case fld.Type
- Case dbCurrency
- '.ListColumns(fld.Name).TotalsCalculation = xlTotalsCalculationSum
- .ListColumns(fld.Name).Range.NumberFormat = "#,##0.00;-#,##0.00"
- Case dbDate
- .ListColumns(fld.Name).Range.NumberFormat = "yyyy-mm-dd;@"
- End Select
- Next
- '.TableStyle = "TableStyleMedium9"
- '.Range.AutoFormat xlRangeAutoFormatList1
- Set FillRS = .Range
- .Unlink
- .Unlist
- End With
- Set qtTable = Nothing
- End Function
復(fù)制代碼
5、復(fù)制粘貼的方法,快速導(dǎo)出數(shù)據(jù)
在某次發(fā)現(xiàn)了,可以手動復(fù)制子窗體上的數(shù)據(jù),然后粘貼到Excel中。于是就嘗試用這代碼實現(xiàn)這個功能
- Me.子窗體控件名.SetFocus '子窗體控件獲得焦點
- DoCmd.RunCommand acCmdSelectAllRecords '選中所有記錄
- DoCmd.RunCommand acCmdCopy '復(fù)制
- DoEvents
- Dim Obj As Object
- Set Obj = CreateObject("excel.application") '創(chuàng)建Excel對象
- Obj.workbooks.Add '新建工作簿
- Obj.Visible = True '設(shè)為可見
- SendKeys "^v", True '粘貼數(shù)據(jù)
復(fù)制代碼
當(dāng)然,還有其他各種方法,例如利用OpenXML方法導(dǎo)出。大家可以回復(fù)討論交流一下。
|
評分
-
參與人數(shù) 1 | 經(jīng)驗 +10 |
金錢 +10 |
技術(shù) +1 |
V幣 +1 |
收起
理由
|
5988143
| + 10 |
+ 10 |
+ 1 |
+ 1 |
(其它)優(yōu)秀教程、原創(chuàng)內(nèi)容、以資鼓勵、其. |
查看全部評分
|