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)
'设置值
'写⼊完成后,保存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
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.unt
'获取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:写⼊哪⼀个⾏的数据
excel中值公式函数'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
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.unt
'获取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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论