不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。                                           it is no use doing what you like, you have got to like what you do.

[转贴]使用Robot循环读取Excel中的数据

上一篇 / 下一篇  2006-12-20 19:34:48 / 个人分类:Robot技术

使用Robot循环读取Excel中的数据


w u*M|@5s0
51Testing软件测试网`U#W'YJV&_[f

51Testing软件测试网'A'qKdR&lSF+t'Ym

前几天使用Robot 循环对清单进行输入,但无奈CSV文件自动将清单首位的零抹除掉,使得从DataPool中读取数据不正确(见《使用Robot输入清单》),其中一种方法就是直接在脚本中 进行循环构造清单,这时我遇到另一个问题:构造的清单在清单库中不存在。程序对于不在库中的清单操作是不一样的,所以循环中的操作步骤就会报错。于是为了 脚本的简洁,只得重新回到从文件中读取清单的思路。选择从Excel文件中读取是较好的一种方法,因为我们可以在Excel中分不同的列保存清单号、定额 号、工程量等信息。51Testing软件测试网uCq O _)\1w&uvL jeF

51Testing软件测试网'\%|$g/ML1K8B3vl!{

Robot与Winrunner不同,他对于Excel文件的读取需要编写很多语句,下面是脚本:51Testing软件测试网9|kz)Z`z3lu

7P V9tfJw:J0'从excel表里读取数据的简单例子51Testing软件测试网K?&_K~I
'$include "sqautil.sbh" '头包含
7^0S/`D:K0Sub Main
x/g8K.]FLe]D0Dim Result As Integer51Testing软件测试网_#eV:kz#FA&I
dim excel as Object '定义对象51Testing软件测试网(m;h~?_7dd9}.Z
dim book as Object51Testing软件测试网7_ tI?+[M
dim worksheet as Object
&^[$A:h/yNw0dim name1 as String
nlp8A`^a)q Hg0dim pass as String
3S kUP;j'l0dim count as Integer

#F,qv m IT0

$Pg G7nY7J0 51Testing软件测试网 u[]:z7[+u+E

51Testing软件测试网3@O#S/n9_ MT4N D)~

on error resume next
gb/H4K`|(U0Set excel = GetObject("excel.application") '设置excel变量为execl对象51Testing软件测试网"PI;vu%jF.V
if(excel Is Nothing) then '判断是否设置成功
+bIX(`3u6D p0Set excel = CreateObject("excel.application") '创建excel对象51Testing软件测试网6{ L ULr;EuXa
if(excel Is Nothing) then '判断是否创建成功
5kmy F3N#O@0MsgBox "Couldn't find Excel!"51Testing软件测试网#RWv.n{P7|aQ
Exit Sub51Testing软件测试网u,_uZ8uj.F0y,z:^1|
End if51Testing软件测试网7{\V y9q*gu m-f
End if51Testing软件测试网X!cG)rA'b%oL!si

)Q&k;\3F.q1hUD0Set book = excel.Workbooks.Open("d:\qingd.xls") '打开具体Excel文件,这里是我d盘下一个名为qingd的xls文件51Testing软件测试网%u8[p$TB2k^!V1N
Set worksheet = book.Worksheets("1") '设置工作的表,工作表的名字为151Testing软件测试网T#A6kW:OR+N(w z

g(g!C8gm f%hfMv0For count=1 To 3 '在excel表里取三行数据

4{'v6Z"y\ ^ \#z0

H6y;w6D2j.vobx0
"NI9T:d+z2?4O ?0name1 = worksheet.Cells(count,1).Value '从表中取具体行列对应的值
XJ.d](Wbb$k0'pass = worksheet.Cells(2,1).Value51Testing软件测试网 W;y)j)fvO0e

51Testing软件测试网w9X?]Ip7{

msgbox name1
KMKZVd0next count

2G@qPA"J5NgY9N N051Testing软件测试网B_;b|!QF~|"A

excel.Quit '关闭excel51Testing软件测试网vM a1l;H-e
Set excel = Nothing '把excel对象设置为空

g{Z2N ]0

1C7mf1dP051Testing软件测试网 v$tA2R Y O2G
End Sub

`9~Z N \ U z0

TAG: Datapool 软件测试技术 Robot技术

 

评分:0

我来说两句

Open Toolbar