QTP操作Excel的函数

使用QT操作Excel 表格的常用方法 使用QT操作excel的常用方法 阅读详情

最近有不少人在问QTP操作Excel的问题,其实QTP安装目录中的CodePlusSample里面就有一个名为“UsingExcel.vbs”的文件,里面有很多操作Excel的函数: 

Dim ExcellApp 'As Excel.Application
Dim excelSheet1 'As Excel.worksheet
Dim excelSheet2 'As Excel.worksheet
 
Set ExcelApp = CreateExcel()
 
'Create a workbook with two worksheets
ret = RenameWorksheet(ExcelApp, "Book1", "Sheet1", "Example1 Sheet Name")
ret = RenameWorksheet(ExcelApp, "Book1", "Sheet2", "Example2 Sheet Name")
ret = RemoveWorksheet(ExcelApp, "Book1", "Sheet3")
 
'SaveAs the work book
ret = SaveWorkbook(ExcelApp, "Book1", "D:/Example1.xls")
 
'Fill worksheets
Set excelSheet1 = GetSheet(ExcelApp, "Example1 Sheet Name")
Set excelSheet2 = GetSheet(ExcelApp, "Example2 Sheet Name")
For column = 1 to 10
   For row = 1 to 10
        SetCellValue excelSheet1, row, column, row + column
        SetCellValue excelSheet2, row, column, row + column
    Next
Next
 
'Compare the two worksheets
ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, False)
If ret Then
    MsgBox "The two worksheets are identical"
End If
 
'Change the values in one sheet
SetCellValue excelSheet1, 1, 1, "Yellow"
SetCellValue excelSheet2, 2, 2, "Hello"
 
'Compare the worksheets again
ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, True)
If Not ret Then
    MsgBox "The two worksheets are not identical"
End If
 
'save the workbook by index identifier
SaveWorkbook ExcelApp, 1, ""
 
'Close the Excel application
CloseExcel ExcelApp
 
' ****************************************** Function Library ***********************************************************
 
Dim ExcelApp 'As Excel.Application
Dim excelSheet 'As Excel.worksheet
Dim excelBook 'As Excel.workbook
Dim fso 'As Scripting.FileSystemObject
 
' This function will return a new Excel Object with a default new Workbook
Function CreateExcel() 'As Excel.Application
    Dim excelSheet 'As Excel.worksheet
    Set ExcelApp = CreateObject("Excel.Application") 'Create a new excel Object
    ExcelApp.Workbooks.Add
    ExcelApp.Visible = True
    Set CreateExcel = ExcelApp
End Function
 
'This function will close the given Excel Object
'excelApp - an Excel application object to be closed
Sub CloseExcel(ExcelApp)
    Set excelSheet = ExcelApp.ActiveSheet
    Set excelBook = ExcelApp.ActiveWorkbook
    Set fso = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    fso.CreateFolder "C:/Temp"
    fso.DeleteFile "C:/Temp/ExcelExamples.xls"
    excelBook.SaveAs "C:/Temp/ExcelExamples.xls"
    ExcelApp.Quit
    Set ExcelApp = Nothing
    Set fso = Nothing
    Err = 0
    On Error GoTo 0
End Sub
 
'The SaveWorkbook method will save a workbook according to the workbookIdentifier
'The method will overwrite the previously saved file under the given path
'excelApp - a reference to the Excel Application
'workbookIdentifier - The name or number of the requested workbook
'path - the location to which the workbook should be saved
'Return "OK" on success and "Bad Workbook Identifier" on failure
Function SaveWorkbook(ExcelApp, workbookIdentifier, path) 'As String
    Dim workbook 'As Excel.workbook
    On Error Resume Next
    Set workbook = ExcelApp.Workbooks(workbookIdentifier)
    On Error GoTo 0
    If Not workbook Is Nothing Then
        If path = "" Or path = workbook.FullName Or path = workbook.Name Then
            workbook.Save
        Else
            Set fso = CreateObject("Scripting.FileSystemObject")
 
            'if the path has no file extension then add the 'xls' extension
            If InStr(path, ".") = 0 Then
                path = path & ".xls"
            End If
 
            On Error Resume Next
            fso.DeleteFile path
            Set fso = Nothing
            Err = 0
           On Error GoTo 0
            workbook.SaveAs path
        End If
        SaveWorkbook = "OK"
    Else
        SaveWorkbook = "Bad Workbook Identifier"
    End If
