以前写的QTP的脚本(excel相关)

原创:转载请注明出处:--Jason Lee

放在DLL-VB(动态库工程)中的常用的excel操作的代码:

以下函数对于读取单个的单元格的值比较方便,如果要去很多单元格的值,经测试,此函数效率不高。

Function ReadExcelForCell(sFileName, nIndex, nRow, nColumn)
  Dim oExcelApp 'as excel.application
  Dim oExcelBook 'as excelbook object
  Dim oExcelSheet 'as excelsheet object
  Dim sStr1 'to stroe the return value(string)
  Set oExcelApp = CreateObject("Excel.Application")
  Set oExcelBook = oExcelApp.Workbooks.Open(sFileName)
  Set oExcelSheet = oExcelBook.Worksheets.Item(nIndex)
  sStr1 = oExcelSheet.Cells(nRow, nColumn)
  ReadExcelForCell = sStr1
  Set oExcelSheet = Nothing
  oExcelBook.Close
  oExcelApp.Quit  '必须退出对象,要不会一直存在excel的进程。
  Set oExcelBook = Nothing
  Set oExcelApp = Nothing
  End Function

 

下面的这个函数对于一次读取很多值很方便,不会频繁的调用函数锁在的堆栈,效率更高~

Function ReadExcelForArray(sFileName, nIndex)
  Dim oExcelApp 'as excel.application
  Dim oExcelBook 'as excelbook object
  Dim oExcelSheet 'as excelsheet object
  Dim sStr1 'to stroe the return value(string)
  Set oExcelApp = CreateObject("Excel.Application")
  Set oExcelBook = oExcelApp.Workbooks.Open(sFileName)
  Set oExcelSheet = oExcelBook.Worksheets.Item(nIndex)
  sStr1 = oExcelSheet.UsedRange.Cells
  ReadExcelForArray = sStr1
  Set oExcelSheet = Nothing
  oExcelBook.Close
  oExcelApp.Quit  '必须退出对象,要不会一直存在excel的进程。
  Set oExcelBook = Nothing
  Set oExcelApp = Nothing
 

End Function

这个函数的核心就是让excel中的值传给数组。

 

QTP对于excel的应用主要是读取其中的值,写的操作一般用的不多。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值