国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > VBScript > 正文

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

2020-06-26 18:28:56
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
用vbs讀取Excel文件的函數(shù)代碼,不需要安裝execl,需要的朋友可以參考下。
 
核心代碼 
復(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 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 比如县| 建水县| 沂水县| 金阳县| 武汉市| 铜鼓县| 高唐县| 聂荣县| 安国市| 大同县| 武城县| 赤水市| 顺平县| 贵溪市| 安平县| 满洲里市| 安塞县| 揭东县| 荃湾区| 祁东县| 镇远县| 花莲市| 阳新县| 陆良县| 敦化市| 若羌县| 富阳市| 揭西县| 正阳县| 乌苏市| 辽宁省| 宿州市| 丰城市| 宿迁市| 梨树县| 临潭县| 宁河县| 阳泉市| 石林| 安顺市| 清原|