vbs讀取excel的一個實例


功能:在excel中對ip與loginType這兩列進行遍歷讀取。本程序依賴於excel文件的"sheet2"表單中具有這兩列。

dim dictTarget, objExcel
'從交換機台賬中提取ip/loginType數據
getDictTarget
objExcel.Quit

'Msgbox dictTarget.Count
'遍歷所有ip,進行登錄
keySet = dictTarget.keys
for i = 0 to dictTarget.Count -1
	ip = keySet(i)
	loginType = dictTarget.Item(ip)
	Msgbox ip&" : "&loginType
next

function getDictTarget
'功能:打開excel並將"ip","登錄方式"這兩列值提取為dictionary
	Set dictTarget = CreateObject("Scripting.Dictionary")
	Set objExcel = CreateObject("Excel.Application")
	Set objWorkbook = objExcel.Workbooks.Open("D:\桌面\Desktop\1.xlsx")
	objExcel.WorkSheets("Sheet2").Activate
	intColOfIP = getColNumInRowByValue(1, "IP")
	'Msgbox "intColOfIP : "&intColOfIP
	intColOfLoginType = getColNumInRowByValue(1, "登錄方式")
	'Msgbox "intColOfLoginType : "&intColOfLoginType
	
	intRow = 2
	Do Until objExcel.Cells(intRow, intColOfIP).Value = ""
		ip = objExcel.Cells(intRow, 1).Value
		loginType = objExcel.Cells(intRow, 2).Value
		dictTarget.add ip, loginType
	intRow = intRow + 1
	Loop
end function

function getColNumInRowByValue(intRow, strValue)
'獲取指定行中指定內容的單元格的列值
	intCol = 1
	intResult = -1
	value = "tmp"
	Do while value <> ""
		value = objExcel.Cells(intRow, intCol).Value
		if value = strValue then
			intResult = intCol
			exit do
		end if
		intCol = intCol + 1		
	Loop
	getColNumInRowByValue = intResult
	'Msgbox "intResult : "&intResult
end function

問題:在win7中測試一切正常。然而在xp中測試發現,運行結束后任務管理器中會殘留一個EXCEL.EXE無法退出,即使已經調用了“objExcel.Quit”語句。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM