如何在vbscript中检测文本文件(EOF)的结尾?

时间:2016-09-26 16:21:53

标签: excel vbscript split eof

我正在使用VBscript将文本文件中的信息排序到Excel工作表。我能够这样做,但只读取了一行文本,并且循环控件似乎没有转到文本文件的下一行。我正在使用 AtEndOfStream ,但正如我所说,我只得到一行输出。有人可以帮我弄清楚如何执行程序直到文件结束?

以下是代码:

Set objExcel = CreateObject("Excel.Application")            'Bind to the Excel object
objExcel.Workbooks.Add                                      'Create a new workbook.
Sheet = 1                                                   'Select the first sheet
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)    'Bind to worksheet. 
objSheet.Name = "ErrorSpreadsheet"                          'Name the worksheet
strExcelPath = "c:\scripts\ErrorSpreadsheet.xlsx"           'Set the save location

objSheet.Range("A1:E1").Font.Bold = True
objExcel.Columns(5).AutoFit()
objSheet.Cells(1, 1).Value = "Date"                         'Row 1 Column 1 (A)
objSheet.Cells(1, 2).Value = "Time"                         'Row 1 Column 2 (B)
objSheet.Cells(1, 3).Value = "Category"                     'Row 1 Column 3 (C)
objSheet.Cells(1, 4).Value = "Error"                        'Row 1 Column 4 (D)
objSheet.Cells(1, 5).Value = "Index"                        'Row 1 Column 5 (E)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\scripts\ErrorMsg.txt",1)

i = 0
r = 2
c = 1
j = 0
Do While NOT objFile.AtEndOfStream 
    Redim Preserve arrFileLines(10)
    arrFileLines(i) = objFile.ReadLine
    text = arrFileLines(j)
    'Dim a() 
    a = Split(text,";")
    For Each line In a
        objSheet.Cells(r,c).Value = a(i)
        i = i + 1
        c = c + 1
    Next
    j = j + 1
    r = r + 1
Loop


objFile.Close
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

我现在以这样的方式编写代码,我没有收到任何错误。在我的excel文件中,我既有标题行又有分割文本文件的一行,但没有以下任何行。文本文件的写法如下:

       9/23;12:00;cat;error;236
       9/24;12:30;cat2;error;897
       9/24;4:06;cat5;error;23
       9/25;3:45;cat1;error;54
       9/26;1:09;cat6;error;18

因此我在excel中得到的输出是Excel Output

有人可以帮我弄清楚如何到达文本文件的末尾吗?

2 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,您的问题在于您的变量,因为您正在尝试重用它们,但却没有正确地执行它们。 如果您手动浏览代码,您会看到,在第一次迭代中,您要将objFile.ReadLine添加到arrFileLines(0),然后将arrFileLines(0)存储到text
但是,您将进入内部For循环并迭代i,这将在分割后将For循环保留为4

第二次进入循环时,您会将objFile.ReadLine添加到arrFileLines(4),然后将arrFileLines(1)(空的)存储到text。您不会收到任何错误,因为数组具有固定的尺寸,并且在您完成文件遍历之前将在范围内,但您也不会得到任何结果。
这就是为什么我会建议你使用不同的变量并避免重复使用 实际上,如果唯一的目的是将CSV中的值添加到Excel工作表中,您甚至不需要将objFile.ReadLine存储到arrFileLines中,因为您没有使用该数组。只需将其直接添加到Text即可。

所以,通过一些修改,比如变量重命名等等,你最终会得到这样的结果:

' The rest of your code, Variables declarations and so forth

iRow = 2    ' Starting Row
iCol = 1    ' Starting Col

Do While Not objFile.AtEndOfStream
    currLine = objFile.ReadLine
    arrLine = Split(currLine, ";")
    For Each item In arrLine
        objSheet.Cells(iRow, iCol).Value = item
        iCol = iCol + 1
    Next
    iCol = 1    ' Reset for every row
    iRow = iRow + 1
Loop

' The rest of your code

答案 1 :(得分:0)

为什么您要分别读取每一行,而现在却可以用objFile.ReadAll一次将整个文件读取到缓冲区中?通过这种方式1)您立即知道EOF(=缓冲区的长度),2)您可以轻松地“读取”缓冲区中的每一行,3)您“读取”行比读取磁盘快得多,并且4)您避免了过度使用磁盘,特别是。如果文件很长!