QTP之excel操作函数整理

*****************************************************  
'Function:读Excel中的某个值
'Input parameter:
 'strFilePath:保存Excel的文件路径
 'strExcelSheetName:要读取的Excel中Sheet的名称
 'intRow:读取哪一行的数据
 'intCol:读取哪一列的数据
'For example:"E:\a.xls","Sheet1",2,3
'Return:取到的值
'****************************************************** 

function getOneValue(strFilePath,strSheetName,intRow,intCol)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)
    
 '获取excel中值, 并返回
    getOneValue = ExcelSheet.Cells(intRow,intCol)
   
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
end function


'******************************************************  
'Sub:给excel中写入一条数据
'Input parameter:
 'strExcelSheetName:要写入的Excel中Sheet的名称
 'intRow:往哪一行的写数据
 'intCol:往哪一列的写数据
 'strValue:写入的值
'For example:"E:\a.xls","Sheet1",2,3,"111"
'Return:
'****************************************************** 

sub setOneValue(strFilePath,strSheetName,intRow,intCol,strValue)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)
 
 '设置值
 ExcelSheet.cells(intRow,intCol).value =strValue  
   
    '写入完成后,保存EXCEL
    ExcelApp.DisplayAlerts=False
 ExcelApp.Save 
 
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
 
end sub


'******************************************************  
'Function:读Excel中某一列的值
'Input parameter:
 'strFilePath:保存Excel的文件路径
 'strExcelSheetName:要读取的Excel中Sheet的名称
 'intCol:读取哪一个列的数据
'For example:"E:\a.xls","Sheet1",2
'Return:取到的值
'****************************************************** 

function getColValues(strFilePath,strSheetName,intCol)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet,intRowscount,arrValues()
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName) 
 
 '得到excel中共有几行 
 intRowscount =ExcelBook.ActiveSheet.UsedRange.Rows.Count
    
 '获取excel中值
 Redim Preserve arrValues (intRowscount-1)
 For i=1 to  intRowscount
      arrValues(i-1) = ExcelSheet.Cells(i,intCol)
    Next

 '返回值
 getColValues=arrValues
    
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
 
end Function


'******************************************************  
'Sub: 写入Excel中某一列的值
'Input parameter:
 'strFilePath:保存Excel的文件路径
 'strExcelSheetName:要写入Sheet的名称
 'intCol:写入哪一个列的数据
 'intFromrow:从哪里行开始写
 'arrValue:写入值(数组)
'For example:"E:\a.xls","Sheet1",2,2,arrRes
'Return:
'****************************************************** 
Sub setColValues(strFilePath,strSheetName,intCol,intFromRow,arrValue)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet,intRowscount
  Dim intArrColumnsCount,intColumnsCount
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName) 
 
 'ExcelSheet.activate
 '得到数组的大小
 intArrColumnsCount=UBound(arrValue)
 
 '最后写到哪一行
 intRowCount=intFromRow+intArrColumnsCount

 '设置值
 For i=intFromRow To intRowCount
  ExcelSheet.cells(i,intCol).value =arrValue(i-intFromRow) 
    Next
    
    '写入完成后,保存EXCEL
    ExcelApp.DisplayAlerts=False
 ExcelApp.Save 
 
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
End Sub


'******************************************************  
'Function:读Excel中某一行的值
'Input parameter:
 'strFilePath:保存Excel的文件路径
 'strExcelSheetName:要读取的Excel中Sheet的名称
 'intRow:读取哪一行的数据
'For example:"E:\a.xls","Sheet1",1
'Return:取到的值
'****************************************************** 

function getRowValues(strFilePath,strSheetName,intRow)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet,intColumnsCount,arrValues()
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName) 
 
 '得到excel中共有几列 
    intColumnsCount  =ExcelBook.ActiveSheet.UsedRange.Columns.count
    
 '获取excel中值 
 Redim Preserve arrValues(intColumnsCount -1)
 For i=1 to  intColumnsCount 
        arrValues(i-1) = ExcelSheet.Cells(intRow,i)
    Next
 
 '返回值
 getRowValues=arrValues
    
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
 
end Function


