2014-03-31 41 views
1

我有一个制表符分隔的文本文件,它使用经典ASP页面来读取它并从上到下进行显示。这里是文本文件:阅读制表符分隔的txt文件并按相反顺序显示

Email      Division  Course TotalIncorrect Score DATE_TIME 
[email protected] National  BI Course 5   40%  2014-01-23 16:38:55 
[email protected] B - H - J - L BI Course 5   100% 2014-01-31 14:56:34 
[email protected]  D - F - K  BI Course 5   100% 2014-02-07 18:11:22 
[email protected] National  BI Course 5   40%  2014-01-23 16:38:55 
[email protected] B - H - J - L BI Course 5   100% 2014-01-31 14:56:34 

这里是ASP代码读取TXT文件:

<% 

Response.CharSet = "UTF-8" 
dim import_file,counter,line,fso,objFile 
import_file="QuizScores.txt" 
counter=0 
set fso = createobject("scripting.filesystemobject") 

If (fso.FileExists("D:\Vignette\QuizScores.txt"))=true Then 

set objFile = fso.opentextfile(server.mappath(import_file)) 

str_imported_data="<table style='text-align: left; width: 98%; margin-left: 0px; margin-right: auto; font-family: Arial Narrow;' cellpadding='3' cellspacing='2' border='0'>" 
Do Until objFile.AtEndOfStream 


line = split(objFile.ReadLine, vbTab) 

if (counter Mod 2 = 0) And (counter = 0) then 
    str_imported_data=str_imported_data&"<tr bgcolor='#3C3C3C' style='font-weight: bold; color:white;'>" 
Elseif (counter Mod 2 = 0) Then 
    str_imported_data=str_imported_data&"<tr bgcolor='#EEEEEE'>" 
Else 
    str_imported_data=str_imported_data&"<tr bgcolor='#FFFFE5'>" 
end if 

counter=counter+1 
total_records=ubound(line) 

for i=0 to total_records 
    if ((i=0) or (i=6)) then 
    str_imported_data=str_imported_data&"<td style='font-weight: bold;'><font size='-1'>"&line(i)&"</td>" 
    else 
    str_imported_data=str_imported_data&"<td><font size='-1'>"&line(i)&"</td>" 
    end if 
next 
    str_imported_data=str_imported_data&"</tr>" & chr(13) 
Loop 
str_imported_data=str_imported_data&"<caption><b>Project Systems Solution Real Property Course Test Scores</b></caption></table>" 

objFile.Close 

response.Write str_imported_data 
set fso=nothing 

Else 
    Response.Write("Test Scores File does NOT exist.") 
End If 

%> 

我想有ASP页读取TXT文件,但颠倒顺序,使最近的记录将出现在表格的顶部并且最下面的记录最早。

任何帮助,将不胜感激。我在ASP上相当新!

感谢

这是我修改后的代码2014年4月1日:


<% 
Response.CharSet = "UTF-8" 
dim import_file,counter,line,fso,objFile 
Dim array_line(), array_column() 

import_file="QuizScores.txt" 

set fso = createobject("scripting.filesystemobject") 

If (fso.FileExists("D:\Vignette\QuizScores.txt")) Then 

set objFile = fso.opentextfile(server.mappath(import_file)) 
counter=0 
Do Until objFile.AtEndOfStream 
    array_line(counter) = objFile.ReadLine 
    counter=counter+1 
    Redim Preserve array_line(counter) 
Loop 
objFile.Close 
set fso=nothing 

str_imported_data="<table style='text-align: left; width: 98%; margin-left: 0px; margin-right: auto; font-family: Arial Narrow;' cellpadding='3' cellspacing='2' border='0'>" 
total_records=ubound(array_line) 

