2016-11-17 135 views
0

我試圖讀取包含行的文本文件,然後輸出它們在列的形式在HTML文件中。使用WScript.echo將其顯示在屏幕上時,我沒有任何問題,但我無法將其獲取到HTML文件中的表格。嘗試運行vbs文件時出現以下錯誤:類型不匹配:'OpenTextFile'。任何指導,將不勝感激VBScript來輸出輸出HTML表格

enter image description here

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") 
Dim OutputHTML : Set OutputHTML = fso.CreateTextFile("C:\Users\Istaley.RXDATA\Desktop\NewEmployeeTest\Part2_TableData.html") 
Dim file : Set file = fso.OpenTextFile("C:\Users\Istaley.RXDATA\Desktop\NewEmployeeTest\Part2_data.txt", 1, True) 
Dim fc : fc = file.ReadAll : file.close : Dim fcArray : fcArray = Split(fc, vbCrLf) 
OutputHTML.WriteLine "<html>" 
OutputHTML.Writeline "<body>" 
OutputHTML.WriteLine "<table BORDER=1>" 
Dim opArray() : ReDim opArray(0) 
For Each row In fcArray 
    Dim tmp: tmp = Split(row, "|") 
    For ent=0 To UBound(tmp) 
     If ent > UBound(opArray) Then 
      ReDim Preserve opArray(UBound(opArray)+1) 
      opArray(ent) = Trim(tmp(ent)) 
     Else 
      If Len(opArray(ent)) > 0 Then 
       OutputHTML.WriteLine "<tr>" 
       opArray(ent) = opArray(ent) & " " & Trim(tmp(ent)) 
       OutputHTML.WriteLine "</tr>" 
      Else 
       opArray(ent) = Trim(tmp(ent)) 
      End If 
     End If 
    Next 
Next 
WScript.echo Join(opArray, vbCrLf) 
OutputHTML.WriteLine "</table>" 
OutputHTML.WriteLine "</body>" 
OutputHTML.WriteLine "</html>" 
OutputHTML.Write Join(opArray, vbCrLf) : OutputHTML.Close 
+0

將'OutputHTML.Write Join(opArray,vbCrLf)'行放在'OutputHTML.WriteLine'之前''。 BTW哪裏都是' 「​​」'和'「」'標籤? – omegastripes

回答

1

的問題是此行..的OpenTextFile的第一個參數需要一個字符串,但你通過它的對象。您已經使用CreateTextFile打開了要寫入的文本文件。

Set WriteOutput = fso.OpenTextFile(OutputHTML, 8, True) 

擺脫這一行的,改變的WriteOutput所有剩餘的實例OutputHTML

+0

感謝您的幫助,錯誤是沒有更多! html輸出未顯示在3列或表中。在1 2 3 2 3 4 3 4 5 4 5 6 5 6之上有一個矩形7.我將更新上面的代碼,告訴你我有什麼併發布了一個屏幕截圖。如果我添加WScript.echo,我可以看到我想要的輸出,但我無法將它放到html表格中 – Istaley

1

我知道這是一個古老的話題,但對於那些希望做同樣的事情的人來說,這可能是有用的,就像我一樣。這有點匆忙,但我已經在線添加了評論。位已採取從多個位置,所以它不是都是我自己的工作......

Function LoadFile(File) 
    On Error Resume Next 
    'Declaire all variables 
    Dim fso,F,ReadText,strError 
    Dim ReadArray 
    Dim ReadHTMLOutput, ReadRowCount, ReadRowItem, ReadRowItemSplit, ReadElementCount, ReadElementItem 
    'Create the object to read files 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    'Set the file to read and the format 
    Set F = fso.OpenTextFile(File,1) 
    'If there's a problem, say so... 
    If Err.Number <> 0 Then 
     strError = "<center><b><font color=Red>The file "& File &" dosen't exists !</font></b></center>" 
     OutputTable.InnerHTML = strError 
     Exit Function 
    End If 
    'Read the contents of the file into ReadText 
    ReadText = F.ReadAll 

    'Split the text based on Carriage return/Line feed 
    ReadArray = Split(ReadText,vbCrLf) 
    'fill the output variable with the HTML of the start of the table 
    ReadHTMLOutput = "<table border=" & chr(34) & "2" & chr(34) & ">" & vbcrlf 
    'starting at 0 until the last line in the array, run through each line 
    For ReadRowCount=0 to UBound(ReadArray) 
     'Take the whole row into it's own variable 
     ReadRowItem = ReadArray(ReadRowCount) 
     'Split the row (separated by commas) into an array 
     ReadRowItemSplit = Split(ReadRowItem,",") 
     'Add the HTML for the row of the table 
     ReadHTMLOutput = ReadHTMLOutput & "<tr>" & vbcrlf 
     'starting at 0 until the last entry of the row array, run through each element 
     For ReadElementCount=0 to UBound(ReadRowItemSplit) 
      'Read the element into a variable 
      ReadElementItem = ReadRowItemSplit(ReadElementCount) 
      'If the element is blank, put a space in (stops the cell being formatted empty) 
      If ReadElementItem = "" Then ReadElementItem = "&nbsp;" 
      'Add the HTML for the cell of the row of the table 
      ReadHTMLOutput = ReadHTMLOutput & "<td>" & ReadElementItem & "</td>" & vbcrlf 
     'Go to the next element in the row 
     Next 
     'Add the HTML for the end of the row of the table 
     ReadHTMLOutput = ReadHTMLOutput & "</tr>" & vbcrlf 
    'Go to the next row in the file 
    Next 
    'Add the HTML for the end of the table 
    ReadHTMLOutput = ReadHTMLOutput & "</table>" & vbcrlf 
    'Fill the DIV with the contents of the variable 
    OutputTable.InnerHTML = ReadHTMLOutput 
End Function 

,並在HTML:

<div id="OutputTable"></div> 

這樣一來,DIV充滿了從HTML ReadHTMLOutput