用vbs读取Excel文件的函数代码_vbs

核心代码

复制代码 代码如下:

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

时间: 2024-08-04 00:16:40

用vbs读取Excel文件的函数代码_vbs的相关文章

可以读取EXCEL文件的js代码第1/2页_javascript技巧

首页给个有中文说明的例子,下面的例子很多大家可以多测试. 复制代码 代码如下: <script language="javascript" type="text/javascript"><!-- function readExcel() { var excelApp; var excelWorkBook; var excelSheet; try{ excelApp = new ActiveXObject("Excel.Applicatio

用VBS获取Unix时间戳的函数代码_vbs

VBS中没有类似C标准库中的time函数,怎么获取Unix时间戳呢?乍一看很简单: 复制代码 代码如下: Function UnixTime() UnixTime = DateDiff("s", "01/01/1970 00:00:00", Now()) End Function 一个很想当然的方法,仅仅注意到了"1970年1月1日0时0分0秒",而忽略了"协调世界时". 协调世界时,又称世界标准时间或世界协调时间,简称UTC

用VBS实现PHP的md5_file函数代码_vbs

复制代码 代码如下: Function md5_file(filename, raw_output) Dim HashedData, Utility, Stream Set HashedData = CreateObject("CAPICOM.HashedData") Set Utility = CreateObject("CAPICOM.Utilities") Set Stream = CreateObject("ADODB.Stream")

php读取excel文件(.csv)实例介绍

PHP有自带的分析.csv函数:fgetcsv array fgetcsv ( int $handle [, int $length [, string $delimiter [, string $enclosure]]] ) handle 一个由 fopen().popen() 或 fsockopen() 产生的有效文件指针. length (可选)必须大于 CVS 文件内最长的一行.在 PHP 5 中该参数是可选的.如果忽略(在 PHP 5.0.4 以后的版本中设为 0)该参数的话,那么长度

通过Javascript读取本地Excel文件内容的代码示例

 这篇文章主要介绍了通过Javascript读取本地Excel文件内容的代码示例,但需要一定的条件才可以使用js操作本地文件,需要的朋友参考下吧 读取本地Excel文件内容的Javascript代码:    代码如下: <script type="text/javascript"> function read_excel(){       var filePath="D:abcd9.com.xls"; //要读取的xls     var sheet_id

java代码-关于poi类java读取Excel文件出现ioexception异常Invalid header signature

问题描述 关于poi类java读取Excel文件出现ioexception异常Invalid header signature 读取是因为头部文件无法识别,我按一般方法是另存为就可以了,有没别的方法是通过代码实现的,我写的功能是通过jsp来上传excel到临时文件,在用poi类来读取,所以打开excel另存为的方法不适合我,求救,很急 解决方案 用poi处理excel文件异常:java.io.IOException: Invalid header signature; read 23380427

php读取EXCEL文件 php excelreader读取excel文件_php实例

php开发中肯定会遇到将excel文件内容导入到数据库的需要,php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件非常方便. php-excel-reader下载地址: http://www.jb51.net/codes/67223.html 我下载的是php-excel-reader-2.21版本,使用的时候还遇到几个小问题,后面再细说,先奉上php实例: 我使用的excel如下图: php代码如下: 复制代码 代码如下: <?php /*by www

php生成与读取excel文件_php实例

在网站中经常会生成表格,CSV和Excel都是常用的报表格式,CSV相对来说比较简单,如果大家有疑问我会相继发布一些CSV的实例,这里主要介绍用PHP来生成和读取Excel文件. 要执行下面的函数,首先要引入一个类库:PHPExcel,PHPExcel是一个强大的PHP类库,用来读写不同的文件格式,比如说Excel 2007,PDF格式,HTML格式等等,这个类库是建立在Microsoft's OpenXML和PHP 的基础上的,对Excel提供的强大的支持,比如设置工作薄,字体样式,图片以及边

PHPExcel生成和读取Excel文件实例程序

在网站的管理后台经常会使用到报表的生成和读取,CSV和Excel都是常用的报表格式,CSV相对来说比较简单,如果大家有疑问我会相继发布一些CSV的实例,这里主要介绍用PHP 来生成和读取Excel文件. 要执行下面的函数,首先要引入一个类库:PHPExcel,PHPExcel是一个强大的PHP类库,用来读写不同的文件格式,比如说Excel 2007,PDF格式,HTML格式等等,这个类库是建立在Microsoft's OpenXML和PHP 的基础上的,对Excel提供的强大的支持,比如设置工作