将ASP纪录集输出成n列的的表格形式显示的方法

前些日子有网友问:将ASP纪录集输出成n列的的表格形式显示的方法,现在写了一个,方便大家使用。
'定义变量
Dim cn,rs,Sql

Sql = "select CustomerID from Orders"

'记录总数
Dim TotalNumbe
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB.1;User ID=sa;Initial Catalog=NorthWind;Data Source=.;Password=;"

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open Sql, cn, 3, 1
TotalNumber = rs.RecordCount
If TotalNumber = 0 Then
Response.Write "没有记录输出。"
'www.knowsky.com
Else
Dim jj,nLeft,cCol
jj = 0
nCol = 415
nLeft = nCol- (TotalNumber Mod nCol)
If nLeft = nCol Then nLeft = 0
Response.Write "

 1<table border=""><tr>" &amp; vbCrLf   
 2While not rs.EOF   
 3Response.Write "<td>" &amp; rs("CustomerID") &amp; "</td>" &amp; vbCrLf   
 4'If (jj Mod nCol) = (nCol - 1) And jj &lt;&gt; TotalNumber - 1 Then Response.Write "</tr><tr>" &amp; vbCrLf   
 5'If (jj Mod nCol) = (nCol - 1) And jj = TotalNumber-1 Then Response.Write "</tr>" &amp; vbCrLf   
 6  
 7If (jj Mod nCol) = (nCol - 1) Then   
 8If jj &lt;&gt; TotalNumber - 1 Then   
 9Response.Write "<tr>" &amp; vbCrLf   
10Else   
11Response.Write "</tr>" &amp; vbCrLf   
12End If   
13End If   
14jj = jj + 1   
15rs.MoveNext   
16Wend   
17If nLeft &lt;&gt; 0 And nLeft &lt;&gt; nCol Then   
18If nCol &lt; TotalNumber Then   
19For i = 1 to nLeft   
20Response.Write "<td> </td>" &amp; vbCrLf   
21Next   
22End If   
23Response.Write "" &amp; vbCrLf   
24End If   
25Response.Write "</table>

"
End If
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
Response.End

Published At
Categories with Web编程
Tagged with
comments powered by Disqus