访问数据库
dim cn as adodb.connection
dim rs as adodb.recordset
set cn = new adodb.connection
cn.ConnectionString = "连接串"
cn.open
set rs = new adodb.recordset
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open "查询语句", cn
输出到excel
Dim xlApplication As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Set xlApplication = New Excel.Application
Set xlWorkBook = xlApplication.Workbooks.Add
Set xlWorkSheet = xlWorkBook.Worksheets(1)
xlApplication.Visible = True
xlApplication.ActiveWindow.DisplayGridlines = False
dim currrow,currcol as long
currrow = 1 : currcol = 1
for i = 0 to rs.fields.count -1
xlworksheet.cells(currrow,currcol) = rs.fields(i).name
currcol = currcol +1
next
currrow = currrow +1 : currcol = 1
do while not rs.eof
for i = 0 to rs.fields.count -1
xlworksheet.cells(currrow,currcol) = rs.fields(i).value
currcol = currcol + 1
next
currrow = currrow +1 : currcol = 1
rs.movenext
loop
Set xlWorkSheet = Nothing
Set xlWorkBook = Nothing
Set xlApplication = Nothing