如何在代码中添加表格样式,使其显示如图所示? 我尝试修改它,但它看起来并不符合我的预期。
Private Function getItems(lb As MSForms.ListBox) As String
Dim row, col As Long
Dim i As String, items As String
Dim total As Double
'check if listbox is empty
If Me.ListboxResult.ListCount = 0 Then
getItems = ""
Exit Function
End If
'table view
items = "<table border='1' style='width: 50%'><tr><th style = 'text-align:left;'>Type</th><th style = 'text-align:left;'>Item</th><th style = 'text-align:left;'>Quantity</th><th style = 'text-align:left;'>Unit</th></tr>"
With lb
total = 0
For row = 0 To .ListCount - 1
i = "<tr>"
'add each cell to the row
i = i & "<td style= 'border: 1px solid black; border-collapse: collapse' >" & .Column(0, row) & "</td>"
i = i & "<td style= 'border: 1px solid black; border-collapse: collapse;'>" & .Column(1, row) & "</td>"
i = i & "<td style='border: 1px solid black; text-align: right; border-collapse: collapse'>" & .Column(2, row) & "</td>"
i = i & "<td style='border: 1px solid black; border-collapse: collapse'>" & .Column(3, row) & "</td>"
i = i & "</tr>" 'end the row
items = items & i
i = ""
total = total + CDbl(.Column(2, row))
Next row
i = "<tr><td style='border: none; border-collapse: collapse'></td><td style='border: none; border-collapse: collapse'></td><td style='text-align: right; border: 1px solid black; border-collapse: collapse'> <b>Total:</b> " & CStr(total) & "</td></tr>"
items = items & i
End With
items = items & "</table>"
getItems = items
End Function
你的问题是html/css问题,不是vba问题。
您的 HTML 结构很糟糕:您应该添加页眉和页脚,添加最后一个单元格,在页脚中合并前两个单元格。
<html>
<head>
<style>
thead {
font-weight: bold;
text-align: center;
}
tbody {
text-align: start;
}
tfoot {
font-weight: bold;
text-align: center;
}
.total_value {
font-weight: normal;
}
table, th, td {
border: none;
box-shadow: 2px 2px 5px grey;
}
th, td {
border: none;
box-shadow: inset 2px 2px 1px grey;
padding: 2px 5px;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Type</th>
<th>Item</th>
<th>Quantity</th>
<th>Unit</th>
</tr>
</thead>
<tbody>
<tr>
<td>General</td>
<td>Letter Opener</td>
<td>3</td>
<td>pc(s)</td>
</tr>
<tr>
<td>aaa aaa</td>
<td>bbb bbb</td>
<td>ccc cccc</td>
<td>ddd dddd dddd</td>
</tr>
<tr>
<td>aaa aaa aaa aaaaa</td>
<td>bbb bbb</td>
<td>ccc cccc cccc ccccc</td>
<td>ddd</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">Total</td>
<td class="total_value">11</td>
<td></td>
</tr>
</tfoot>
</table>
</body>
</html>
你的表格样式很丑,你应该使用这个CSS样式:
<style>
thead,tfoot {
font-weight: bold;
text-align: center;
}
tbody {
font-weight: normal;
text-align: start;
}
.total_value {
font-weight: normal;
}
th, td {
padding: 5px 5px;
}
table {
border-collapse: collapse;
border: 2px solid #009977;
box-shadow: 4px 4px 5px rgba(0, 0, 0, 0.35);
}
thead {
border-bottom: 2px solid #009977;
}
tfoot {
border-top: 2px solid #009977;
}
thead,tfoot tr {
background-color: #22bb22;
color: #ffffff;
}
tbody tr {
background-color: #ffffff;
color: #333333;
}
tbody tr {
border-bottom: 1px solid #dddddd;
}
tbody tr:nth-of-type(even) {
background-color: #f5f5f5;
}
</style>
生成表格 HTML 代码的 VBA 代码(未测试):
Private Function getItems(lb As MSForms.ListBox) As String
Dim row, col As Long
Dim i As String, html As String
Dim total As Double
'check if listbox is empty
If Me.ListboxResult.ListCount = 0 Then
getItems = ""
Exit Function
End If
'table view
html = "<table>"
' add head table
html = html & "<thead><tr><th>Type</th><th>Item</th><th>Quantity</th><th>Unit</th></tr></thead>"
' add body table
html = html & "<tbody>" 'start of body
With lb
total = 0
For row = 0 To .ListCount - 1
i = "<tr>" 'start of row
'add each cell to the row
i = i & "<td style= 'border: 1px solid black; border-collapse: collapse' >" & .Column(0, row) & "</td>"
i = i & "<td style= 'border: 1px solid black; border-collapse: collapse;'>" & .Column(1, row) & "</td>"
i = i & "<td style='border: 1px solid black; text-align: right; border-collapse: collapse'>" & .Column(2, row) & "</td>"
i = i & "<td style='border: 1px solid black; border-collapse: collapse'>" & .Column(3, row) & "</td>"
i = i & "</tr>" 'end the row
html = html & i
total = total + CDbl(.Column(2, row))
Next row
End With
html = html & "<tbody>" 'end of body
' add footer table
html = html & "<tfoot><tr><td colspan=""2"">Total</td><td class=""total_value"">" + CStr(total) + "</td><td></td></tr></tfoot>"
html = html & "</table>" 'end of table
getItems = html
End Function
您必须在输出文件的开头添加CCS样式(标记内容)...或者通过在每个HTML标记中使用“style”属性将CCS代码集成到HTML代码中,但它非常重...