VBS 采用单例模式来封装 操作Excel类

Option Explicit

Public oExcel  'Global Var

'''''''''''''''''''''''''''''''''''''''''''''''''''''  
'Description: 采用单例模式来封装的Excel操作类  
'Creator: Eric_1991
'Date : 2014/1/2
'''''''''''''''''''''''''''''''''''''''''''''''''''''

Class ExcelUtil
	Private oExcelObject

	Private Sub Class_Initialize
		Dim boolInit
		boolInit  = False
		Err.Clear
		On Error Resume Next
		
		'boolInit = IsObject(oExcelObject)
		If( IsObject(oExcel) = True ) Then
			If Not oExcel Is Nothing Then 
				'Active status
				boolInit = True
			End If 
		End If 
		
		If boolInit = False Then
			Set oExcel = CreateObject("Excel.Application")
			'oExcel.Visible = True 
			If(Err.Number <> 0) Then
				WSH.Echo Err.Description & " || " &Err.Number
				On Error Goto 0
			End If 
		End  If 
		
		Set oExcelObject = oExcel
				
	End Sub
	
	Private Sub Class_Terminate
		oExcelObject.Quit
		Set oExcelObject = Nothing
		WSH.Echo "close excel app"
	End Sub
	
	'open the workbook by filepath
	Public Default Function OpenWorkbook(strFilepath)
		Err.Clear
		Dim newWorkbook
		On Error Resume Next
	 
		Set newWorkbook = oExcelObject.Workbooks.Open(strFilepath)
		If(Err.Number <> 0) Then
			'WSH.Echo "the filepath is invalid"
			WSH.Echo Err.Description & " || " &Err.Number
			On Error Goto 0
		End If
		Set OpenWorkbook = newWorkbook
		Set newWorkbook = Nothing
	End Function
	
	'change the activate book by workbookname
	Public Function ActivateWorkbook(strWorkbookName)
		Err.Clear
		On Error Resume Next 
		oExcelObject.Workbooks(strWorkbookName).Activate
	End Function 
	
	'todo this action,pls to call ActivateWorkbook Method
	Public Function GetSheet(strWorkbookName, strSheetName)
		Err.Clear
		Dim worksheet
		On Error Resume Next
		'Active the target excel name
		ActivateWorkbook strWorkbookName
		Set worksheet = oExcelObject.Worksheets.Item(strSheetName)
		Set GetSheet = worksheet
		Set worksheet  = nothing
	End Function 
	
	Public Function GetSheetUsedColumnsCount(strFilePath, strFileName, strSheetName)
		Dim workbook, worksheet
		Set workbook = OpenWorkbook(strFilePath)
		Set worksheet = GetSheet(strFileName, strSheetName)
		If Err.Number <> 0 Then 
			WSH.Echo Err.Description & " || " & Err.Number
			On Error Goto 0
		End If 
		GetSheetUsedColumnsCount = worksheet.UsedRange.Columns.Count
		Set worksheet = Nothing
		Set workbook = Nothing
	End Function
	
	Function GetSheetUsedRowsCount(strFilePath, strFilename, strSheetName)
		Dim workbook, worksheet
		Set workbook = OpenWorkbook(strFilePath)
		Set worksheet = GetSheet(strFileName, strSheetName)
		GetSheetUsedRowsCount = worksheet.UsedRange.Rows.Count
		Set worksheet = Nothing
		Set workbook = Nothing
	End Function

	Private Function GetCellvalue(ByRef ExcelSheet, intRow, intColumn)
		On Error Resume Next
		GetCellvalue = ExcelSheet.Cells(intRow, intColumn)
	End Function

	Function GetSheetData2Array(strFilePath, strFileName, strSheetName)
		Dim Columnscount, RowsCount
		Columnscount = GetSheetUsedColumnsCount(strFilePath, strFileName,strSheetName)
		RowsCount = GetSheetUsedRowsCount(strFilePath, strFileName, strSheetName)
		
		Dim workbook, worksheet
		Set workbook = OpenWorkbook(strFilePath)
		
		ActivateWorkbook strFileName
		Set worksheet = GetSheet(strFileName, strSheetName)
		
		ReDim scriptItemArray(RowsCount-2,Columnscount-1)
		Dim Actual
		Dim i,j
		Dim number 

		Actual = 0
		For i=2 To RowsCount
				number = Trim(GetCellvalue(worksheet,i,1))
				If(IsEmpty(number) Or number = "" Or Not (IsNumeric(number))) Then
						'WSH.Echo number 
						Exit For 
				End If
				Actual = Actual + 1
			For j=1 To Columnscount
			    scriptItemArray(i-2,j-1) = Trim(GetCellvalue(worksheet,i,j))
				'WSH.Echo GetCellvalue(worksheet,i,j)
			Next 
		Next
		
		ReDim actualScriptItemArray(Actual-1, Columnscount-1)
	
		For i=0 To Actual-1 
			For j = 0 To Columnscount-1
				actualScriptItemArray(i,j) = scriptItemArray(i,j)
			Next
		Next
		
		Set worksheet = Nothing
		Set workbook =  Nothing
		
		GetSheetData2Array = actualScriptItemArray
		
	End Function 

End Class

Dim fso, obj1,filepath,filename
Set fso = CreateObject("scripting.filesystemobject")
Set obj1 = New ExcelUtil
filepath = "D:\dd1.xlsx"
filename = fso.GetFileName(filepath)

Dim arrDate
arrDate = obj1.GetSheetData2Array(filepath, filename, "script")

Dim oneDimension
Dim twoDimension 

oneDimension = UBound(arrDate,1)
twoDimension = UBound(arrDate,2)
Dim i,j
For i = 0 To UBound(arrDate,1)
	For j = 0 To UBound(arrDate,2)
		WSH.Echo arrDate(i,j)
	Next 
	WSH.Echo "==============================="
Next

Set obj1 = Nothing
Set fso = Nothing 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值