设为首页 加入收藏

TOP

qtp之excel操作函数都有哪些?(二)
2014-11-23 22:07:30 来源: 作者: 【 】 浏览:11
Tags:qtp excel 操作 函数 都有 哪些
ntCol).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

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇QTP面试题:如何用QTP录制鼠标右.. 下一篇Java企业面试题整理集合(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: