数据库中查到的记录输出到execl
上一篇 /
下一篇 2009-03-27 17:44:29
/ 个人分类:QTP脚本
'*********************************************************
'创建记录集对象,执行查询语句
'*********************************************************
Set res = CreateObject("ADODB.RecordSet") '创建一个记录集对象
Set resl= CreateObject("ADODB.RecordSet") '创建一个记录集对象
strSql="select * from zhang_students" '设置查询语句
strSqll="select count(*) from zhang_students"
res.open strSql,conn '执行查询语句
resl.open strSqll,conn
'*********************************************************
'创建excel表格
'*********************************************************
Dim excelApp,excelSheet,fso
Set excelApp = CreateObject("Excel.Application") '创建Excel Application对象
Set excelSheet = CreateObject("Excel.sheet") '创建Excel Sheet对象
Set fso = CreateObject("scripting.FileSystemObject") '创建FileSystemObject对象
filename = "E:\QTP\text.xls" '设定文件路径
excelApp.Visible = true 'excel可见
excelApp.WorkBooks.Add
'*********************************************************
'查询结果输出到excel中
'*********************************************************
numbe=CLng(resl("count(*)"))
For i=1 to numbe
'Do While Not res.EOF
'MsgBox Trim(res.Fields("Test Results"))
'Loop
'msgbox res("name")
excelApp.Cells(i,1).Value =res("name")
excelApp.Cells(i,2).Value =res("sex")
excelApp.Cells(i,3).Value =res("address")
excelApp.Cells(i,4).Value =res("class")
excelApp.Cells(i,5).Value =res("score")
res.MoveNext
Next
excelApp.ActiveWorkBook.Saved
excelApp.Quit
'*********************************************************
'关闭连接
'*********************************************************
res.close
Set res = nothing
conn.close
Set conn = nothing
End If
收藏
举报
TAG: