Board logo

标题: [转贴] VBScript脚本读取Excel文件的函数 [打印本页]

作者: find    时间: 2012-2-17 12:54     标题: VBScript脚本读取Excel文件的函数

用vbs读取Excel文件的函数代码,不需要安装execl,需要的朋友可以参考下。

核心代码

  1. Function ReadExcel( myXlsFile, mySheet, my1stCell, myLastCell, blnHeader )
  2. ' Function : ReadExcel
  3. ' Version : 2.00
  4. ' This function reads data from an Excel sheet without using MS-Office
  5. '
  6. ' Arguments:
  7. ' myXlsFile [string] The path and file name of the Excel file
  8. ' mySheet [string] The name of the worksheet used (e.g. "Sheet1")
  9. ' my1stCell [string] The index of the first cell to be read (e.g. "A1")
  10. ' myLastCell [string] The index of the last cell to be read (e.g. "D100")
  11. ' blnHeader [boolean] True if the first row in the sheet is a header
  12. '
  13. ' Returns:
  14. ' The values read from the Excel sheet are returned in a two-dimensional
  15. ' array; the first dimension holds the columns, the second dimension holds
  16. ' the rows read from the Excel sheet.
  17. '
  18. ' Written by Rob van der Woude
  19. ' http://www.robvanderwoude.com
  20. Dim arrData( ), i, j
  21. Dim objExcel, objRS
  22. Dim strHeader, strRange
  23. Const adOpenForwardOnly = 0
  24. Const adOpenKeyset = 1
  25. Const adOpenDynamic = 2
  26. Const adOpenStatic = 3
  27. ' Define header parameter string for Excel object
  28. If blnHeader Then
  29. strHeader = "HDR=YES;"
  30. Else
  31. strHeader = "HDR=NO;"
  32. End If
  33. ' Open the object for the Excel file
  34. Set objExcel = CreateObject( "ADODB.Connection" )
  35. ' IMEX=1 includes cell content of any format; tip by Thomas Willig
  36. objExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
  37. myXlsFile & ";Extended Properties=""Excel 8.0;IMEX=1;" & _
  38. strHeader & """"
  39. ' Open a recordset object for the sheet and range
  40. Set objRS = CreateObject( "ADODB.Recordset" )
  41. strRange = mySheet & "$" & my1stCell & ":" & myLastCell
  42. objRS.Open "Select * from [" & strRange & "]", objExcel, adOpenStatic
  43. ' Read the data from the Excel sheet
  44. i = 0
  45. Do Until objRS.EOF
  46. ' Stop reading when an empty row is encountered in the Excel sheet
  47. If IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do
  48. ' Add a new row to the output array
  49. ReDim Preserve arrData( objRS.Fields.Count - 1, i )
  50. ' Copy the Excel sheet's row values to the array "row"
  51. ' IsNull test credits: Adriaan Westra
  52. For j = 0 To objRS.Fields.Count - 1
  53. If IsNull( objRS.Fields(j).Value ) Then
  54. arrData( j, i ) = ""
  55. Else
  56. arrData( j, i ) = Trim( objRS.Fields(j).Value )
  57. End If
  58. Next
  59. ' Move to the next row
  60. objRS.MoveNext
  61. ' Increment the array "row" number
  62. i = i + 1
  63. Loop
  64. ' Close the file and release the objects
  65. objRS.Close
  66. objExcel.Close
  67. Set objRS = Nothing
  68. Set objExcel = Nothing
  69. ' Return the results
  70. ReadExcel = arrData
  71. End Function
复制代码


使用方法:

  1. Option Explicit
  2. Dim arrSheet, intCount
  3. ' Read and display columns A,B, rows 2..6 of "ReadExcelTest.xls"
  4. arrSheet = ReadExcel( "ReadExcelTest.xls", "Sheet1", "A1", "B6", True )
  5. For intCount = 0 To UBound( arrSheet, 2 )
  6. WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )
  7. Next
  8. WScript.Echo "==============="
  9. ' An alternative way to get the same results
  10. arrSheet = ReadExcel( "ReadExcelTest.xls", "Sheet1", "A2", "B6", False )
  11. For intCount = 0 To UBound( arrSheet, 2 )
  12. WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )
  13. Next
复制代码

转自:http://www.robvanderwoude.com




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2