End Function
 
'The SetCellValue method sets the given 'value' in the cell which is identified by
'its row column and parent Excel sheet
'excelSheet - the excel sheet that is the parent of the requested cell
'row - the cell's row in the excelSheet
'column - the cell's column in the excelSheet
'value - the value to be set in the cell
Sub SetCellValue(excelSheet, row, column, value)
    On Error Resume Next
    excelSheet.Cells(row, column) = value
    On Error GoTo 0
End Sub
 
'The GetCellValue returns the cell's value according to its row column and sheet
'excelSheet - the Excel Sheet in which the cell exists
'row - the cell's row
'column - the cell's column
'return 0 if the cell could not be found
Function GetCellValue(excelSheet, row, column)
    value = 0
    Err = 0
    On Error Resume Next
    tempValue = excelSheet.Cells(row, column)
    If Err = 0 Then
        value = tempValue
        Err = 0
    End If
    On Error GoTo 0
    GetCellValue = value
End Function
 
'The GetSheet method returns an Excel Sheet according to the sheetIdentifier
'ExcelApp - the Excel application which is the parent of the requested sheet
'sheetIdentifier - the name or the number of the requested Excel sheet
'return Nothing on failure
Function GetSheet(ExcelApp, sheetIdentifier) 'As Excel.worksheet
    On Error Resume Next
    Set GetSheet = ExcelApp.Worksheets.Item(sheetIdentifier)
    On Error GoTo 0
End Function
 
'The InsertNewWorksheet method inserts an new worksheet into the active workbook or
'the workbook identified by the workbookIdentifier, the new worksheet will get a default
'name if the sheetName parameter is empty, otherwise the sheet will have the sheetName
'as a name.
'Return - the new sheet as an Object
'ExcelApp - the excel application object into which the new worksheet should be added
'workbookIdentifier - an optional identifier of the worksheet into which the new worksheet should be added
'sheetName - the optional name of the new worksheet.
Function InsertNewWorksheet(ExcelApp, workbookIdentifier, sheetName) 'As Excel.worksheet
    Dim workbook 'As Excel.workbook
    Dim worksheet 'As Excel.worksheet
 
    'In case that the workbookIdentifier is empty we will work on the active workbook
    If workbookIdentifier = "" Then
        Set workbook = ExcelApp.ActiveWorkbook
    Else
        On Error Resume Next
        Err = 0
        Set workbook = ExcelApp.Workbooks(workbookIdentifier)
        If Err <> 0 Then
            Set InsertNewWorksheet = Nothing
            Err = 0
            Exit Function
        End If
        On Error GoTo 0
    End If
 
    sheetCount = workbook.Sheets.Count
    workbook.Sheets.Add , sheetCount
    Set worksheet = workbook.Sheets(sheetCount + 1)
 
    'In case that the sheetName is not empty set the new sheet's name to sheetName
    If sheetName <> "" Then
        worksheet.Name = sheetName
    End If
 
    Set InsertNewWorksheet = worksheet
End Function
 
'The RenameWorksheet method renames a worksheet's name
'ExcelApp - the excel application which is the worksheet's parent
'workbookIdentifier - the worksheet's parent workbook identifier
'worksheetIdentifier - the worksheet's identifier
'sheetName - the new name for the worksheet
Function RenameWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier, sheetName) 'As String
    Dim workbook 'As Excel.workbook
    Dim worksheet 'As Excel.worksheet
    On Error Resume Next
    Err = 0
    Set workbook = ExcelApp.Workbooks(workbookIdentifier)
    If Err <> 0 Then
        RenameWorksheet = "Bad Workbook Identifier"
        Err = 0
        Exit Function
    End If
    Set worksheet = workbook.Sheets(worksheetIdentifier)
    If Err <> 0 Then
        RenameWorksheet = "Bad Worksheet Identifier"
        Err = 0
        Exit Function
    End If
    worksheet.Name = sheetName
    RenameWorksheet = "OK"
End Function
 
