QTP - excel操作
通常,我们与Excel的交互,是通过创建Excel对象的方式:
Set ExcelApp = CreateObject("Excel.Application")
但是在需要处理格式规范、数据量大的数据表的时候,不妨采取数据库的操作方式,使得操作更高效、灵活。
把Excel表格的第一行作为字段名,其余行作为数据项。
范例代码:
'创建连接字符串
'创建连接
SQL = "select * from [Sheet1$]"
Set Conn= createobject("ADODB.Connection")
ConnetString="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="&dataFile1&";Extended Properties=""Excel 8.0;HDR=yes;IMEX=1;"""
'ConnetString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z_9P_MAINTAIN_OFFER_ID.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Conn.Open ConnetString
Set RST = createobject("ADODB.Recordset")
RST.open SQL ,Conn,2,2
RST.MoveFirst
text = RST("OFFER_ID").value
'RST即为查询返回的结果集
'移动游标至顶行
RST.MoveFirst
'移动游标至下一行
RST.MoveNext
'更新结果集并保存到Excel
RST("Username").value = NewUser
RST.update
'关闭连接
RST.Close
Conn.Close
Set RST= Nothing
Set Conn = Nothing
2. 读写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
QTP - excel操作的更多相关文章
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- Delphi Excel 操作大全
Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...
- C#EXCEL 操作类--C#ExcelHelper操作类
主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...
- Excel 操作类
转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...
- C# excel操作
开源的Excel操作项目: http://www.cnblogs.com/lwme/archive/2011/11/27/2265323.html 添加引用:Microsoft Excel 11.0 ...
- [Excel操作]Microsoft Office Excel 不能访问文件
最近,客户服务器迁移,因操作系统环境变化而引起的的环境问题一堆,遇到的问题并解决方法在“[Excel]操作”类别会体现. Microsoft Office Excel 不能访问文件“C:\\LMSEx ...
- C#常用工具类——Excel操作类
/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...
- 报表中的Excel操作之Aspose.Cells(Excel模板)
原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...
- C# Excel操作类
/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...
- Excel操作 Microsoft.Office.Interop.Excel.dll的使用
----转载: http://www.cnblogs.com/lanjun/archive/2012/06/17/2552920.html 先说说题外话,前段时间近一个月,我一直在做单据导入功能,其中 ...
随机推荐
- javaScript Queue
function Queue() { var items = []; this.enqueue = function(element) { items.push(element) } this.deq ...
- python面试题之什么是Tkinter ?
TKinter是一款很知名的Python库,用它我们可以制作图形用户界面.其支持不同的GUI工具和窗口构件,比如按钮.标签.文本框等等.这些工具和构件均有不同的属性,比如维度.颜色.字体等. > ...
- 中文字符集编码unicode,gb2312,cp936,GBK,GB18030介绍
概要: UTF-8的一个特别的好处是它与ISO- 8859-1完全兼容,可以表示世界上所有的字符,汉字通常用3个字节来表示.GB2312的code page是CP20936.GBK的code page ...
- html input标签 要求只能输入纯数字
在input标签添加以下代码即可 oninput = "value=value.replace(/[^\d]/g,'')" <input type="text&qu ...
- Mac 安装cnpm
1.先安装node node的下载地址:http://nodejs.cn/download/ 这个没什么好说的,安装完成后测试一下,在终端输入:node -v 这时候就可以看到安装的nod ...
- jackson 问题定位
问题背景: 云计算Pass平台版本升级,导致引用的jackson的包直接由1.*升级为2.* .在版本1.*中对于字段名与实际json不符的直接忽略了,而在2.*中则会报错.诸如此类,有较大差异,需要 ...
- 解决error: Microsoft Visual C++ 14.0 is required 问题
1.https://964279924.ctfile.com/fs/1445568-239446865 2.重新安装 .Net framework 更高的版本:https://support.micr ...
- 使用 jQuery 制作京东网的焦点图
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- 简易的Master-Worker框架
Master-Worker模式是一种使用多线程进行数据处理的结构,多个worker进程协作处理用户请求,master进程负责维护worker进程,并整合最终处理结果 主要参与者 Worker:用于实际 ...
- qq for linux tar.gz安装
借用官网的一段话 QQ for Linux 怎么命令行安装和卸载 RPM版本 安装 ①打开控制台,使用管理员身份登录 :②在终端中输入命令“rpm –U package_name.rpm“ , pac ...