'******************************************************  
'Sub: 写入Excel中某一行的值
'Input parameter:
 'strFilePath:保存Excel的文件路径
 'strExcelSheetName:要写入Sheet的名称
 'intRow:写入哪一个行的数据
 'intFromCol:从哪里列开始写
 'arrValue:写入值(数组)
'For example:"E:\a.xls","Sheet1",5,2
'Return:
'******************************************************

Sub setRowValues(strFilePath,strSheetName,intRow,intFromCol,arrValue)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet,intColcount
  Dim intArrColumnsCount,intColumnsCount
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)

 '得到数组的大小
 intArrColumnsCount=UBound(arrValue)
 
 '最后写到哪一列
 intColcount=intFromCol+intArrColumnsCount

 '设置值
 For i=intFromCol To intColcount
  ExcelSheet.cells(intRow,i).value =arrValue(i-intFromCol) 
    Next
    
    '写入完成后,保存EXCEL
    ExcelApp.DisplayAlerts=False
 ExcelApp.Save 
 
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet

End Sub


'******************************************************  
'Function:读Excel中所有的值
'Input parameter:
 'strFilePath:保存Excel的文件路径
 'strExcelSheetName:要读取的Excel中Sheet的名称
'For example:"E:\a.xls","Sheet1"
'Return:取到的值
'****************************************************** 

function getAllValues(strFilePath,strSheetName)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet,intRowscount,intColumnsCount,arrGetCellValue()
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName) 
 
 '得到excel中共有几列 
 intRowscount =ExcelBook.ActiveSheet.UsedRange.Rows.Count
    intColumnsCount =ExcelBook.ActiveSheet.UsedRange.Columns.count
    
 '获取excel中值 
 Redim Preserve arrGetCellValue (intRowscount-1,intColumnsCount-1)
 For i=1 To intRowscount
   For j=1 to  intColumnsCount
         arrGetCellValue(i-1,j-1) = ExcelSheet.Cells(i,j)
      Next
    Next

 '返回值
 getAllValues=arrGetCellValue
    
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
 
end Function


'******************************************************  
'Function:读取某值第一次出现的行号
'Input parameter:
 'strFilePath:Excel文件保存的路径
 'strSheetName:要读取的Excel中Sheet的名称
 'Value:第一次出现的值
'For example:"E:\a.xls","Sheet1","root"
'Return:行号
'******************************************************

Function getRowByValue(strFilePath,strSheetName,Value)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet
  Dim rowcount,colcount
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)
 
 '取得EXCEL表共有几行、几列
 rowcount =ExcelBook.ActiveSheet.UsedRange.Rows.Count
 colcount=ExcelBook.ActiveSheet.UsedRange.Columns.Count  
 
 '从行开始循环
 For i=1 To rowcount
  For j=1 To colcount
   '判断是否找到需要的值
   If ExcelSheet.cells(i,j)= Value Then
      
      '返回值    
    getRowByValue=i
    
    '如果找到了此值,退出循环
       Exit for
      End If      
  Next 
  
  '如果找到了此值,退出循环
  If getRowByValue <>"" Then
   Exit For 
  End If 
 Next
 
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
 
End Function


'******************************************************  
'Function:读取某值第一次出现的列号
'Input parameter:
 'strFilePath:Excel文件保存的路径
 'strSheetName:要读取的Excel中Sheet的名称
 'Value:第一次出现的值
'For example:"E:\a.xls","Sheet1","root"
'Return:行号
'****************************************************** 
Function getColByValue(strFilePath,strSheetName,Value)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet
  Dim rowcount,colcount
  
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)
 
 '取得EXCEL表共有几行、几列
 rowcount =ExcelBook.ActiveSheet.UsedRange.Rows.Count
 colcount=ExcelBook.ActiveSheet.UsedRange.Columns.Count  
 
 '从行开始循环
 For i=1 To rowcount
  For j=1 To colcount
   '判断是否找到需要的值
   If ExcelSheet.cells(i,j)= Value Then
      
      '返回值
    getColByValue=j
       
       '如果找到了此值,退出循环
       Exit for
      End If      
  Next 
  
  '如果找到了此值,退出循环
  If getColByValue <>"" Then
   Exit For 
  End If 
 Next
 
 '关闭Excel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
 
End Function


