如何阅读WSH电子表格?
我需要使用WSH下的JScript从.xls文件读取一些单元格值。
有没有COM对象或任何我可以用来做到这一点?
其实这是一个COM组件。 它的progId是“Excel.Application”,你可以这样使用它:
var XLS = WScript.CreateObject("Excel.Application") ; XLS.Workbooks.open(xlsFile) ; var cellValue = XLS.Cells(row,col).Value ;
而已。 就如此容易。 variablescellValue
现在保存在单元格( row
, col
)中的值。
如果这还不够好, xlsFile
可能是一个文件或URL的path(是的,一个URL!,不是很好)。
您可以使用ADO来读取值…
%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