zoukankan      html  css  js  c++  java
  • QTP - excel操作

    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: est.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

  • 相关阅读:
    ABAPSAP的SCreen录入的简单模板
    SD--如何编程提取SD的定价信息
    如何从SAP中连接其他数据库
    许多的图书下载连接列表
    2006最新版个人所得税计算器
    如何编写上传(PC>AS)和下载(AS->PC)程序示例
    关于sap的字段和对象修改记录的查找
    添加视频播放器
    oracle中exp,imp的使用详解
    [转载]简历中的自我评价用语──珍藏版
  • 原文地址:https://www.cnblogs.com/ellie-test/p/3823500.html
Copyright © 2011-2022 走看看