'The RemoveWorksheet method removes a worksheet from a workbook
'ExcelApp - the excel application which is the worksheet's parent
'workbookIdentifier - the worksheet's parent workbook identifier
'worksheetIdentifier - the worksheet's identifier
Function RemoveWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier) 'As String
    Dim workbook 'As Excel.workbook
    Dim worksheet 'As Excel.worksheet
    On Error Resume Next
    Err = 0
    Set workbook = ExcelApp.Workbooks(workbookIdentifier)
    If Err <> 0 Then
        RemoveWorksheet = "Bad Workbook Identifier"
        Exit Function
    End If
    Set worksheet = workbook.Sheets(worksheetIdentifier)
    If Err <> 0 Then
        RemoveWorksheet = "Bad Worksheet Identifier"
        Exit Function
    End If
    worksheet.Delete
    RemoveWorksheet = "OK"
End Function
 
'The CreateNewWorkbook method creates a new workbook in the excel application
'ExcelApp - the Excel application to which an new Excel workbook will be added
Function CreateNewWorkbook(ExcelApp)
    Set NewWorkbook = ExcelApp.Workbooks.Add()
    Set CreateNewWorkbook = NewWorkbook
End Function
 
'The OpenWorkbook method opens a previously saved Excel workbook and adds it to the Application
'excelApp - the Excel Application the workbook will be added to
'path - the path of the workbook that will be opened
'return Nothing on failure
Function OpenWorkbook(ExcelApp, path)
    On Error Resume Next
    Set NewWorkbook = ExcelApp.Workbooks.Open(path)
    Set OpenWorkbook = NewWorkbook
    On Error GoTo 0
End Function
 
'The ActivateWorkbook method sets one of the workbooks in the application as Active workbook
'ExcelApp - the workbook's parent excel Application
'workbookIdentifier - the name or the number of the workbook
Sub ActivateWorkbook(ExcelApp, workbookIdentifier)
    On Error Resume Next
    ExcelApp.Workbooks(workbookIdentifier).Activate
    On Error GoTo 0
End Sub
 
'The CloseWorkbook method closes an open workbook
'ExcelApp - the parent Excel application of the workbook
'workbookIdentifier - the name or the number of the workbook
Sub CloseWorkbook(ExcelApp, workbookIdentifier)
    On Error Resume Next
    ExcelApp.Workbooks(workbookIdentifier).Close
    On Error GoTo 0
End Sub
 
'The CompareSheets method compares between two sheets.
'if there is a difference between the two sheets then the value in the second sheet
'will be changed to red and contain the string:
'"Compare conflict - Value was 'Value2', Expected value is 'value2'"
'sheet1, sheet2 - the excel sheets to be compared
'startColumn - the column to start comparing in the two sheets
'numberOfColumns - the number of columns to be compared
'startRow - the row to start comparing in the two sheets
'numberOfRows - the number of rows to be compared
Function CompareSheets(sheet1, sheet2, startColumn, numberOfColumns, startRow, numberOfRows, trimed) 'As Boolean
    Dim returnVal 'As Boolean
    returnVal = True
 
    'In case that one of the sheets doesn't exists, don't continue the process
    If sheet1 Is Nothing Or sheet2 Is Nothing Then
        CompareSheets = False
        Exit Function
    End If
 
    'loop through the table and fill values into the two worksheets
    For r = startRow to (startRow + (numberOfRows - 1))
        For c = startColumn to (startColumn + (numberOfColumns - 1))
            Value1 = sheet1.Cells(r, c)
            Value2 = sheet2.Cells(r, c)
 
            'if 'trimed' equels True then used would like to ignore blank spaces
            If trimed Then
                Value1 = Trim(Value1)
                Value2 = Trim(Value2)
            End If
 
            'in case that the values of a cell are not equel in the two worksheets
            'create an indicator that the values are not equel and set return value
            'to False
            If Value1 <> Value2 Then
                Dim cell 'As Excel.Range
                sheet2.Cells(r, c) = "Compare conflict - Value was '" & Value2 & "', Expected value is '" & Value1 & "'."
                Set cell = sheet2.Cells(r, c)
                cell.Font.Color = vbRed
                returnVal = False
            End If
        Next
    Next
    CompareSheets = returnVal
End Function
 
qtp excel操作 qtp excel操作,------------------------------------- 立即下载

相关推荐

DDR3性能优化实战:如何用Xilinx MIG控制器实现800MHz稳定读写(7系列FPGA版)

本文深入探讨了在Xilinx 7系列FPGA上使用MIG控制器实现DDR3存储器800MHz稳定高速读写的实战优化技巧。文章聚焦于高级时序调优、Bank Machine调度策略、用户接口设计优化以及利用ILA进行信号完整性调试,旨在帮助工程师突破性能瓶颈,实现接近理论峰值的稳定带宽,并为进一步冲刺更高频率提供进阶调参指南。

