刚开始在公司里面使用 silktest的时候,不止一位老员工告诉我,silktest对excel支持不好,有的干脆遇到 excel就截图 。也有同事调用vb函数对excel进行操作。也有同事用silk自带函数db_connect()进行操作。
这几天比较闲,就对DB系列函数小小研究了一下。对一个叫122.xlsx的excel中的sheet1表进行操作,读取下一行,前一行,列名,表名。 写了如下代码:
[-] testcase CQTEST()
[ ] List of STRING lsExcel
[ ] HDATABASE hDB1
[ ] STRING cat, sch, tbl, name, tbltype, ignore,col,colum,s1,s2,s3
[ ]
[ ]
[ ] HSQL hstmnt
[ ] String sTable = "[Sheet1$]"
[ ] String sSql = "SELECT * FROM {sTable}"
[ ] String sFile = "C:\122.xlsx"
[ ] hDB1 = DB_Connect ("dsn={"Excel"};DBQ={sFile}")
[ ]
[-] if(hDB1 == NULL)
[ ] print("Error : Fail to open Excel.")
[ ] hstmnt = DB_ExecuteSql(hDB1,sSql)
[ ]
[ ] int i
[-] while(DB_FetchNext(hstmnt,lsExcel)== true)
[ ] print(lsExcel[1])
[ ] print(lsExcel[2])
[ ] print(lsExcel[3])
[-] while(DB_FetchPrev(hstmnt,lsExcel)== true)
[ ] print(lsExcel[1])
[ ] print(lsExcel[2])
[ ] print(lsExcel[3])
[ ] cat = NULL
[ ] sch = NULL
[ ] tbl = "Sheet1%"
[ ] tbltype = NULL
[ ] col="%"
[ ] hstmnt = DB_Columns (hDB1, cat, sch, tbl, col)
[-] while(DB_FetchNext(hstmnt,s1,s2,s3,colum)== true)
[ ] print("s1: {s1}")
[ ] print("s2: {s2}")
[ ] print("s3: {s3}")
[ ] print(colum)
[ ]
[ ] hstmnt = DB_Tables (hDB1, cat, sch, tbl, tbltype)
[-] while (DB_FetchNext (hstmnt,ignore,ignore,ignore,name) == TRUE)
[ ] print (name)
结果如下
[-] Testcase CQTEST - Passed
[ ] aaa
[ ] 555
[ ] 666
[ ] rerw
[ ] wrwe
[ ] werw
[ ] rerw
[ ] wrwe
[ ] werw
[ ] aaa
[ ] 555
[ ] 666
[ ] s1: C:\122.xlsx
[ ] s2: NULL
[ ] s3: Sheet1$
[ ] 1213
[ ] s1: C:\122.xlsx
[ ] s2: NULL
[ ] s3: Sheet1$
[ ] fwefsrd
[ ] s1: C:\122.xlsx
[ ] s2: NULL
[ ] s3: Sheet1$
[ ] 333
[ ] SYSTEM TABLE
[ ] 2012-02-03 13:32:57
[ ] 2012-02-03 13:32:57