'******************************************************  
'Function:初始化数据,获取测试数据
'Input parameter:
 'strFilePath: 测试数据Excel的文件路径
 'strSheetName:要读取的Excel中Sheet的名称
 'colNumber:标示符所在列
 'flag:是否执行的标示符
 'parmNumbers:测试参数的个数
 
'For example:"D:\test.xls","login",1,"x",4
'Return:测试数据(二维数组)
    '第一列为每条测试数据在excel中的行号,以便于结果的写回
'******************************************************

Function getTestdata( strFilePath,strSheetName,colNumber,flag,parmNumbers)
  '定义变量
  Dim ExcelApp,ExcelBook,ExcelSheet,rowcount,colcount,array(),arra(),k
    
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)
 
 '取得EXCEL表共有几行、几列
 rowcount=ExcelBook.ActiveSheet.UsedRange.Rows.Count
 colcount=ExcelBook.ActiveSheet.UsedRange.Columns.Count  
 
 '确定哪些行的数据需要执行,存在一维数组中
 m=0
 For i=1 To rowcount
   If   ExcelSheet.cells(i,colNumber)= flag Then
   ReDim PreServe arra(m)
   arra(m)=i
   m=m+1
  End If 
 Next
 
 '重定义二纬数组,第一列存放每条测试数据行号,及测试数据的参数
  ReDim PreServe array(m-1,parmNumbers)
  
  For i=0 To m-1
   array(i,0)=arra(i)   
   For j=1 To parmNumbers
   array(i,j)=ExcelSheet.cells(arra(i),j+colNumber)
   Next 
  Next 
 
 '返回值 
 getTestdata=array
 
 '关闭Ecxel
 closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet

End Function


'******************************************************  
'Sub:根据过滤的测试数据写入结果
'Input parameter:
 'strFilePath: 测试数据Excel的文件路径
 'strSheetName:要读取的Excel中Sheet的名称
 'arrData: 存放测试数据的数组
 'strColName: 结果的列名
 'arrResult: 存放测试结果的数组
'For example:"D:\1.xls","sheet1",arrData,"actualResult",arrResult
'Return:
'****************************************************** 
Sub setResultByArrdata(strFilePath,strSheetName,arrData,resultColname,arrResult)
Dim ExcelApp,ExcelBook,ExcelSheet,notNullNumber,intCol
    '创建EXCEL程序,打开工作簿,设置当前活动sheet
 Set ExcelApp = CreateObject("Excel.Application")
 Set ExcelBook = ExcelApp.WorkBooks.Open(strFilePath)
 Set ExcelSheet = ExcelBook.WorkSheets(strSheetName)

    '取得EXCEL表共有几行、几列
 rowcount =ExcelBook.ActiveSheet.UsedRange.Rows.Count
 colcount=ExcelBook.ActiveSheet.UsedRange.Columns.Count 
 
 intCol =getColByValue(strFilePath,strSheetName,resultColname)
 '统计结果所在的列有多少行不为空
 notNullNumber=0
 
 For i=1 To rowcount
  If ExcelSheet.cells(i,intCol)<>"" Then 
   notNullNumber=notNullNumber+1
  End If 
 Next 
 
 If notNullNumber=1 Then 
  For i=0 To UBound(arrResult)
   ExcelSheet.cells(arrData(i,0),intCol).value = arrResult(i)
  Next 
 Else 
  For i=0 To UBound(arrResult)
   ExcelSheet.cells(arrData(i,0),colcount+1).value = arrResult(i)
  Next  
 End If 
 
 ExcelApp.DisplayAlerts = false
    ExcelApp.Save
    closeExcelSheet  ExcelBook,ExcelApp,ExcelSheet
End Sub


'******************************************************  
'Sub:关闭Excel
'Input parameter:
 'ExcelBook:打开的Excel
 'ExcelApp:创建的Excel对象
 'ExcelSheet:当前活动的表单
'For example:ExcelBook,ExcelApp,ExcelSheet
'Return:
'****************************************************** 
Sub closeExcelSheet(ExcelBook,ExcelApp,ExcelSheet)
 
 ExcelBook.Close
 ExcelApp.Quit
 Set ExcelApp = Nothing
 Set ExcelBook = Nothing
 Set ExcelSheet = Nothing
 
End Sub

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值