我无法将数组矩阵放入一个HTML表中。由于某种原因,矩阵的表和值增加了三倍,我现在不解释为什么。
输出:
Sub writeTable(arrData)
Dim intCount
For intCount = LBound(arrData) To UBound(arrData)
document.write "<!DOCTYPE html>"
document.write "<html>"
document.write "<head>"
document.write "<style>"
document.write "table, th, td {"
document.write "border: 1px solid black;"
document.write "border-collapse: collapse;"
document.write "}"
document.write "</style>"
document.write "</head>"
document.write "<body>"
document.write "<p>Test</p>"
document.write "<table style='width:50px'>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "</table>"
document.write "</body>"
document.write "</html>"
Next
End Sub
答案 0 :(得分:1)
您发布的代码将创建一个完整的HTML文档,其中数组的每一行都有3行的表格。以下代码片段将输出一行:
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
重复该代码段3次将在输出中重复该行3次。
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(1, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
第二个语句中只有一个错字,导致第一个字段在第一行中输出两次。
基本上有两种方法可以解决此问题:
仅对表行而不是整个文档进行循环,并在循环主体中仅输出一行:
document.write "<!DOCTYPE html>"
document.write "<html>"
...
document.write "<p>Test</p>"
document.write "<table style='width:50px'>"
For intCount = LBound(arrData) To UBound(arrData)
document.write "<tr>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(0, intCount) &"</td>"
document.write "<td>"& arrData(2, intCount) &"</td>"
document.write "</tr>"
Next
document.write "</table>"
document.write "</body>"
document.write "</html>"
这是推荐的方法。
删除循环并重复语句,同时自己增加行索引:
document.write "<td>"& arrData(0, 0) &"</td>"
document.write "<td>"& arrData(1, 0) &"</td>"
document.write "<td>"& arrData(2, 0) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, 1) &"</td>"
document.write "<td>"& arrData(1, 1) &"</td>"
document.write "<td>"& arrData(2, 1) &"</td>"
document.write "</tr>"
document.write "<tr>"
document.write "<td>"& arrData(0, 2) &"</td>"
document.write "<td>"& arrData(1, 2) &"</td>"
document.write "<td>"& arrData(2, 2) &"</td>"
我不推荐这种方法,因为它不能在不修改代码的情况下处理数组中不同数量的行。