Function ReadFile(sFileName,sSheetName)
Dim oExcel
Dim oSheet
Dim oRange
Dim arrRange
On Error Resume Next
' 创建Excel应用程序对象
Set oExcel = CreateObject("Excel.Application")
If err.Number <> 0 Then
MsgBox "未能初始化Excel" & vbCrLf & _
"请确保Excel已安装", vbCritical
Exit Function
End If
On Error Goto 0
On Error Resume Next
' 打开Excel文件
oExcel.Workbooks.Open(sFileName)
If err.Number <> 0 Then
MsgBox "未能加载Excel文件" & vbCrLf & _
"请确保Excel文件路径正确或格式正确", vbCritical
Exit Function
End If
On Error Goto 0
COM接口 : Set excelApp = CreateObject("Excel.Application")
Set newBook = excelApp.Workbooks.open(excelpath) 'excelpath为EXCEL路径
For i= 1 to 10000 '若第N行是用例起始行,则修改为for i=N to 10000 ,10000是任意一个大于用例总数的数字
CaseName=newBook.WorkSheets(SheetName).Cells(i,j) 'SheetName是用例所在Sheet,i是用例所在行,j是用例所在列
if CaseName<>"" then
print CaseName
Else
exit for
End if
Next
excelApp.Workbooks.Close
excelApp.Quit
Set newBook=nothing
Set excelApp =nothing ADO接口: Set conn= createobject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&excelpath&";Extended Properties=Excel 8.0" 'EXCEL97-2003,excelpath为EXCEL所在路径
'conn.Open "Provider=Microsoft.Jet.OLEDB.12.0;Data Source="&excelpath&";Extended Properties=Excel 12.0" 'EXCEL2007要安装OLEDB12.0不然会报错
Set rs=CreateObject("ADODB.Recordset") '创建记录集
rs.Open "Select CaseName from [Sheet1$] ",conn 'SQL语句查询所有用例,CaseName 为用例所在列的列名,[Sheet1$]为用例所在Sheet
Do
If not rs.eof Then
CaseName=rs("CaseName")
print "用例名称:"&CaseName
rs.MoveNext
else
print "没有用例"
End If
loop until rs.eof=true
conn.close
Set rs=nothing
Set conn = nothing