for i_row=total_records to 0 step -1 
    array_column = split(array_line(i_row), vbTab) 

    if (i_row Mod 2 = 0) And (i_row = 0) then 
     s_bgcolor = "bgcolor='#3C3C3C' style='font-weight: bold; color:white;'" 
    Elseif (i_row Mod 2 = 0) Then 
     s_bgcolor = "bgcolor='#EEEEEE'" 
    Else 
     s_bgcolor = "bgcolor='#FFFFE5'" 
    end if 

    str_imported_data=str_imported_data & "<tr>" & vbCrLf 

    for i_col = 0 to 6 
     if ((i=0) or (i=6)) then 
      str_imported_data = str_imported_data & "<td " & s_bgcolor & "><font size='-1' style='font-weight: bold;'>" & array_column(i_col) & "</td>" & vbCrLf 
     else 
      str_imported_data = str_imported_data & "<td " & s_bgcolor & "><font size='-1'>" & array_column(i_col) & "</td>" & vbCrLf 
     end if 
    next 'i_col 
    str_imported_data = str_imported_data & "</tr>" & vbCrLf & vbCrLf 
next 'i_row 

str_imported_data = "<caption><b>Project Systems Solution Real Property Course Test Scores</b></caption></table>" & vbCrLf & str_imported_data 

response.Write (str_imported_data) 
Else 
Response.Write("Test Scores File does NOT exist.") 
End If 
%> 
+2

读取到一个数组/集合,反向/排序,然后显示它.... –

+0

谢谢。从概念上讲,我知道这是必需的,但我不太清楚如何去做。我正在考虑将文本文件的每一行读入一个二维数组,但就像我所说的,我的ASP技能并不在这个级别。 – user3481759

回答

1

未经测试,但不会有很多错误。 供参考:我建议你注意我把事情分开的方式;获取更多可读代码。 哦,如果内存服务,将“bgcolor”属性置入TR在大多数浏览器中不起作用,这就是为什么我将它移动到TD的原因。

<% 
Response.CharSet = "UTF-8" 
dim import_file,counter,line,fso,objFile 
import_file="QuizScores.txt" 
counter=0 
set fso = createobject("scripting.filesystemobject") 

If (fso.FileExists("D:\Vignette\QuizScores.txt")) Then 

    set objFile = fso.opentextfile(server.mappath(import_file)) 
    counter=0 
    Do Until objFile.AtEndOfStream 
     array_line(counter) = objFile.ReadLine 
     counter=counter+1 
    Loop 
    objFile.Close 
    set fso=nothing 

    str_imported_data="<table style='text-align: left; width: 98%; margin-left: 0px; margin-right: auto; font-family: Arial Narrow;' cellpadding='3' cellspacing='2' border='0'>" 
    total_records=ubound(array_line) 

    for i_row=total_records to 0 step -1 
     array_column = split(array_line(i_row), vbTab) 

     if (i_row Mod 2 = 0) And (i_row = 0) then 
      s_bgcolor = "bgcolor='#3C3C3C' style='font-weight: bold; color:white;'" 
     Elseif (i_row Mod 2 = 0) Then 
      s_bgcolor = "bgcolor='#EEEEEE'" 
     Else 
      s_bgcolor = "bgcolor='#FFFFE5'" 
     end if 

     str_imported_data=str_imported_data & "<tr>" & vbCrLf 

     for i_col = 0 to 6 
      if ((i=0) or (i=6)) then 
       str_imported_data = str_imported_data & "<td " & s_bgcolor & "><font size='-1' style='font-weight: bold;'>" & array_column(i_col) & "</td>" & vbCrLf 
      else 
       str_imported_data = str_imported_data & "<td " & s_bgcolor & "><font size='-1'>" & array_column(i_col) & "</td>" & vbCrLf 
      end if 
     next 'i_col 
     str_imported_data = str_imported_data & "</tr>" & vbCrLf & vbCrLf 
    next 'i_row 

    str_imported_data = "<caption><b>Project Systems Solution Real Property Course Test Scores</b></caption></table>" & vbCrLf & str_imported_data 

    response.Write (str_imported_data) 
Else 
    Response.Write("Test Scores File does NOT exist.") 
End If 
%> 
+0

非常感谢您为我提供这种可能的解决方案。我一直在尝试一段时间,但无法使其工作。我想知道的一件事是total_records = ubound(line)。行不在程序的其他地方使用。有可能是我错过了另一个错字吗?再次感谢。 – user3481759

+0

哎呀! total_records = ubound(array_line) – ScotterMonkey

+0

请随时将我的代码标记为解决方案。我可以使用以下几点:-) – ScotterMonkey

相关问题