两个excel表对比的vbs脚本(原创)

上一篇 / 下一篇  2011-09-22 12:24:02 / 个人分类:QTP的学习研究

最近测试一个软件是开机提速软件,工作中需要对比两次电脑启动的服务状态是否相同,服务数量是否相同,为了减少花费时间,就自己写了一段vbs脚本解决这个问题,具体脚本如下:

filepath=getProjectPath()
filepath1=filepath&”\list1.xls” ‘这里输入EXCEL的实际路径
filepath2=filepath&”\list2.xls” ‘这里输入EXCEL的实际路径
sqlrowcount=”Select count(name) As RowCont from [Sheet1$]”
sql=”Select name,status from [Sheet1$]”
If FilePathFind(Filepath1)And FilePathFind(Filepath2)Then
Set rowcount1=excel_Record(filepath1,sqlrowcount)
Set rowcount2=excel_Record(filepath2,sqlrowcount)
If rowcount1(“RowCont”)>=rowcount2(“RowCont”)Then
excel_compare filepath1,filepath2
Else
excel_compare filepath2,filepath1
End If
Set rowcount1=nothing
Set rowcount2=Nothing
Else
MsgBox “请检查文件是否存在!!”
End if
msgbox”比较完成,请到 “&filepath&”\result.txt”&”下查看结果!”
‘*************************ADO打开EXCEL并返回记录集********************************start

Public Function excel_Record(filepath,sql)
Set conn= createobject(“ADODB.Connection”) ‘创建ADO对象
conn.Open “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=”&filepath&”;Extended Properties=Excel 8.0″
Set rs=CreateObject(“ADODB.Recordset”) ‘创建记录集
rs.Open sql,conn ‘打开记录集
Set excel_Record=rs’将记录集作为函数返回值
Set rs=nothing
End Function

‘********************************************************************************end

‘*************************获取项目所在文件夹函数********************************start

Public Function getProjectPath()
Set bjFSO = CreateObject(“Scripting.FileSystemObject”) ‘创建SFO对象
Set bjFile = objFSO.GetFile(wscript.scriptfullname) ‘获取文件的完整路径
getProjectpath= objFSO.GetParentFolderName(objFile) ‘获取上一级文件夹路径
Set bjFSO = Nothing
Set bjFile = Nothing
End Function

‘********************************************************************************end

‘*************************判断当前文件是否存在*********************************start
Public Function FilePathFind(Filepath)
Set bjFSO = CreateObject(“Scripting.FileSystemObject”)
if objFSO.FileExists(Filepath) then
FilePathFind=1
else
FilePathFind=0
end if
Set bjFSO=nothing
End Function
‘********************************************************************************end
‘*************************比较2excel数据*********************************start
Public Function excel_compare(filepath1,filepath2)
a1=split(filepath1,”/”)
a2=split(filepath2,”/”)
filename1=a1(ubound(a1))
filename2=a2(ubound(a2))
txt_write(“***********************”&date()&” “&time()&” ************************************************************”)
Set rs1=excel_Record(filepath1,sql)
Do
If not rs1.eof Then
name1=rs1(“name”)
status1=rs1(“status”)
‘msgbox “name :”&name1&” status:”&status1
sqlname=”select name,status from [Sheet1$] where name=’”+name1+”‘”
Set rs2=excel_Record(filepath2,sqlname)
If rs2.eof=false Then
If status1=rs2(“status”) Then
‘MsgBox “服务 “&name1&” 状态相同,服务状态为:”&status1
msg=”服务 “&name1&” 状态相同,服务状态为:”&status1
txt_write(msg)
Else
‘MsgBox “服务 “&name1&” 状态不相同 “&status1
msg=”服务 “&name1&” 状态不相同 “&filepath1&”中状态为:”&status1&” “&filepath2&” 中状态为:”&rs2(“status”)
txt_write(msg)
End If
Else
‘MsgBox “list2.xls中不存在 :”&name1&” 服务”
msg=filename2&” 中不存在 :”&name1&” 服务”
txt_write(msg)
End If
rs1.MoveNext
else
msgbox “没有记录”
End If
loop until rs1.eof=True
Set rs1=nothing
Set rs2=nothing

Set rs3=excel_Record(filepath2,sql)
Do
name3=rs3(“name”)
msgbox name3
status3=rs3(“status”)
If rs3.eof=false Then

‘msgbox “name :”&name1&” status:”&status1
sqlname3=”select name,status from [Sheet1$] where name=’”+name3+”‘”
Set rs4=excel_Record(filepath1,sqlname3)
If rs4.eof=true Then
msg=filename1&” 中不存在 :”&name3&” 服务”
txt_write(msg)
End If
rs3.MoveNext
else
msgbox “没有记录”
End If
loop until rs3.eof=True

txt_write(“***************************************************************************************”)
End function
‘********************************************************************************end

‘*************************打印比较结果函数*********************************start
Public Function txt_write(msg)
txtpath=filepath&”\result.txt”
Set fso = CreateObject(“Scripting.FileSystemObject”)
If FilePathFind(txtpath) Then
Set f = fso.OpenTextFile(txtpath, 8, True)
f.Writeline msg
Else

fso.CreateTextFile txtpath, True
Set f = fso.OpenTextFile(txtpath, 8, True)
f.Writeline msg
End If
set f=nothing
Set fso =nothing
End Function

‘********************************************************************************end

不过这段脚本有个缺陷就是比较每个服务name都会提示msgbox做提示,这样显得不方便,如果服务的数量很多,那需要使用者一直守着点击msgbox的确定按钮,显得比较麻烦,所以这种需要改进,还有打印出来的日志结果也有点乱,需要做调整,暂时就这样,以后学习过程中不断改进。本来还要对指定路径下的excel进行判断,判断是xls还是xlsx扩展名的,但是电脑上没有安装对应的ODB软件,就没有做判断了。直接指定的xls扩展名。这儿也有一定的局限性。
    大家有什么好的建议也尽管给我提哦。。。谢谢。。

 

 


TAG:

 

评分:0

我来说两句

Open Toolbar