Mar*_*ark 9 html parsing vba html-parsing
我有一个包含html的单元格的Excel表格.如何将它们批量转换为纯文本?目前有这么多无用的标签和风格.我想从头开始编写它,但如果我能得到明文,那将会容易得多.
我可以编写一个脚本来将PHP转换为PHP中的纯文本,这样如果您无法想到VBA中的解决方案,那么您可以了解我如何将单元格数据传递到网站并检索数据.
Tim*_*ams 18
设置对"Microsoft HTML对象库"的引用.
Function HtmlToText(sHTML) As String
Dim oDoc As HTMLDocument
Set oDoc = New HTMLDocument
oDoc.body.innerHTML = sHTML
HtmlToText = oDoc.body.innerText
End Function
Run Code Online (Sandbox Code Playgroud)
蒂姆
一种非常简单的提取文本的方法是逐个字符扫描HTML,将尖括号外的字符累加成一个新的字符串。
Function StripTags(ByVal html As String) As String
Dim text As String
Dim accumulating As Boolean
Dim n As Integer
Dim c As String
text = ""
accumulating = True
n = 1
Do While n <= Len(html)
c = Mid(html, n, 1)
If c = "<" Then
accumulating = False
ElseIf c = ">" Then
accumulating = True
Else
If accumulating Then
text = text & c
End If
End If
n = n + 1
Loop
StripTags = text
End Function
Run Code Online (Sandbox Code Playgroud)
这可能会留下很多无关的空白,但它有助于删除标签。
蒂姆的解决方案很棒,效果非常好。
\n\nI\xc2\xb4d 喜欢贡献:使用此代码在运行时添加“Microsoft HTML 对象库”:
\n\nSet ID = ThisWorkbook.VBProject.References\nID.AddFromGuid "{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}", 2, 5\nRun Code Online (Sandbox Code Playgroud)\n\n它适用于 Windows XP 和 Windows 7。
\n