QTP对Excel的操作(EOM) 类封装
2012-09-14 16:29
274 查看
接上一篇,我把Excel读取的出来的数据封装成一个Dictionary对象,这样调用起来比较方便。调用的时候根据输入的所需要第几行数据取得数据
下面是代码:
Class ExcelDic
Public Dic 'Dictionary对象存放查询到指定行的Excel结果集
'Public DicDictionary '存放所有的行记录,以行号作为Key值
Public FilePath 'Excel文件存放路径
Public SheetName '要查询的Sheet名称,即表名
Private RowIndex '要查询的数据所在行
Public Default Function Load(pth,sheet,row)
Set Dic = CreateObject("Scripting.Dictionary")
'Set DicDictionary = CreateObject("Scripting.Dictionary")
FilePath = pth
SheetName = sheet
RowIndex = row
GetContext
Set Load = Dic
End Function
Private Function GetContext()
Dim connStr,sqlStr
Dim conn,rst
Set conn = CreateObject("ADODB.CONNECTION")
Set rst = CreateObject("ADODB.RECORDSET")
'连接数据库的字符串,后面的“HDR=yes”需要注意,它的意思是把Excel表第一行作为字段名,第二行开始方是有效数据。
'HDR=no则反之,从第一行开始就看做有效数据
'2007中新的Provider为Microsoft.ACE.OLEDB.12.0,之前的2003为Microsoft.Jet.OLEDB.4.0
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & ";Extended Properties=""Excel 8.0;HDR=yes;IMEX=1"""
sqlStr = "select * from [" & SheetName & "$]"
conn.Open connStr
Set rst = conn.Execute(sqlStr)
rst.MoveFirst
For i=2 to RowIndex
If rst.EOF Then
Exit For
End If
rst.MoveNext
Next
If rst.EOF Then
msgbox "Record Set 游标越界"
Exit Function
End If
For i = 0 to rst.Fields.Count-1
Dic.Add rst.Fields.Item(i).Name,rst.Fields.Item(i).Value
Next
End Function
End Class
'测试代码
Set aa = new ExcelDic
Set myDic = aa.Load("d:\1.xlsx","sunyu",1)
msgbox myDic("PWD")
msgbox myDic("Age")
下面是代码:
Class ExcelDic
Public Dic 'Dictionary对象存放查询到指定行的Excel结果集
'Public DicDictionary '存放所有的行记录,以行号作为Key值
Public FilePath 'Excel文件存放路径
Public SheetName '要查询的Sheet名称,即表名
Private RowIndex '要查询的数据所在行
Public Default Function Load(pth,sheet,row)
Set Dic = CreateObject("Scripting.Dictionary")
'Set DicDictionary = CreateObject("Scripting.Dictionary")
FilePath = pth
SheetName = sheet
RowIndex = row
GetContext
Set Load = Dic
End Function
Private Function GetContext()
Dim connStr,sqlStr
Dim conn,rst
Set conn = CreateObject("ADODB.CONNECTION")
Set rst = CreateObject("ADODB.RECORDSET")
'连接数据库的字符串,后面的“HDR=yes”需要注意,它的意思是把Excel表第一行作为字段名,第二行开始方是有效数据。
'HDR=no则反之,从第一行开始就看做有效数据
'2007中新的Provider为Microsoft.ACE.OLEDB.12.0,之前的2003为Microsoft.Jet.OLEDB.4.0
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & ";Extended Properties=""Excel 8.0;HDR=yes;IMEX=1"""
sqlStr = "select * from [" & SheetName & "$]"
conn.Open connStr
Set rst = conn.Execute(sqlStr)
rst.MoveFirst
For i=2 to RowIndex
If rst.EOF Then
Exit For
End If
rst.MoveNext
Next
If rst.EOF Then
msgbox "Record Set 游标越界"
Exit Function
End If
For i = 0 to rst.Fields.Count-1
Dic.Add rst.Fields.Item(i).Name,rst.Fields.Item(i).Value
Next
End Function
End Class
'测试代码
Set aa = new ExcelDic
Set myDic = aa.Load("d:\1.xlsx","sunyu",1)
msgbox myDic("PWD")
msgbox myDic("Age")
相关文章推荐
- QTP对Excel的操作(EOM)
- 新手封装的POI操作Excel
- QTP之excel操作函数整理
- C# NPOI excel操作封装
- QT操作Excel封装类(包含高级功能:合并单元格,文本及单元格格式设定等)
- QT 操作excel 类封装(转载)
- Orcad原理图封装库批量编辑引脚时需要配合Excel进行制作时候需要操作(遇到无法复制粘贴操作)
- QTP-利用EOM自动化EXCEL
- QT 操作excel 类封装使用
- 封装poi操作Excel工具类
- 自己封装的poi操作Excel工具类
- excelpanel--extjs中嵌入excel,并封装简单操作
- QT 操作excel 类封装
- JavaWEB--POI之EXCEL操作、优化、封装详解系列(二)--导入导出helloworld(对比JXL库)
- java中Excel操作的简单封装
- QTP对Excel的操作(三)之 Vbs对Excel的操作 实例讲解
- 封装对NPOIExcel的操作,方便的设置导出Excel的样式
- JavaWEB--POI之EXCEL操作、优化、封装详解系列(三)--万能POI之EXCEL导出工具--PoiExportUtil入门篇
- 一个QTP中用ADO操作Excel的例子
- JXL.jar简单封装Excel读写操作