seed的博客 775

qt如何操作Excel文件

在上述示例中,我们使用QAxObject类创建了一个Excel应用程序对象,并打开了一个Excel文件。最后,我们保存并关闭工作簿,并退出Excel应用程序。QAxObject是Qt的一个类,用于与ActiveX对象进行交互,因此可以使用它来与Excel进行通信。QAxObject是Qt的一个类,用于与ActiveX对象进行交互,因此可以使用它来与Excel进行通信。请注意,在使用QAxObject类之前,您需要在Qt项目的.pro文件中添加QT += axcontainer,以启用ActiveX支持。

zhangzhechun的专栏 2170

汽车氛围灯 PCBA 方案开发:从技术底层解析车载光效的核心逻辑

摘要: 车载氛围灯PCBA方案开发需兼顾性能与车规要求。主控芯片选型应关注运算能力、温度适应性和低功耗(如NXPS32K344、复旦微FM33FG065A)。电路防护需采用TVS二极管等组合设计,满足ISO7637-2浪涌防护标准。通信协议分层应用(CAN+LIN),调光需解决色准与温漂问题,动态灯效通过FIFO队列实现。开发中需进行温度循环、振动、EMC等可靠性测试,细节处理(如三防涂覆)至关重要。该领域开发需熟悉车规标准并积累实车经验。

2401_87454052的博客 1132

QT之Excel表格操作

打开Excel文件,读取文件中数据。

平凡的人,平凡的路,平凡的人生 4030

深入解析uncompyle6反编译pyc文件及常见错误修复指南

本文深入解析了使用uncompyle6工具反编译Python字节码文件(.pyc)的完整流程。针对实践中最高频的KeyError和Python版本范围限制两大错误,提供了详细的修复方案与步骤指南,并分享了处理复杂反编译结果的高级技巧与安全实践,是进行代码恢复、调试与分析的重要参考。

uber9的博客 534

Java 开发 修改【冒险岛 079 私服】完整学习流程

第 1-2 天:环境搭建 + 运行原版 079 服务端,能正常登录游戏;第 3-4 天:纯配置文件修改,掌握倍率、属性、怪物、道具的修改,改出第一个「百倍经验私服」;第 5-10 天:Java 代码基础修改,掌握 Player/Skill/Monster 核心类的修改,实现技能无冷却、一刀秒怪、升级满属性;第 11-20 天:Java 代码进阶修改,掌握业务逻辑修改,实现死亡不掉经验、自动捡物、装备强化无失败;第 21-30 天。

萧曳丶 3888

QT笔记——操作Execl

在.pro文件中添加 QT + = axcontainer 头文件:#include <QAxObject> 主要函数: dynamicCall();//实现跨QT到COM的函数调用 setProperty();//设置属性 querySubObject();//实现对象关联或对象转换 1:我们需要连接与Execl应用程序 //连接Excel控件 QAxObject* excel = new QAxObject(this); excel->setControl("Excel.Appli

lion_cxq的博客 998

Linux系统监控实战:夜莺监控+categraf采集器配置全流程(附常见问题排查)

本文详细介绍了在Linux系统中使用夜莺监控和categraf采集器进行系统监控的全流程配置,包括环境准备、核心配置文件(如config.toml和procstat.toml)详解、服务集成与验证、高级监控配置实战以及常见问题排查。通过实战案例和优化建议,帮助运维人员快速构建高效的监控体系。

xray4的博客 735

QT :QAxObject操作Excel

QT操作Excel 参考链接:Qt-excel文件操作方法 - Coologic - 博客园

dongyoubin的博客 673

Java与OSG的梦幻联动:深入编译OpenSceneGraph的Java JNI桥梁,开启3D视觉盛宴的奇幻之旅

🔥关注墨瑾轩,带你探索编程的奥秘!🚀🔥超萌技术攻略,轻松晋级编程高手🚀🔥技术宝库已备好,就等你来挖掘🚀🔥订阅墨瑾轩,智趣学习不孤单🚀🔥即刻启航,编程之旅更有趣🚀在编译OpenSceneGraph (OSG) 的Java版本时,首先需要了解OSG本身是一个主要用于3D图形和视觉仿真的开源库,它主要支持C++。然而,OSG 也支持通过JNI(Java Native Interface)与Java集成,这允许Java程序调用OSG的C++库。

java专栏 869

万字长文介绍ARINC 653,以及在综合模块化航空电子设备(IMA)中的作用

ARINC 653 在综合模块化航空电子设备(IMA)的发展进程中扮演着极为关键的角色。从行业应用的实际情况来看,空客 A380 和波音 787 等先进飞机的航电系统套件充分体现了行业对 IMA 的高度重视和积极投入。在 IMA 架构下,核心软件具备强大的能力,能够支持多个航空电子应用程序的同时运行。这些应用程序可以并行开发,并高效地部署在同一个计算平台上,极大地提高了开发效率和系统集成度。ARINC 653 的出现为航空电子行业创造了一个充满活力和竞争力的商业环境。

2956

QTP对Excel的操作函数

<br />Class ExcelApp<br />   Private ExcelApp<br />   Private ExcelBook<br />   Private ExcelSheet <br />'打开指定的EXCEL文件<br />Function OpenExcel(excelFile)<br />    If Execl_CheckFileExist(excelFile)=1 Then<br />       Set ExcelApp = CreateObject("Excel.Appl

邹苏玉的专栏 832

Python爬虫实战:社交媒体热点事件追踪与情感分析可视化

本文探讨了如何利用Python爬虫技术从微博和Twitter等社交媒体平台抓取数据,进行热点事件追踪和情感分析,并通过可视化方式展示舆论情感变化。文章详细介绍了最新的爬虫技术、数据处理方法和可视化实现,提供了完整的代码示例,帮助读者构建自己的社交媒体分析系统。关键词:Python爬虫、社交媒体分析、情感分析、数据可视化、热点追踪爬虫框架:Requests-HTML、aiohttp、Twint(Twitter专用)数据处理文本处理:Jieba(中文分词)、NLTK(英文处理)情感分析。

2201_76125261的博客 1104

QTP对EXCEL操作函数

' ********************************************************************************************* '''''''''参数: ReportExcelFile 报告输出的路径,每次QTP运行后的测试结果路径 ''''''调用方法: '' Repor...

tengzejun508的博客 289

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

QTP操作Excel常用函数

对excel对象的操作,一般有打开文件、读取文件、修改文件、保存文件、关闭excel对象,以及对excel文件中sheet的新增、重命名、删除等操作,以下是这些常用功能实现的一些函数。 Dim ExcellApp 'As Excel.Application Dim excelSheet1 'As Excel.worksheet Dim excelSheet2 'As Excel.worksh...

mmdev 137

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

QTP的那些事--操作excel的函数

  1: QTP Excel函数 操作EXCEL 数据表格 表单 编辑EXCEL 工作表 2: Dim ExcelApp 'As Excel.Application 3: Dim excelSheet 'As Excel.worksheet 4: Dim excelBook 'As Excel.workbook 5: Dim fso ...

weixin_34402408的博客 108

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

QTP之excel操作函数整理

*****************************************************   'Function:读Excel中的某个值 'Input parameter:  'strFilePath:保存Excel的文件路径  'strExcelSheetName:要读取的Excel中Sheet的名称  'intRow:读取哪一行的数据  'intCol:读取哪一列

lmxr133的博客 1739

工作中常用的QTP操作Excel函数

前言 本文只是对工作中常用的EOM相关函数的整理,并不是要写个大而全的操作手册,如果想对EOM有更多的了解可以参考QTP的帮助文档或查看QTP安装目录\CodeSamplesPlus\UsingExcel.vbs文件。 自动化测试中,为了达到数据与脚本分离的目的,我们需要一种能够统一管理测试数据的工具。QTP自带的DataTable非常好用,但是不利于管理和维护,这时候Excel就进入了我们的...

weixin_30677073的博客 245

【QTP】读取Excel表的用法

这个是个非常非常常用且实用的函数,使用QTP读取Excel中的数据。 先贴上函数: '======================================== '名 称:QTP_ReadExcel '作 用:从Excel读数据 '参 数:sFileName:EXCEL文件名 sSheetName:Sheet表名 '返回值:把EXCEL变为2维数组返回

Dylan Chen 1592
上一篇: QTP9.5 Web Extensibility WebEvent视频教程截图
下一篇: QTP的未来
TIB
TIB
博客等级 码龄19年 2879粉丝 957原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值