Excel Operation

上一篇 / 下一篇  2009-05-12 20:47:45 / 个人分类:VBScript & QTP

Class ExcelOperation

private ExcelApp    'As Excel.Application
private  excelSheet  'As Excel.worksheet
private excelBook   'As Excel.workbook


' *********************************************************************************************
' 函数说明:打开工作
' 参数说明:
'          (1)ExcelApp:Excel应用程序名称;
'          (2)path:要打开的工作薄路径;
' 返回结果:
'          (1)成功:工作表对象NewWorkbook
'          (1)失败:Nothing
' 调用方法:
'          set NewWorkbook = CreateNewWorkbook(ExcelApp)
' *********************************************************************************************
Public Function OpenWorkbook(path, sheetName)
    On Error Resume Next

    Set ExcelApp = CreateObject("Excel.Application")
    ExcelApp.Visible = True
    Set CreateExcel = ExcelApp
   
    ExcelApp.Workbooks.Open(path)
    Set excelBook = ExcelApp.Workbooks(1)
    excelBook.WorkSheets(sheetName).Activate
    Set excelSheet = excelBook.worksheets(sheetName)
    On Error GoTo 0
End Function

'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
' *********************************************************************************************
' 函数说明:获取工作表excelSheet单元格的值
' 参数说明:
'          (1)excelSheet:工作表名称;
'          (2)row:列的序号;
'          (3)column:行的序号;
' 返回结果:
'          (1)单元格存在,返回单元格值;
'          (2)单元格不存在,返回0;
' 调用方法:
'           set CellValue = GetCellValue(excelSheet, 1, 2)
' *********************************************************************************************
Public Function GetCellValue(row, column)
    value = 0
    Err = 0
    On Error Resume Next
    tempValue = excelSheet.Cells(row, column).Value
    If Err = 0 Then
        value = tempValue
        Err = 0
    End If
    On Error GoTo 0
    GetCellValue = value
End Function


Private Sub Class_Terminate
   Call CloseExcel()
End Sub

' *********************************************************************************************
' 函数说明:关闭Excel应用程序;
' 参数说明:
'          (1)ExcelApp:Excel应用程序名称;
' 调用方法:
'           CloseExcel(ExcelApp)
' *********************************************************************************************
Private Function CloseExcel()
    On Error Resume Next
    ExcelApp.Quit
    Set ExcelApp = Nothing
    On Error GoTo 0
End Function

End Class


TAG:

 

评分:0

我来说两句

日历

« 2024-04-26  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 13881
  • 日志数: 28
  • 书签数: 1
  • 建立时间: 2009-04-02
  • 更新时间: 2010-06-09

RSS订阅

Open Toolbar