我從來沒有使用Excel,也沒有一個副本來測試任何代碼,所以請耐心等待。
的基想法的VBScript(單機.vbs文件)...
'connect to Excel
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = True
'open the file
XLApp.DisplayAlerts = False
Set XLBook = XLApp.WorkBooks.Open("c:\path_to\my.xls")
XLApp.DisplayAlerts = True
'if you know the Worksheet name:
sSheetName = "MySheetName"
Set XLSheet = XLBook.Sheets.Item(sSheetName)
'else use index:
Set XLSheet = XLBook.Worksheets(1)
'or if you want to process all sheets:
For Iter = 1 To XLBook.Worksheets.Count
Set XLSheet = XLBook.Worksheets(Iter)
...
Next
'you can use XLSheet.Columns.Count to iterate
'or if the columns names are known:
Set Col1 = XLSheet.Columns("Col1")
Set Col2 = XLSheet.Columns("Col2")
'or get Range:
Set Range = XLSheet.Columns("Col1:Col2")
'same as above:
Set Range = XLSheet.Range("Col1:Col2")
'vbs FileSystemObject:
Set fso = CreateObject("Scripting.FileSystemObject")
iLoop = 1 'to enumerate file names
intRow = 2 '?... not sure about this
Do While Range.Cells(intRow,1).Value <> ""
Set stream = fso.CreateTextFile("Row" & iLoop & ".exp", True)
stream.WriteLine "Col1 " & Range.Cells(intRow, 1).Value
stream.WriteLine "Col2 " & Range.Cells(intRow, 2).Value
stream.Close
iLoop = iLoop + 1
intRow = intRow + 1
Loop
'XLBook.Save
XLBook.Close
XLApp.Quit
Set stream = Nothing
Set fso = Nothing
Set Range = Nothing
Set XLSheet = Nothing
Set XLBook = Nothing
Set XLApp = Nothing
你必須標記'vb.net'和'script'這是獨家恕我直言。這是一個VB.NET程序還是VBA腳本? – GolfWolf 2013-02-25 09:54:12
@ w0lf理想情況下,它應該是Windows腳本文件。 – 2013-02-25 10:15:41