使用VBScript导出CSV文件中的最后一行

时间:2018-05-17 18:47:39

标签: excel csv vbscript

对于脚本来说,我仍然是新手,所以请原谅我这是一个简单的错误。

我正在尝试使用我创建的VBS脚本在不断更新的CSV文件中显示最后一行,并使用CMD执行。我一直在拉动和修补来自多个来源的代码。

到目前为止,我只能将它拉到最上面,无法弄清楚如何将其切换到底部。我的代码有点粗糙。任何帮助将不胜感激。

到目前为止,这是我的代码:

Option Explicit
Dim objExcel
Dim excelPath
Dim workSheetCount
Dim counter
Dim currentWorkSheet
Dim usedRowsCount
Dim row
Dim top
Dim Cells
Dim curRow
Dim word


excelPath = "c:\Test\test.csv"

WScript.Echo "Reading Data from " & excelPath

Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = 0
objExcel.Workbooks.open excelPath, false, true

workSheetCount = objExcel.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "-----------------------------------------------"
WScript.Echo "Reading data from worksheet " & counter & vbCRLF

Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(counter)

usedRowsCount = currentWorkSheet.UsedRange.Rows.Count
top = currentWorksheet.UsedRange.Row

Set Cells = currentWorksheet.Cells
For row = 0 to (usedRowsCount-1)
    curRow = row+top
    word = Cells(curRow).Value
    WScript.Echo (word)
Next
Next

Set currentWorkSheet = Nothing
objExcel.Workbooks(1).Close
objExcel.Quit
Set currentWorkSheet = Nothing
Set objExcel = Nothing

1 个答案:

答案 0 :(得分:0)

嗯...

filename = "C:\Test\test.csv"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f   = fso.OpenTextFile(filename)

Do Until f.AtEndOfStream
    line = f.ReadLine
Loop

f.Close

WScript.Echo line

CSV是一种文本格式,因此您可以使用FileSystemObject方法轻松处理它,而无需使用Excel。