[原创文章] 比较Excel的VBS函数

news/2024/7/7 9:58:45

QTP中Excel的比较

作者:Wally Yu (微博: http://weibo.com/quicktest)

最近在SQAForum经常有人提出关于比较两个Excel的值的问题的帖子

例如:

http://www.sqaforums.com/showflat.php?Cat=0&Number=549014&an=0&page=0#Post549014

http://www.sqaforums.com/showflat.php?Cat=0&Number=567634&an=0&page=0#Post567634

 

国人肯定在此问题上也会有一定的困惑,我把自己写的一些函数发出来,给大家予以一定的启发和思路

 

两个Excel比较的函数:

'*************************************************

'**     Functionality: compare two excels

'**     input parameter:

'**               excelFullPath1 - the first excelsheet

'**               excelFullPath2 - the secondexcel sheet

'**     output: if excel sheet are equal, return "equal",else return difference

'**     Author: Wally Yu in Shanghai,quicktest@yahoo.cn

'**     Date:1st May 2009

'*************************************************

Function CompareExcelSheet(excelFullPath1, excelFullPath2,sheetName)

     Dim rows1,rows2, columns1, columns2,ret, i,j, tempDoc1,tempDoc2, diffFlag

     diffFlag = False

 

     Dim srcData1,srcDoc1

     set srcData1 =CreateObject("Excel.Application")

     srcData1.Visible = False

     set srcDoc1 =srcData1.Workbooks.Open(excelFullPath1)

     srcDoc1.Worksheets(sheetname).Activate

     rows1 =srcDoc1.Worksheets(sheetname).UsedRange.rows.Count

     columns1 =srcDoc1.Worksheets(sheetname).UsedRange.columns.Count

    

     Dim srcData2,srcDoc2

     set srcData2 =CreateObject("Excel.Application")

     srcData2.Visible = False

     set srcDoc2 =srcData2.Workbooks.Open(excelFullPath2)

     srcDoc2.Worksheets(sheetname).Activate

     rows2 =srcDoc1.Worksheets(sheetname).UsedRange.rows.Count

     columns2 = srcDoc1.Worksheets(sheetname).UsedRange.columns.Count

    

     If (rows1 <> rows2) Or (columns1<> columns2) Then

                 ret = "Excel sheet "& sheetName & "used ranges are not equal"

                 diffFlag = True

     Else

                 For i = 1 To rows1

                             For j = 1 Tocolumns1

                                         tempDoc1= srcDoc1.Worksheets(sheetname).Cells(i,j).value

                                         tempDoc2= srcDoc2.Worksheets(sheetname).Cells(i,j).value

                                         IftempDoc1 <> tempDoc2 Then

                                                     diffFlag= True

                                                     ret= ret & "cell(" & i & ","& j & ") values are differert: value1 = " & tempDoc1& ", value2 = " &tempDoc2 & vbCrLf

                                         End If

                             Next

                 Next

                 ret = "Excel sheet "& sheetName & "cells are different:" & vbCrLf & ret

     End If

     If diffFlag = False Then

                 ret = "equal"

     End If

     'srcDoc1.Close

     'scrDoc2.Close

     CompareExcelSheet = ret

     srcData1.Workbooks.Close

     srcData2.Workbooks.Close

     srcData1.Quit

     srcData2.Quit

'    QTP_Read_Excel = ret

End Function


读取Excel单元格的函数:

 

PublicFunction readExcelCell(pathway,sheetname,x,y)

     On Error Resume Next

     Setfso=CreateObject("scripting.FileSystemObject")

     If fso.FileExists(pathway) Then

                 Dim srcData,srcDoc,ret

                 set srcData =CreateObject("Excel.Application")

                 srcData.Visible = False

                 set srcDoc =srcData.Workbooks.Open(pathway)

                 srcDoc.Worksheets(sheetname).Activate

                 ret =srcDoc.Worksheets(sheetname).Cells(x,y).value

                 srcData.Workbooks.Close

                 srcData.Quit

     Else

                 ret = "file notfound"

     End If

    

     Set fso = Nothing

     Set srcData = Nothing

     If Err Then

                 readExcelCell = Err.Description

     Else

                 readExcelCell = ret

     End If

End Function

 


 

写入Excel单元格函数

 

PublicFunction writeExcelCell(content,pathway,sheetname,x,y)

     On Error Resume Next

     Setfso=CreateObject("scripting.FileSystemObject")

     If fso.FileExists(pathway) Then

                 Dim srcData,srcDoc,ret

                 set srcData =CreateObject("Excel.Application")

                 srcData.Visible = False

                 set srcDoc =srcData.Workbooks.Open(pathway)

                 srcDoc.Worksheets(sheetname).Activate

                 srcDoc.Worksheets(sheetname).Cells(x,y).value = content

                 srcDoc.Save

                 srcData.Workbooks.Close

                 srcData.Quit

                 ret = "success"

     Else

                 ret = "File notfound"

     End If

    

     Set fso = Nothing

     Set srcData = Nothing

                

     If Err Then

                 writeExcelCell =Err.Description

     Else

                 writeExcelCell = ret

     End If

End Function


注:

这三个函数都是对Excel的基本操作,我们只是给还没有掌握的测试界的朋友一个基本思路

其实在真正自动化测试过程中可能需要更多的对于Excel的操作,各位朋友都可以参照这样的思路实现自己的操作
鄙人微博:http://weibo.com/quicktest,愿与大家一起讨论


http://www.niftyadmin.cn/n/3649278.html

相关文章

Flutter的HTTP请求

It’s hard to do any sort of development without having to make some form of HTTP request, therefore, we’ll be looking at how to use the http plugin within Flutter. 无需进行某种形式的HTTP请求就很难进行任何类型的开发&#xff0c;因此&#xff0c;我们将研究如…

android上的JAVA8:使用retrolambda

Java8引入了lambda表达式&#xff0c;让许多开发者激动不已&#xff0c;本文介绍如何在Android上使用lambda。 首先下载 java8 sdk &#xff0c;并且配置完成在 build.gradle 中加入编译的依赖&#xff0c;这会使用retrolambda来编译Java代码 buildscript { //开始加入depend…

react 生命挂钩_简而言之,React useContext挂钩

react 生命挂钩Early February 2019, React introduced Hooks as a way to rewrite your components as simple, more manageable, and classless. useContext is one of the built-in Hooks, giving functional components easy access to your context. But before we dive i…

OkHttp3的使用

一、首先需要Gradle,GitHub的链接:http://square.github.io/okhttp/ compile com.squareup.okhttp3:okhttp:3.9.0 二、测试get方法 /*** 测试get方法*/ Test public void testGet() {//创建OKHttpClient对象OkHttpClient okHttpClient new OkHttpClient();//创建request对象Re…

Android 使用Fragment,ViewPagerIndicator 开发APP项目的主框架

本来准备下载个CSDN的客户端放手机上&#xff0c;没事可以浏览浏览资讯&#xff0c;下载了官方的之后&#xff0c;发现并不能很好的使用。恰好搜到一个大神自己写的csdn的app&#xff0c;下载安装了一下&#xff0c;感觉很不错&#xff0c;也很流畅&#xff0c;基本满足了我们 …

spread运算符_JavaScript中的Spread运算符有什么用?

spread运算符Learn about the ES6 spread operator, and some practical uses for this powerful JavaScript feature! 了解ES6传播运算符&#xff0c;以及此强大JavaScript功能的一些实际用法&#xff01; The spread operator is a feature of JavaScript introduced with E…

E测中国翻译团队成立,首战告捷!

应广大网友的热烈要求&#xff0c;E测翻译小组正式成立&#xff0c;对外宣传的E测翻译团队首页&#xff1a;https://sites.google.com/site/5etestingtranslating/home 说明&#xff1a;E测翻译团队为E测中国&#xff08;www.5etesting.com&#xff09;旗下的翻译团队&#xf…

基于MVP架构的OKHttp3的封装

一、OKHttp的优势 url测试地址&#xff1a;http://httpbin.org 二、首先对Response进行封装 ①定义IResponse接口 public interface IResponse {//状态码int getCode();//数据体String getData(); } ②BaseResponse实现继承IResponse public class BaseResponse implements …