主頁 > 知識庫 > 用vbs讀取Excel文件的函數(shù)代碼

用vbs讀取Excel文件的函數(shù)代碼

熱門標(biāo)簽:辦理膠州400電話財稅 外呼營銷下單系統(tǒng) 加盟電銷機(jī)器人好的品牌 海外美發(fā)店地圖標(biāo)注 百度地圖標(biāo)注怎么卸載 打電話的外呼系統(tǒng)貴不貴 陜西高頻外呼回?fù)芟到y(tǒng)哪家好 前鋒辦理400電話申請 新密防封卡外呼系統(tǒng)違法嗎
核心代碼
復(fù)制代碼 代碼如下:

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

使用方法:
復(fù)制代碼 代碼如下:

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
您可能感興趣的文章:
  • C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)
  • php讀取EXCEL文件 php excelreader讀取excel文件
  • Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁面而不是文件的問題)
  • PHPExcel讀取Excel文件的實現(xiàn)代碼
  • 將datagrid控件內(nèi)容輸出到excel文件
  • .NET中讀取Excel文件的數(shù)據(jù)及excelReader應(yīng)用

標(biāo)簽:伊春 梅州 阜陽 咸陽 河南 四平 牡丹江 武威

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用vbs讀取Excel文件的函數(shù)代碼》,本文關(guān)鍵詞  用,vbs,讀取,Excel,文件,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《用vbs讀取Excel文件的函數(shù)代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于用vbs讀取Excel文件的函數(shù)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章