TestComplete中操作excel还是非常方便的,这里提供了一个写的函数和一个读的示例,如果您需要用到,稍微改写一下应该就能对付了。
这个程序本身来讲,没有什么特别的地方,唯一需要注意的是
aqFile.Delete("C:\Documents and Settings\" & Sys.UserName & "\My Documents\RESUME.XLW")
这里地方可能需要根据你的实际情况(操作系统的用户目录)要做修改,每次保存excel文件操作前,需要先删除掉RESUME.XLW。这个函数还调用了FblnFileExist判断文件是否存在的函数,需要留心。
这是写excel文件的:
'打开excel文件,写入一个数组到指定的起止位置
Function FblnWrite2xls(strXlsFile,lngXstart,lngYstart,arrData)
Dim excel
Dim MsExcel
Dim i,j
FblnWrite2xls=false
Set excel = Sys.WaitProcess("EXCEL")
If excel.Exists Then
Call excel.Terminate()
End If
on error resume next
Set MsExcel = Sys.OleObject("Excel.Application")
if Err.Number <> 0 then
call Log.Warning("初始化 MS Excel 失败", "", pmHigher)
FblnWrite2xls=False
exit Function
end if
if not FblnFileExist(strXlsFile) then
Log.Warning(strXlsFile & " 不存在")
FblnWrite2xls=False
exit Function
End If
Call MsExcel.Workbooks.Open(strXlsFile)
Log.Message("打开:" & strXlsFile)
MsExcel.Visible = True
MsExcel.Cells(1,1).Activate
for i=LBound(arrData) to UBound(arrData)
MsExcel.Cells(lngXstart+i,lngYstart).Value=arrData(i)
next
aqFile.Delete("C:\Documents and Settings\" & Sys.UserName & "\My Documents\RESUME.XLW")
MsExcel.DisplayAlarts = False
MsExcel.AlertBeforeOverwriting=False
MsExcel.Save
MsExcel.Close
MsExcel.Quit
set MsExcel=Nothing
FblnWrite2xls=true
End Function
'判断文件是否存在
'例子: if lib.FblnFileExist(Project.Path + "001.xlsx") then
Function FblnFileExist(strFile)
If Not aqFile.Exists(strFile) Then
FblnFileExist=False
Else
FblnFileExist=True
End If
End Function
俗语有云:别人嚼过的馍馍不香。所以,读excel文件我就没有再写成函数,只是提供了一个例子,可以根据自己的需要稍加改造就能用了。当然如果谁写成了通用的函数,也可以发给我共享给大家。
'读excel文件内容,这里只是一个示例,需要修改一下,把文件名和需要读取的范围作为参数就可以了
sub SreadXls
dim i
dim j
dim strFileName
strFileName=Project.Path&"\data\001.xls"
'检查Excel是否启动,如果是启动的,就关闭了
Set oleExcel = CreateObject("Excel.Application")
set ExcelFile = Sys.WaitProcess("EXCEL")
if ExcelFile.Exists then
ExcelFile.Terminate()
set MsExcel = Sys.OleObject("Excel.Application")
else
Log.Warning("Unable to initialize MS Excel.")
Exit Sub
end if
'MsExcel.Visible = True
MsExcel.Workbooks.Open(strFileName)
for i = 1 to 7
strVal=MsExcel.Cells(i,1).Value
next
MsExcel.Workbooks.Close
Set oleExcel = Nothing
end sub
附件:http://download.csdn.net/source/3515484
内含<EXCEL操作示例.txt>,就那几个函数