• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    用vbs读取Excel文件的函数代码
    核心代码
    复制代码 代码如下:

    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
    您可能感兴趣的文章:
    • C#数据导入/导出Excel文件及winForm导出Execl总结
    • php读取EXCEL文件 php excelreader读取excel文件
    • Asp.net生成Excel文件并下载(更新:解决使用迅雷下载页面而不是文件的问题)
    • PHPExcel读取Excel文件的实现代码
    • 将datagrid控件内容输出到excel文件
    • .NET中读取Excel文件的数据及excelReader应用
    上一篇:网络接口利用率监控VBS脚本代码(Windows)
    下一篇:从XML文件中获取信息的vbs代码
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

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