感受:由于每次测试都需要执行类似操作,不得已写了这段操作。虽然内容比较简单,但中间磕磕碰碰还是有的,完成之后还是有所收获的。其实在任何编译器里都能执
行,为什么选择TC呢?因为在研究这货,捣鼓一个月发现自动化功能难以发挥其威力,因此决定当半自动化工具用,第一个想到的就是连接数据库,今后就决定用这货分析数据了,顺便学习下VBS。
代码作用:根据查询语句,在excel中得到需要数据,现数据还需要手动分析,正研究如何自动分析并得到结果。
Sub Main
Dim driver,Path,i,jDim connection,cnstr
Dim qry,recordset
Dim xlbook,objExcel,xlsheet
Dim irowcount,icolcount
Dim AreaId,ProjectId
'输入地区号(业务)
'连接数据库
set connection=Sys.OleObject("ADODB.Connection")
connection.ConnectionString= "Provider=SQLOLEDB;"&_
"Server=.\BILL2006;Database=ScoreData2010;Uid=sa; Pwd=BILL2006;"
call connection.open(cnstr)
if err Then
MsgBox("连接SQL出错!")
Wscript.quit
end if
'SQL查询语句(业务原因查询了两次,现仍不知道有没其他方法可以在不关闭的情况下再执行一次查询)
set recordset=Sys.OleObject("ADODB.Recordset")
qry="select * from td_project where proj_area="&AreaId
call recordset.Open(qry,connection,1,1)
recordset.MoveFirst
ProjectId=recordset.Fields(0).value
recordset.Close
set recordset=Sys.OleObject("ADODB.Recordset")
qry="select biin_Idx,biin_Name,biin_Total from dbo.td_BidInfo where biin_ProjectID='"&ProjectId&"' order by biin_Idx"
call recordset.Open(qry,connection,1,1)
'打开excel
path="d:\test.xls"
Set objExcel = CreateObject("Excel.Application")
objexcel.Visible = False
set xlbook = objExcel.workbooks.open(path)
Set xlsheet = xlbook.Worksheets(1)
'导出数据
if recordset.RecordCount<1 Then
Log.Message("没有数据!")
End If
irowcount=recordset.RecordCount
icolcount=recordset.Fields.Count
xlsheet.Activate
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(irowcount+1,icolcount)).Font.Size=10
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(irowcount+1,icolcount)).Font.name="黑体"
for i=0 to irowcount-1
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(irowcount+1,icolcount)).Rows(i+1).RowHeight=15
Next
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,icolcount)).Font.Bold=true
for i=0 to icolcount-1
xlsheet.cells(1,i+1)=recordset(i).name
Next
recordset.MoveFirst
while not recordset.EOF
for i=0 to irowcount-1
for j=0 to recordset.Fields.Count-1
if IsNull(recordset.Fields(j).value) Then
xlsheet.Cells(i+1,j+1)=""
Else
xlsheet.Cells(i+2,j+1)=CStr(recordset.Fields(j).value)
end If
next
recordset.MoveNext
next
Wend
'保存excel 关闭excel、数据库
xlbook.Save
xlbook.Close
Set xlbook = nothing
objExcel.quit
set objexcel = Nothing
recordset.Close
set recordset=Nothing
connection.Close
set connection=Nothing
Log.Message("导出成功!")
Log.Message("ProjectId:"&ProjectId)
Log.Message("")
End Sub