将带有unicode的文本转换为HTML实体

时间:2017-10-16 04:49:05

标签: vba unicode html-entities

VBA中,如何将包含Unicode的文本转换为HTML实体?

EG。测试字符:èéâ?将转换为Test chars: èéâ👍

2 个答案:

答案 0 :(得分:2)

在Excel中,使用Unicode UTF-16存储字符。 “Thumbs up”字符(?)对应于Unicode字符U+1F44D, encoded as follows

  

以UTF-16(十六进制):0xD83D 0xDC4D(d83ddc4d)

     

以UTF-16(十进制):55357,56397

以下函数(和测试程序)应按预期转换:

Sub test()
    txt = String2Html("Test chars: èéâ" & ChrW(&HD83D) & ChrW(&HDC4D))
    debug.print txt ' -> Test chars: èéâ👍
End Sub



Function String2Html(strText As String) As String

Dim i As Integer
Dim strOut As String
Dim char As String
Dim char2 As String
Dim intCharCode As Integer
Dim intChar2Code As Integer
Dim unicode_cp As Long


For i = 1 To Len(strText)
    char = Mid(strText, i, 1)
    intCharCode = AscW(char)
    If (intCharCode And &HD800) = &HD800 Then
        i = i + 1
        char2 = Mid(strText, i, 1)
        intChar2Code = AscW(char2)
        unicode_cp = (intCharCode And &H3FF) * (2 ^ 10) + (intChar2Code And &H3FF)
        strOut = strOut & "&#x" & CStr((intCharCode And &H3C0) + 1) & Hex(unicode_cp) & ";"
    ElseIf intCharCode > 127 Then
        strOut = strOut & "&#x" & Hex(intCharCode) & ";"
    ElseIf intCharCode < 0 Then
        strOut = strOut & "&#x" & Hex(65536 + intCharCode) & ";"
    Else
        strOut = strOut & char
    End If
Next

String2Html = strOut

End Function

答案 1 :(得分:0)

  

要将Unicode转换为Asci(例如:æ转换为CaseProducts

&#230;
  

要将Asci转换为Unicode(例如: Public Function UnicodeToAscii(sText As String) As String Dim x As Long, sAscii As String, ascval As Long If Len(sText) = 0 Then Exit Function End If sAscii = "" For x = 1 To Len(sText) ascval = AscW(Mid(sText, x, 1)) If (ascval < 0) Then ascval = 65536 + ascval ' http://support.microsoft.com/kb/272138 End If sAscii = sAscii & "&#" & ascval & ";" Next UnicodeToAscii = sAscii End Function 到æ)

&#230;

我希望这会对某人有所帮助, 我从here

获得了这段代码