醉里乾坤大,壶中日月长

封了一段EXCLE的简单代码

上一篇 / 下一篇  2009-11-11 21:40:50

Class Excel
    Private sExcelHandle,sWorkbookHandle,sWorksheetHandle
    
    Sub Class_Initialize
        '类的初始化,生成一个Excel Application对象,并将句柄保存在sExcelHandle
        Set sExcelHandle = CreateObject("excel.Application")
        sExcelHandle.Visible = True
    End Sub
   
    Function OpenExcel(Byval sExcelPath)
        Set sWorkbookHandle = sExcelHandle.Workbooks.Open(sExcelPath)
    End Function
   
    Function SelectSheet(Byval sSheetName)
        'Set sSheetHandle = sExcelHandle.Worksheets(sSheetName).Activate
        Set sSheetHandle = sExcelHandle.Worksheets(sSheetName)
        sSheetHandle.Activate
    End Function
   
    Function GetCellValue(Byval iRowNum,Byval iColNum)
        GetCellValue = sExcelHandle.Cells(iRowNum,iColNum)
    End Function
   
    Sub SetCellValue(Byval iRowNum,Byval iColNum,Byval sCellValue)
        sExcelHandle.Cells(iRowNum,iColNum) = sCellValue
    End Sub
   
    Sub closeExcel()
        sWorkbookHandle.Save
        sWorkbookHandle.Close
        sExcelHandle.Quit
        Set sSheetHandle = Nothing
        Set sWorkbookHandle = Nothing
        Set sExcelHandle = Nothing
    End Sub
End Class

Set Excel = New Excel
oExcel.OpenExcel("R:\test\Case.xlsx")
oExcel.SelectSheet("Pkg_download")
MsgBox oExcel.GetCellValue(1,1)
oExcel.SetCellValue 1,2,"True"
oExcel.closeExcel()
Set Excel = Nothing

TAG:

 

评分:0

我来说两句

Open Toolbar