【问题标题】:Exporting last row in CSV file using VBScript使用 VBScript 导出 CSV 文件中的最后一行
【发布时间】:2018-05-17 18:47:39
【问题描述】:

在脚本方面我还是个新手,所以如果这是我的一个简单错误,请原谅我。

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

到目前为止,我只能让它拉到最上面一行,不知道如何将它切换到底部。我的代码有点粗糙。任何帮助将不胜感激。

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

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

【问题讨论】:

    标签: excel csv vbscript


    【解决方案1】:

    嗯……

    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。

    【讨论】:

    • 谢谢!比较一下,我发现我做了很多不必要的脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2011-10-28
    相关资源
    最近更新 更多