如何将EXCEL中的单元格内容转换为HTML代码?

时间:2013-08-21 09:23:30

标签: excel cell

基本上我想创建宏来获取单元格内容并放入HTML段落标记,但是如果单元格包含新行符号以将新行放入新标记中。有人可以帮我做或提供给我吗?提前谢谢。

不正确:

<html>
<head>
    <title>example</title>
</head>
<body>
    <p>1
       2
       3</p>
</body>
</html>

正确:

<html>
<head>
    <title>example</title>
</head>
<body> 
    <p>1</p>
    <p>2</p>
    <p>3</p>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

Sub lf2html()

Dim i, chrCount As Integer
Dim htmlTxt As String
Dim cell As Range
Dim SetCellStart
Dim SetCellStop

SetCellStart = InputBox("StartCell ( f0r example: a1 ):", "Enter number!")
SetCellStop = InputBox("StopCell ( f0r example: a2 )", "Enter number!")

'MsgBox SetCellStart SetCellStop
For Each cell In Range(SetCellStart, SetCellStop)
    htmlTxt = "<html><head></head><body><p>"
        chrCount = cell.Characters.Count

        For i = 1 To chrCount
            With cell.Characters(i, 1)
                If (Asc(.Text) = 10) Then
                    htmlTxt = htmlTxt & "</p><p>"
                Else
                    htmlTxt = htmlTxt & .Text
                End If
            End With
        Next

    htmlTxt = htmlTxt & "</p></body></html>"
    cell.Value = htmlTxt
Next cell

End Sub