用vbs讀取Excel文件的函數(shù)代碼
來(lái)源:易賢網(wǎng) 閱讀:1696 次 日期:2016-07-05 14:37:49
溫馨提示:易賢網(wǎng)小編為您整理了“用vbs讀取Excel文件的函數(shù)代碼”,方便廣大網(wǎng)友查閱!

用vbs讀取Excel文件的函數(shù)代碼,不需要安裝execl,需要的朋友可以參考下。

核心代碼

代碼如下:

Function ReadExcel( myXlsFile, mySheet, my1stCell, myLastCell, blnHeader )

' Function : ReadExcel

' Version : 2.00

' This function reads data from an Excel sheet without using MS-Office

'

' Arguments:

' myXlsFile [string] The path and file name of the Excel file

' mySheet [string] The name of the worksheet used (e.g. "Sheet1")

' my1stCell [string] The index of the first cell to be read (e.g. "A1")

' myLastCell [string] The index of the last cell to be read (e.g. "D100")

' blnHeader [boolean] True if the first row in the sheet is a header

'

' Returns:

' The values read from the Excel sheet are returned in a two-dimensional

' array; the first dimension holds the columns, the second dimension holds

' the rows read from the Excel sheet.

'

' Written by Rob van der Woude

' http://www.robvanderwoude.com

Dim arrData( ), i, j

Dim objExcel, objRS

Dim strHeader, strRange

Const adOpenForwardOnly = 0

Const adOpenKeyset = 1

Const adOpenDynamic = 2

Const adOpenStatic = 3

' Define header parameter string for Excel object

If blnHeader Then

strHeader = "HDR=YES;"

Else

strHeader = "HDR=NO;"

End If

' Open the object for the Excel file

Set objExcel = CreateObject( "ADODB.Connection" )

' IMEX=1 includes cell content of any format; tip by Thomas Willig

objExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _

myXlsFile & ";Extended Properties=""Excel 8.0;IMEX=1;" & _

strHeader & """"

' Open a recordset object for the sheet and range

Set objRS = CreateObject( "ADODB.Recordset" )

strRange = mySheet & "$" & my1stCell & ":" & myLastCell

objRS.Open "Select * from [" & strRange & "]", objExcel, adOpenStatic

' Read the data from the Excel sheet

i = 0

Do Until objRS.EOF

' Stop reading when an empty row is encountered in the Excel sheet

If IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do

' Add a new row to the output array

ReDim Preserve arrData( objRS.Fields.Count - 1, i )

' Copy the Excel sheet's row values to the array "row"

' IsNull test credits: Adriaan Westra

For j = 0 To objRS.Fields.Count - 1

If IsNull( objRS.Fields(j).Value ) Then

arrData( j, i ) = ""

Else

arrData( j, i ) = Trim( objRS.Fields(j).Value )

End If

Next

' Move to the next row

objRS.MoveNext

' Increment the array "row" number

i = i + 1

Loop

' Close the file and release the objects

objRS.Close

objExcel.Close

Set objRS = Nothing

Set objExcel = Nothing

' Return the results

ReadExcel = arrData

End Function

使用方法:

代碼如下:

Option Explicit

Dim arrSheet, intCount

' Read and display columns A,B, rows 2..6 of "ReadExcelTest.xls"

arrSheet = ReadExcel( "ReadExcelTest.xls", "Sheet1", "A1", "B6", True )

For intCount = 0 To UBound( arrSheet, 2 )

WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )

Next

WScript.Echo "==============="

' An alternative way to get the same results

arrSheet = ReadExcel( "ReadExcelTest.xls", "Sheet1", "A2", "B6", False )

For intCount = 0 To UBound( arrSheet, 2 )

WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )

Next

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:用vbs讀取Excel文件的函數(shù)代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)