ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:17.06KB ,
资源ID:23749022      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/23749022.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(QTP操作Excel.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

QTP操作Excel.docx

1、QTP操作ExcelQTP操作ExcelQTPExcel函数 操作EXCEL 数据表格 表单 编辑EXCEL工作表Dim ExcelApp As Excel.ApplicationDim excelSheet As Excel.worksheetDim excelBook As Excel.workbookDim fso As scrpting.FileSystemObject * 函数说明:创建一个Excel应用程序ExcelApp,并创建一个新的工作薄Workbook; 参数说明:无 调用方法: CreateExcel() *Function CreateExcel() Dim excel

2、Sheet Set ExcelApp = CreateObject(Excel.Application) ExcelApp.Workbooks.Add ExcelApp.Visible = True Set CreateExcel = ExcelAppEnd Function * 函数说明:关闭Excel应用程序; 参数说明: (1)ExcelApp:Excel应用程序名称; 调用方法: CloseExcel(ExcelApp) *Sub CloseExcel(ExcelApp) Set excelSheet = ExcelApp.ActiveSheet Set excelBook = Exc

3、elApp.ActiveWorkbook Set fso = CreateObject(scrpting.FileSystemObject) On Error Resume Next fso.CreateFolder C:Temp fso.DeleteFile C:TempExcelExamples.xls excelBook.SaveAs C:TempExcelExamples.xls ExcelApp.Quit Set ExcelApp = Nothing Set fso = Nothing Err = 0 On Error GoTo 0End Sub * 函数说明:保存工作薄; 参数说明

4、: (1)ExcelApp:Excel应用程序名称; (2)workbookIdentifier:属于ExcelApp的工作薄名称; (3)path:保存的路径; 返回结果: (1)保存成功,返回字符串:OK (2)保存失败,返回字符串:Bad Worksheet Identifier 调用方法: ret = SaveWorkbook(ExcelApp, Book1, D:Example1.xls) *Function SaveWorkbook(ExcelApp, workbookIdentifier, path) As String Dim workbook On Error Resume

5、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(scrpting.FileSystemObject) 判断路径中是否已添加扩展名.xls If InStr(path, .) = 0

6、 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 IfEnd Function * 函数说明:设置工作表excelSheet单元格的值 参数说明: (1)excelSheet:工作表名称; (2)row:列

7、的序号,第一列为1; (3)column:行的序号,第一行为1; (4)value:单元格要设置的值; 返回结果: 无返回值 调用方法: SetCellValue excelSheet1, 1, 2, test *Sub SetCellValue(excelSheet, row, column, value) On Error Resume Next excelSheet.Cells(row, column) = value On Error GoTo 0End SubThe GetCellValue returns the cells value according to its row c

8、olumn and sheetexcelSheet - the Excel Sheet in which the cell existsrow - the cells rowcolumn - the cells columnreturn 0 if the cell could not be found * 函数说明:获取工作表excelSheet单元格的值 参数说明: (1)excelSheet:工作表名称; (2)row:列的序号; (3)column:行的序号; 返回结果: (1)单元格存在,返回单元格值; (2)单元格不存在,返回0; 调用方法: set CellValue = GetC

9、ellValue(excelSheet, 1, 2) *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 = valueEnd Function * 函数说明:获取并返回工作表对象 参数说明: (1)ExcelApp:Excel应用程序名

10、称; (2)sheetIdentifier:属于ExcelApp的工作表名称; 返回结果: (1)成功:工作表对象Excel.worksheet (1)失败:Nothing 调用方法: Set excelSheet1 = GetSheet(ExcelApp, Sheet Name) *Function GetSheet(ExcelApp, sheetIdentifier) On Error Resume Next Set GetSheet = ExcelApp.Worksheets.Item(sheetIdentifier) On Error GoTo 0End Function * 函数说明

11、:添加一张新的工作表 参数说明: (1)ExcelApp:Excel应用程序名称; (2)workbookIdentifier:属于ExcelApp的工作薄名称; (2)sheetName:要插入的工作表名称; 返回结果: (1)成功:工作表对象worksheet (1)失败:Nothing 调用方法: InsertNewWorksheet(ExcelApp, workbookIdentifier, new sheet) *Function InsertNewWorksheet(ExcelApp, workbookIdentifier, sheetName) Dim workbook As E

12、xcel.workbook Dim worksheet As Excel.worksheet 如果指定的工作薄不存在,将在当前激活状态的工作表中添加工作表 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 Functi

13、on End If On Error GoTo 0 End If sheetCount = workbook.Sheets.Count 获取工作薄中工作表的数量 workbook.Sheets.Add , sheetCount 添加工作表 Set worksheet = workbook.Sheets(sheetCount + 1) 初始化worksheet为新添加的工作表对象 设置新添加的工作表名称 If sheetName Then worksheet.Name = sheetName End If Set InsertNewWorksheet = worksheetEnd Functio

14、n * 函数说明:修改工作表的名称; 参数说明: (1)ExcelApp:Excel应用程序名称; (2)workbookIdentifier:属于ExcelApp的工作薄名称; (3)worksheetIdentifier:属于workbookIdentifier工作薄的工作表名称; (4)sheetName:修改后的工作表名称; 返回结果: (1)修改成功,返回字符串:OK (2)修改失败,返回字符串:Bad Worksheet Identifier 调用方法: set ret = RenameWorksheet(ExcelApp, Book1, Sheet1, Sheet Name) *

15、Function RenameWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier, sheetName) Dim workbook Dim 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 = wo

16、rkbook.Sheets(worksheetIdentifier) If Err 0 Then RenameWorksheet = Bad Worksheet Identifier Err = 0 Exit Function End If worksheet.Name = sheetName RenameWorksheet = OKEnd Function * 函数说明:删除工作表; 参数说明: (1)ExcelApp:Excel应用程序名称; (2)workbookIdentifier:属于ExcelApp的工作薄名称; (3)worksheetIdentifier:属于workbookI

17、dentifier工作薄的工作表名称; 返回结果: (1)删除成功,返回字符串:OK (2)删除失败,返回字符串:Bad Worksheet Identifier 调用方法: set ret = RemoveWorksheet(ExcelApp, Book1, Sheet1) *Function RemoveWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier) Dim workbook As Excel.workbook Dim worksheet As Excel.worksheet On Error Resume Next

18、 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 RemoveWorksh

19、eet = OKEnd Function * 函数说明:添加新的工作薄 参数说明: (1)ExcelApp:Excel应用程序名称; 返回结果: (1)成功:工作表对象NewWorkbook (1)失败:Nothing 调用方法: set NewWorkbook = CreateNewWorkbook(ExcelApp) *Function CreateNewWorkbook(ExcelApp) Set NewWorkbook = ExcelApp.Workbooks.Add() Set CreateNewWorkbook = NewWorkbookEnd Function * 函数说明:打开

20、工作薄 参数说明: (1)ExcelApp:Excel应用程序名称; (2)path:要打开的工作薄路径; 返回结果: (1)成功:工作表对象NewWorkbook (1)失败:Nothing 调用方法: set NewWorkbook = CreateNewWorkbook(ExcelApp) *Function OpenWorkbook(ExcelApp, path) On Error Resume Next Set NewWorkbook = ExcelApp.Workbooks.Open(path) Set penWorkbook = NewWorkbook On Error GoTo 0End Function *

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1