1.将excel数据导入DataTable
Set Excelobj=createobject("Excel.Application")
excelpath="C:UsersAdministratorDesktop4.xls"
Excelobj.Workbooks.Open(excelpath) '打开excel的文件
Set newsheet=Excelobj.Sheets.Item(1) '打开excel的sheet
colcount=newsheet.UsedRange.Columns.Count 'Add to Header
For i =1 To colcount
param=newsheet.Cells(1,i)
DataTable.GetSheet("Action1").AddParameter param,""
next
rowcount=newsheet.UsedRange.Rows.Count 'Add to content
colcount=newsheet.UsedRange.Columns.Count
For i = 2 To rowcount
DataTable.GetSheet("Action1").SetCurrentRow i-1
For j =1 To colcount
param=newsheet.Cells(i,j)
DataTable.value(j,"Action1")=param
Next
Next
Excelobj.Quit
导入excel之后,平常都习惯用DataTable("列名",dtLocalSheet),但是这样的在excel导入之后默认取的是最后一行数据,所以如何在导入的数据中从第一行开始呢?
1)这里我们就要用到QTP的 ValueByRow函数了。具体语法为:DataTable.GetSheet("Action1").GetParameter("列名").ValueByRow(行数)
举例:eTime=DataTable.GetSheet("Action1").GetParameter("endTime").ValueByRow(1)这样eTime取到的便是列名为"endTime“的第一行的数据了。