qtp之excel操作函数都有哪些?(一)

2014-11-23 22:07:30 · 作者: · 浏览: 19

******************************************************
‘Function:读Excel中的某个值
‘Input parameter:
‘strFilePath:保存Excel的文件路径
‘strExcelSheetName:要读取的Excel中Sheet的名称
‘intRow:读取哪一行的数据
‘intCol:读取哪一列的数据
‘For example:”E:\a.xls”,”Sheet1″,2,3
‘Return:取到的值
‘******************************************************
function getOneva lue(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中值, 并返回
getOneva lue = 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 setOneva lue(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,i