程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> asp實現表格的行列轉換代碼

asp實現表格的行列轉換代碼

編輯:ASP技巧

由於我使用程序生成一個表格,生成出來的表格行和列需要轉換一下,如2行3列的表格要轉換成3行2列的表格(這只是一個舉例,實際過程中表格的行和列是不確定的)。由於生成表格的過程太麻煩,所以不想再去程序只找其生成規律,想通過生成後的表格字符串,直接轉換表格的行和列,這樣比較方便。
下面是我的代碼:
< %
str=str&"<table width=""500"" border=""1"" cellspacing=""0"" cellpadding=""0""> "
str=str&"  <tr> "
str=str&"    <td height=""20"" align=""center"">1 </td> "
str=str&"    <td align=""center"">2 </td> "
str=str&"    <td align=""center"">3 </td> "
str=str&"  </tr> "
str=str&"  <tr> "
str=str&"    <td height=""20"" align=""center"">4 </td> "
str=str&"    <td align=""center"">5 </td> "
str=str&"    <td align=""center"">6 </td> "
str=str&"  </tr> "
str=str&"</table> "

response.write str&"<br>" '這裡輸出原始的表格

str=Replace(str,"  ","")
str=Replace(str," <","<")
str=Replace(str,"<table width=""500"" border=""1"" cellspacing=""0"" cellpadding=""0"">","")
str=Replace(str,"</table>","")
str=Replace(str,"<tr>","")
str=Replace(str,"<td align=""center"">","")
str=Replace(str,"<td height=""20"" align=""center"">","")

str=Replace(str,"</tr>","|")
str=Trim(Replace(str,"</td>",","))

str=Left(str,Len(str)-1)

a=Split(str,"|")
m=UBound(a)
n=0
If m>0 Then
a2=Trim(a(0))
a2=Left(a2,Len(a2)-1)
b=Split(a2,",")
n=UBound(b)
End if
ReDim c(n+1,m+1)

'response.write "m="&m&" X n="&n&"<br>" 
For i=0 To m
    a2=Trim(a(i))
 a2=Left(a2,Len(a2)-1)
 b=Split(a2,",")
    For j=0 To UBound(b)
       c(j,i)=Trim(b(j))
 next
Next

str5="<table width=""500"" border=""1"" cellspacing=""0"" cellpadding=""0"" >"
For i=0 To n
   str5=str5&"<tr>"
   For j=0 To m
      str5=str5&"<td align=""center""  height=""20"" >"&c(i,j)&"</td>"
   Next
   str5=str5&"</tr>"
Next
str5=str5&"</table>"

response.write str5 '這裡輸出轉換後的表格
% >

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved