我希望VBScript读取CSV文件并使用条件格式在HTML表格中打印

时间:2014-11-20 11:02:43

标签: vbscript

我需要VB脚本来读取text / csv文件并使用表格格式在html页面中打印数据。当值超过200时,我需要应用条件来更改字段颜色。

我的要求:

  1. 用于读取csv / text(日志解析)文件的VB脚本,并在html页面中以表格格式打印数据
  2. 当AvgTime字段值超过200
  3. 时,需要应用条件格式来更改字段颜色

    目前我正在使用下面不支持条件格式的方法。

    1. 将IIS日志解析为CSV文件,下面是输出格式(使用logparser

      Date,csUriStem,Hits,AvgTime,MaxTime,MinTime,Status    
      2014-11-14,/test/al/trmpaaaaresearch.aspx,24,57.625000,328,0,200.000000     
      2014-11-14,/test/al/trmalmaindets.aspx,335,217.677612,13593,15,200.000000    
      2014-11-14,/test/queuebulkapprove.aspx,64,862.875000,2281,0,200.000000    
      2014-11-14,/test/al/trmtestsearch.aspx,67,53.268657,296,0,200.000000    
      
    2. 将CSV文件解析为文本文件,下面是输出格式(使用logparser

      '<tr><td>' Date       '</td><td>' csUriStem                       '</td><td>' Hits '</td><td>' AvgTime    '</td><td>' MaxTime '</td><td>' MinTime '</td><td>' Status     '</td></tr>'
      ---------- ---------- ----------- ------------------------------- ----------- ---- ----------- ---------- ----------- ------- ----------- ------- ----------- ---------- ------------
      <tr><td>   2014-11-14 </td><td>   /test/al/trmalmaindets.aspx      </td><td>   335  </td><td>   217.677612 </td><td>   13593   </td><td>   15      </td><td>   200.000000 </td></tr>    
      <tr><td>   2014-11-14 </td><td>   /test/queuebulkapprove.aspx      </td><td>   64   </td><td>   862.875000 </td><td>   2281    </td><td>   0       </td><td>   200.000000 </td></tr>    
      <tr><td>   2014-11-14 </td><td>   /test/al/trmtestsearch.aspx      </td><td>   67   </td><td>   53.268657  </td><td>   296     </td><td>   0       </td><td>   200.000000 </td></tr>    
      
    3. 使用vbscript读取文本文件并在html文件行中打印数据。外出显示为附件123.jpg![htmlpage] [1]

    4. 批次代码:

      rem IIS logs parsing into CSV file
      C:\InfraLogsSync_dontdelete\Website_status\LogParser\LogParser.exe -i:iisw3c -o:csv "SELECT Min(TO_LOCALTIME(date)) AS Date, TO_LOWERCASE(cs-uri-stem) AS csUriStem, COUNT(*) AS Hits, DIV ( MUL(1.0, SUM(time-taken)), Hits ) AS AvgTime, Max(time-taken) AS MaxTime, Min(time-taken) AS MinTime, TO_REAL(STRCAT(TO_STRING(sc-status), STRCAT('.', TO_STRING(sc-substatus)))) AS Status INTO C:\InfraLogsSync_dontdelete\Website_status\Website_TimeTaken.csv FROM E:\Wlogs\W3SVC1755586166\ex*.log WHERE (TO_LOCALTIME(TO_TIMESTAMP(date, time)) >= SUB(TO_LOCALTIME(SYSTEM_TIMESTAMP()), TIMESTAMP('0000-01-01 01:00', 'yyyy-MM-dd HH:mm'))) and (cs-uri-stem like '%%%.%.as%%%') GROUP BY TO_LOWERCASE(cs-uri-stem), TO_REAL(STRCAT(TO_STRING(sc-status), STRCAT('.', TO_STRING(sc-substatus)))) HAVING (COUNT(*)>2) order by status"
      
      rem CSV file parsing into text file
      C:\InfraLogsSync_dontdelete\Website_status\LogParser\LogParser.exe -rtp:-1 "select '<tr><td>', Date, '</td><td>', csUriStem, '</td><td>', Hits, '</td><td>', AvgTime, '</td><td>', MaxTime, '</td><td>', MinTime, '</td><td>', Status, '</td></tr>' into C:\Users\ilom08990\Desktop\Website_status\Website_status\123.txt from C:\Users\ilom08990\Desktop\Website_status\Website_status\123.csv"    
      
      rem VBScript execution
      cscript //nologo C:\Users\ilom08990\Desktop\Website_status\Website_status\Website_TimeTaken.vbs > C:\Users\ilom08990\Desktop\Website_status\Website_status\123.html    
      
    5. VBScript代码:

      Option Explicit
      Dim objFile, strLine
      Dim objFSO, aline
      
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile= objFSO.OpenTextFile("C:\Users\ilom08990\Desktop\Website_status\Website_status\123.txt", 1)
      
      Wscript.Echo "<tr><td><h1 style=color:blue> WebSite Perofrmance : 140 Server </h1></td></tr>"
      
      Wscript.Echo "<html>"
      Wscript.Echo "<head>"
      Wscript.Echo "<style> "
      Wscript.Echo "table"
      Wscript.Echo "{"
      Wscript.Echo "border-collapse:collapse;"
      Wscript.Echo "}"
      Wscript.Echo "table, td, th"
      Wscript.Echo "{"
      Wscript.Echo "border:1px solid black;"
      Wscript.Echo "}"
      Wscript.Echo "table"
      Wscript.Echo "{"
      Wscript.Echo "background-color:LIGHTBLUE;"
      Wscript.Echo "}"
      Wscript.Echo "</style>"
      Wscript.Echo "<head/>"
      Wscript.Echo "<body><table>"
      
      Do While Not objFile.AtEndOfStream
         strLine = objFile.readline
         Wscript.Echo "<p style=color:red>" & strLine & "</p>"
      Loop
      objFile.Close
      
      Wscript.Echo "</table>"
      Wscript.Echo "</body>"
      Wscript.Echo "</html>"
      

2 个答案:

答案 0 :(得分:0)

我认为对于条件格式,您需要删除第二个logparser步骤并在VBScript中构建整个表。像这样:

...
Wscript.Echo "<body><table>"

Do Until objFile.AtEndOfStream
  line = Split(objFile.ReadLine, ",")
  On Error Resume Next
  If IsNumeric(line(3)) Then
    If CDbl(line(3)) > 200 Then
      WScript.StdOut.Write "<tr style=""color:red""><td>"
    Else
      WScript.StdOut.Write "<tr><td>"
    End If
  Else
    WScript.StdOut.Write "<tr><td>"
  End If
  WScript.StdOut.WriteLine Join(a, "</td><td>") & "</td></tr>"
Loop
objFile.Close

Wscript.Echo "</table>"
...

答案 1 :(得分:0)

谢谢Ansgar Wiechers,以获得迅速的支持。请在下面找到更新代码

Option Explicit     
Dim objFile, strLine     
Dim objFSO, aline, i     
Set objFSO = CreateObject("Scripting.FileSystemObject")     
Set objFile= objFSO.OpenTextFile("C:\InfraLogsSync_dontdelete\Website_status\test.txt", 1)     

Wscript.Echo "<tr><td><h1 style=color:blue> Fasttrack WebSite Perofrmance : 140 Server </h1></td></tr>"     

Wscript.Echo "<html>"     
Wscript.Echo "<head>"     
Wscript.Echo "<style> "     
Wscript.Echo "table"     
Wscript.Echo "{"     
Wscript.Echo "border-collapse:collapse;"     
Wscript.Echo "}"     
Wscript.Echo "table, td, th"     
Wscript.Echo "{"     
Wscript.Echo "border:1px solid black;"     
Wscript.Echo "}"     
Wscript.Echo "table"     
Wscript.Echo "{"     
Wscript.Echo "background-color:LIGHTBLUE;"     
Wscript.Echo "}"     
Wscript.Echo "</style>"     
Wscript.Echo "<head/>"     
Wscript.Echo "<body><table style=width:100%>"     

  Do While Not objFile.AtEndOfStream     
     strLine = objFile.readline     
     aline = Split(strLine, ",")     
         Wscript.Echo "<tr>"     

           For i = LBound(aline) to UBound(aline)     
             If IsNumeric(aline(3)) then                
                If CLng(aline(3)>200) then     
            Wscript.Echo "<td style='color:red;font-Weight:bold;'>" & aline(i) & "</td>"     
            Else     
            Wscript.Echo "<td>" & aline(i) & "</td>"     
                End If     
              Else     
          Wscript.Echo "<td style='font-Weight:bold;font-size: 20px;'>" & aline(i) & "</td>"     
              End If     
           Next     
     Wscript.Echo "</tr>"            

  Loop     
  objFile.Close     


Wscript.Echo "</table>"     
Wscript.Echo "</body>"     
Wscript.Echo "</html>"