Public Function txtTable(strArr() As String, Optional alignLeft As Boolean = False) As String
'风格3:完整边框 + 竖线 + 横线
On Error Resume Next
Dim rows As Integer, cols As Integer
Dim row As Integer, col As Integer
Dim colMax() As Integer
Dim topLine As String, midLine As String, botLine As String
Dim line As String, result As String
rows = UBound(strArr, 1) - LBound(strArr, 1) + 1
cols = UBound(strArr, 2) - LBound(strArr, 2) + 1
If rows = 0 Or cols = 0 Then
Exit Function
[mw_shl_code=python,true]def txtTable(data):
"""风格3:完整边框 + 竖线 + 横线"""
if not data:
return ""
cols = len(data[0])
col_max = [max(get_str_width(row[i]) for row in data) for i in range(cols)]
top = '┌─' + '─┬─'.join(['─' * w for w in col_max]) + '─┐'
mid = '├─' + '─┼─'.join(['─' * w for w in col_max]) + '─┤'
bot = '└─' + '─┴─'.join(['─' * w for w in col_max]) + '─┘'
lines = [top]
for i, row in enumerate(data):
cells = [pad_to_width(row[j], col_max[j]) for j in range(cols)]
lines.append('│ ' + ' │ '.join(cells) + ' │')
if i == 0:
lines.append(mid)
lines.append(bot)
return '\n'.join(lines)
End If
' 计算每列最大宽度
ReDim colMax(1 To cols)
For col = 1 To cols
colMax(col) = 0
For row = 1 To rows
Dim w As Integer
w = GetStrWidth(strArr(row, col))
If w > colMax(col) Then colMax(col) = w
Next
Next