将单单元格值列表转换为HTML

时间:2013-10-04 20:04:34

标签: excel

我的格式为:

键:值| KEY1:VALUE1 | KEY2:VALUE2 |等...

每个单元格中的键数从0到10不等。

我需要将该单元格转换为简单的HTML表格,例如:

    <table>
        <tr>
            <td>Key</td>
            <td>Value</td>
        </tr>
        <tr>
            <td>Key1</td>
            <td>Value2</td>
        </tr>
        ... etc
    </table>

这可能在使用计算时,还是我需要查看宏?一眼就看出我没有看到任何用于使用分隔符来分割字符串的函数,所以看起来在计算中做起来很麻烦,如果不是不可能的话。如果可行的话,计算将是首选。

1 个答案:

答案 0 :(得分:0)

我确信它可以在一个包含很多辅助列的公式中完成,但它会是多么糟糕。这是一些可以做你想做的VBA。

Public Function KeyValueToHtml(ByVal sKeyValue As String) As String

    Dim vaPipe As Variant
    Dim vaColon As Variant
    Dim i As Long
    Dim aReturn() As String

    vaPipe = Split(sKeyValue, "|")
    ReDim aReturn(LBound(vaPipe) To UBound(vaPipe))

    For i = LBound(vaPipe) To UBound(vaPipe)
        vaColon = Split(vaPipe(i), ":")
        aReturn(i) = Tag(Tag(vaColon(0), "td") & vbNewLine & Tag(vaColon(1), "td"), "tr", , True)
    Next i

    KeyValueToHtml = Tag(Join(aReturn, vbNewLine), "table", , True)

End Function

Function Tag(ByVal sValue As String, ByVal sTag As String, _
    Optional ByVal sAttr As String = "", Optional ByVal bIndent As Boolean = False) As String

    Dim sReturn As String

    If Len(sAttr) > 0 Then
        sAttr = Space(1) & sAttr
    End If

    If bIndent Then
        sValue = vbTab & Replace(sValue, vbNewLine, vbNewLine & vbTab)
        sReturn = "<" & sTag & sAttr & ">" & vbNewLine & sValue & vbNewLine & "</" & sTag & ">"
    Else
        sReturn = "<" & sTag & sAttr & ">" & sValue & "</" & sTag & ">"
    End If

    Tag = sReturn

End Function