1. 以数据库的形式访问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操作的更多相关文章

  1. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  2. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

  3. C#EXCEL 操作类--C#ExcelHelper操作类

    主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...

  4. Excel 操作类

    转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...

  5. C# excel操作

    开源的Excel操作项目: http://www.cnblogs.com/lwme/archive/2011/11/27/2265323.html 添加引用:Microsoft Excel 11.0 ...

  6. [Excel操作]Microsoft Office Excel 不能访问文件

    最近,客户服务器迁移,因操作系统环境变化而引起的的环境问题一堆,遇到的问题并解决方法在“[Excel]操作”类别会体现. Microsoft Office Excel 不能访问文件“C:\\LMSEx ...

  7. C#常用工具类——Excel操作类

    /// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...

  8. 报表中的Excel操作之Aspose.Cells(Excel模板)

    原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...

  9. C# Excel操作类

    /// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...

  10. Excel操作 Microsoft.Office.Interop.Excel.dll的使用

    ----转载: http://www.cnblogs.com/lanjun/archive/2012/06/17/2552920.html 先说说题外话,前段时间近一个月,我一直在做单据导入功能,其中 ...

随机推荐

  1. apt-cyg for Cygwin(setup-x86_64 .exe )在win10下的安装

    cygwin安装后,如果没有选择安装所有包(这会占用5G空间,很多包不需要),再需要安装新的包,可以启动setup-x86_64 .exe(我把它放置在C:\cygwin64目录下),添加包(如wge ...

  2. golang的数据类型之基本数据类型的默认值和转换

    默认值: 整型的默认值:0 浮点型的默认值:0字符串的默认值:""   //空布尔类型的默认值:false package mainimport "fmt" f ...

  3. Redis项目实战,一些经验总结

    来源:https://my.oschina.net/u/920698/blog/3031587 背景 Redis 是一个开源的内存数据结构存储系统. 可以作为数据库.缓存和消息中间件使用. 支持多种类 ...

  4. Python爬虫抓取 python tutorial中文版,保存为word

    看到了中文版的python tutorial,发现是网页版的,刚好最近在学习爬虫,想着不如抓取到本地 首先是网页的内容 查看网页源码后发现可以使用BeautifulSoup来获取文档的标题和内容,并保 ...

  5. 2019 牛客多校第一场 B Integration

    题目链接:https://ac.nowcoder.com/acm/contest/881/B 题目大意 给定 n 个不同的正整数 ai,求$\frac{1}{\pi}\int_{0}^{\infty} ...

  6. WPF绑定のRelativeSource

    在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...

  7. Dagger2 探索记2——四大基本组件(二)

    书接上文,先回顾以下前一章写的内容. 内容大概就是在Activity中用@Inject标记一个注入的类,然后在这个类的构造函数上也打个@Inject标记,然后使用@Component来连接两边,完成对 ...

  8. MAC如何与linux服务器传递文件

    scp命令可以从本地拷贝文件到Linux服务器,也可以将Linux服务器文件下载到本地 将远程/root/articaleFetch/dist目录下文件和文件夹拷贝到dist文件夹 scp root@ ...

  9. java环境--JDK和Tomcat在linux上的安装和配置

    Tomcat在Linux上的安装与配置 以下使用的Linux版本为: Redhat Enterprise Linux 7.0 x86_64,Tomcat版本为tomcat-7.0.54.1.下载JDK ...

  10. 分析dwebsocket的源码过程

    前言 dwebsocet 是python django的websocket库,github地址:https://github.com/duanhongyi/dwebsocket 本章是对dwebsoc ...