|
- '---------------------------------------------------------------------------------------
- ' Procedure : QueryToExcel
- ' DateTime : 2008-12-2 00:02
- ' Author : Henry D. Sy
- ' Purpose : strQueryName 查询名(表名)
- ' xlsName 工作簿名
- ' strShtName 工作表名
- ' 需要引用Microsoft Excel 11.0 Object Library
- '---------------------------------------------------------------------------------------
- '
- Sub QueryToExcel(ByVal strQueryName As String, ByVal xlsName As String, ByVal _
- strShtName As String)
- ' Send the Query results to Excel
- ' for further analysis
- Dim rs As ADODB.Recordset
- Dim objXL As Excel.Application
- Dim objWs As Excel.Workbook
- Dim fld As ADODB.Field
- Dim intCol As Integer
- Dim intRow As Integer
- Set rs = New ADODB.Recordset
- ' Get the desired data into a recordset
- rs.Open strQueryName, CurrentProject.Connection
- ' Launch Excel
- Set objXL = New Excel.Application
- ' Open a worksheet
- Set objWs = objXL.Workbooks.Open(CurrentProject.Path & "" & xlsName & _
- ".xls")
- objWs.Worksheets(strShtName).Activate
- ' Copy the data
- ' First the field names
- For intCol = 0 To rs.Fields.Count - 1
- Set fld = rs.Fields(intCol)
- objWs.Worksheets(strShtName).Cells(1, intCol + 1) = fld.Name
- Next intCol
- ' Now the actual data
- intRow = 2
- Do Until rs.EOF
- For intCol = 0 To rs.Fields.Count - 1
- objWs.Worksheets(strShtName).Cells(intRow, intCol + 1) = _
- rs.Fields(intCol).Value
- Next intCol
- rs.MoveNext
- intRow = intRow + 1
- Loop
- ' Make the worksheet visible
- objXL.Visible = True
- rs.Close
- Set rs = Nothing
- End Sub
复制代码- Private Sub Command0_Click()
- QueryToExcel "要导出的查询(表)名", "工作簿名", "工作表名"
- End Sub
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|