VBScript で Excel
Excel シートの内容を 取得する
↓ こんな Excel ファイルを読む
A | B | C | D | E | F | G | H | I | J | |
---|---|---|---|---|---|---|---|---|---|---|
1 | A1 | B1 | C1 | D1 | E1 | F1 | G1 | H1 | J1 | |
2 | A2 | B2 | C2 | D2 | E2 | F2 | G2 | H2 | J2 | |
3 | A3 | B3 | C3 | D3 | E3 | F3 | G3 | H3 | J3 | |
4 | A4 | B4 | C4 | D4 | E4 | F4 | G4 | H4 | J4 | |
5 | A5 | B5 | C5 | D5 | E5 | F5 | G5 | H5 | J5 | |
6 | J6 | |||||||||
7 | A7 | B7 | C7 | D7 | E7 | F7 | G7 | H7 | I7 | J7 |
実行結果
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False '警告メッセージをOFF'ブックを読み取り専用で開く
Dim book
Set book = objExcel.Workbooks.Open(WScript.Arguments(0))Dim sheet
Set sheet = book.WorkSheets.Item(WScript.Arguments(1))For Each row In sheet.Cells.CurrentRegion.Rows
Dim rec: rec = Array()
Dim idx: idx = 0For Each col In row.Columns
ReDim Preserve rec(idx)
rec(idx) = col.Value
idx = idx + 1
NextWScript.Echo Join(rec, ",")
NextobjExcel.Quit
Set objExcel = Nothing
C:\>cscript c:\study\vbscript\chapter004\lesson004.vbs c:\study\Book1.xls Sheet1
//nologo
A1,B1,C1,D1,E1,F1,G1,H1
A2,B2,C2,D2,E2,F2,G2,H2
A3,B3,C3,D3,E3,F3,G3,H3
A4,B4,C4,D4,E4,F4,G4,H4
A5,B5,C5,D5,E5,F5,G5,H5