开发者

How to read a spreadsheet with WSH?

开发者 https://www.devze.com 2023-02-21 21:17 出处:网络
I need to read some cell values from a .xls file using JScript under WSH. Is there any COM object or anything that I can use to do tha开发者_开发技巧t?Actually there is a COM component for this. Its

I need to read some cell values from a .xls file using JScript under WSH.

Is there any COM object or anything that I can use to do tha开发者_开发技巧t?


Actually there is a COM component for this. Its progId is "Excel.Application" and you use it like this:

var XLS = WScript.CreateObject("Excel.Application") ;
XLS.Workbooks.open(xlsFile) ;
var cellValue = XLS.Cells(row,col).Value ;

That's it. As simple as that. The variable cellValue now holds the value in the cell (row,col).
And if that's not good enough, xlsFile may be the path to a file or a URL (yes, a URL!, great isn't it).


You could use ADO to read the values...

http://technet.microsoft.com/en-us/library/ee692882.aspx


%windir%\SysWOW64\cscript.exe ExcelSheetName.vbs

ExcelSheetName.vbs:

Const ArrSize = 100

Dim ArrSheetName()

ReDim ArrSheetName(ArrSize)

IndexArr = 0



Dim ExcelFileName

ExcelFileName = "D:\1\ExcelSrc.xls"


Dim ADOCatalog, ADOTable, ADODBConnection

Set ADOCatalog      = Createobject("ADOX.Catalog")

Set ADOTable        = Createobject("ADOX.Table")

Set ADODBConnection = CreateObject("ADODB.Connection")


Dim strConnString, strSheetName

strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ExcelFileName & ";Extended Properties=Excel 8.0;"


ADODBConnection.Open strConnString

Set ADOCatalog.ActiveConnection = ADODBConnection


For Each ADOTable In ADOCatalog.Tables

    strSheetName = ADOTable.Name

    strSheetName = Replace(strSheetName, "'", "")

    strSheetName = Left(strSheetName, InStr(1, strSheetName, "$", 1) - 1)

    'Wscript.Echo strSheetName

    ArrSheetName(IndexArr)=strSheetName

    IndexArr=IndexArr+1

Next

ReDim Preserve ArrSheetName(IndexArr-1)

ADODBConnection.Close

Set ADOCatalog      = Nothing

Set ADOTable        = Nothing

Set ADODBConnection = Nothing


For Each ArrValue in ArrSheetName

    Wscript.Echo ArrValue

Next
0

精彩评论

暂无评论...
验证码 换一张
取 消