2010-06-28 70 views
2

我想要去除所有的HTML代碼的文本值的左,右走:Excel公式剝離HTML

我有這個...
<option value="41">GECommonUI</option>

我想這個.. 。
GECommonUI

+0

在VBA或工作表函數中? – 2010-06-28 18:30:57

+0

作爲Excel工作表功能不是vba – kacalapy 2010-06-29 13:50:13

回答

0

的傢伙,這是一組簡單的字符串函數。因爲我對excel字符串函數的速度不夠快,這對我來說是個痛苦......

在程序設計語言中,這將會像 mid(start,length)那樣得到整個值html字符串 - 訣竅是獲取長度作爲開始位置減去結束html長度的長度)

mid(pos(「>」)+ 2,len(string)-pos(「>」))

1
在Excel

選擇所有
打開替換(Ctrl + F鍵)
取代<*>
沒事

或用perl正則表達式
$行= 「這是一些文本與HTML和文字」;
$ line =〜s/<(。*?)> // gi;

+0

替換不工作,它的文字。和即時消息不使用Perl。 – kacalapy 2010-06-28 18:40:53

+1

我不知道爲什麼我的上面的帖子不在<>之間顯示星號。替換接受通配符。不知道你的意思是字面意思。在您發佈的示例中勝出至少。 – prat 2010-06-28 18:55:49

+0

我不認爲這是OP所期待的,但謝謝。我瞭解到替換接受通配符!誰知道? – EroSan 2011-03-07 15:37:02

1

設置對MS Forms 2.0的引用以使用DataObject對象。

Public Function StripHTML(sInput As String) As String 

    Dim rTemp As Range 
    Dim oData As DataObject 

    Set oData = New DataObject 
    oData.SetText "<html><style>br{mso-data-placement:same-cell;}</style>" & sInput & "</html>" 
    oData.PutInClipboard 

    Set rTemp = Workbooks.Add.Worksheets(1).Range("a1") 
    rTemp.Parent.PasteSpecial "Unicode Text" 
    StripHTML = rTemp.Text 

    rTemp.Parent.Parent.Close False 
    Set rTemp = Nothing 
    Set oData = Nothing 

End Function 

有關更多信息,請參閱http://www.dailydoseofexcel.com/archives/2005/02/23/html-in-cells-ii/

+0

+1,但我不認爲這正是OP所期待的。我認爲@kacalapy想要去掉HTML,而不是將其轉換爲有效的格式。 – technomalogical 2010-06-28 20:06:36

+0

將其轉換爲有效的格式只是一個副作用。因爲該函數返回Range對象的Text屬性,所以格式將不相關。但是,它會在轉換html標籤時將其刪除。 – 2010-06-28 20:33:28

1

您可以在下面的頁面上下載帶有工作用戶定義的函數= stripHTML()的Excel文件。該文件有一個工作示例和註釋。這將解決您的問題。請注意,您必須在此Excel文檔上允許宏才能使用此功能。

http://jfrancisconsulting.com/how-to-strip-html-tags-in-excel/

如何爲我的功能有什麼不同?

我發現的大多數函數在做全局查找和替換時都做了很好的工作,以刪除括號內的所有HTML標記。

但是,這會刪除某些換行符,這會導致最終結果看起來像是被搗碎的文本塊。我修改了一個現有的函數來添加回行空格,以便最終結果保持適當的換行符。

其次,全局HTML只替換括號中的屬性<>所有HTML特殊符號都將保留。防爆。而不是「&」符號&,您將剩下HTML版本&。爲了解決這個問題,我插入了87個替換行來將所有的HTML符號替換爲字符。

Function StripHTML(cell As Range) As String 

Dim RegEx As Object 
Set RegEx = CreateObject(「vbscript.regexp」) 
Dim sInput As String 
Dim sOut As String 
sInput = cell.Text 

sInput = Replace(sInput, 「\x0D\x0A」, Chr(10)) 
sInput = Replace(sInput, 「\x00″, Chr(10)) 

‘replace HTML breaks and end of paragraphs with line breaks 
sInput = Replace(sInput, 「</P>」, Chr(10) & Chr(10)) 
sInput = Replace(sInput, 「<BR>」, Chr(10)) 

‘replace bullets with dashes 
sInput = Replace(sInput, 「<li>」, 「-」) 

‘add back all of the special characters 
sInput = Replace(sInput, 「&ndash;」, 「–」) 
sInput = Replace(sInput, 「&mdash;」, 「—」) 
sInput = Replace(sInput, 「&iexcl;」, 「¡」) 
sInput = Replace(sInput, 「&iquest;」, 「¿」) 
sInput = Replace(sInput, 「&quot;」, 「」) 
sInput = Replace(sInput, 「&ldquo;」, 「「」) 
sInput = Replace(sInput, 「&rdquo;」, 「」」) 
sInput = Replace(sInput, 「」, 「‘」) 
sInput = Replace(sInput, 「&lsquo;」, 「‘」) 
sInput = Replace(sInput, 「&rsquo;」, 「’」) 
sInput = Replace(sInput, 「&laquo;」, 「«」) 
sInput = Replace(sInput, 「&raquo;」, 「»」) 
sInput = Replace(sInput, 「&nbsp;」, 」 「) 
sInput = Replace(sInput, 「&amp;」, 「&」) 
sInput = Replace(sInput, 「&cent;」, 「¢」) 
sInput = Replace(sInput, 「&copy;」, 「©」) 
sInput = Replace(sInput, 「&divide;」, 「÷」) 
sInput = Replace(sInput, 「&gt;」, 「>」) 
sInput = Replace(sInput, 「&lt;」, 「<」) 
sInput = Replace(sInput, 「&micro;」, 「µ」) 
sInput = Replace(sInput, 「&middot;」, 「·」) 
sInput = Replace(sInput, 「&para;」, 「¶」) 
sInput = Replace(sInput, 「&plusmn;」, 「±」) 
sInput = Replace(sInput, 「&euro;」, 「€」) 
sInput = Replace(sInput, 「&pound;」, 「£」) 
sInput = Replace(sInput, 「&reg;」, 「®」) 
sInput = Replace(sInput, 「&sect;」, 「§」) 
sInput = Replace(sInput, 「&trade;」, 「™」) 
sInput = Replace(sInput, 「&yen;」, 「¥」) 
sInput = Replace(sInput, 「&aacute;」, 「á」) 
sInput = Replace(sInput, 「&Aacute;」, 「Á」) 
sInput = Replace(sInput, 「&agrave;」, 「à」) 
sInput = Replace(sInput, 「&Agrave;」, 「À」) 
sInput = Replace(sInput, 「&acirc;」, 「â」) 
sInput = Replace(sInput, 「&Acirc;」, 「Â」) 
sInput = Replace(sInput, 「&aring;」, 「å」) 
sInput = Replace(sInput, 「&Aring;」, 「Å」) 
sInput = Replace(sInput, 「&atilde;」, 「ã」) 
sInput = Replace(sInput, 「&Atilde;」, 「Ã」) 
sInput = Replace(sInput, 「&auml;」, 「ä」) 
sInput = Replace(sInput, 「&Auml;」, 「Ä」) 
sInput = Replace(sInput, 「&aelig;」, 「æ」) 
sInput = Replace(sInput, 「&AElig;」, 「Æ」) 
sInput = Replace(sInput, 「&ccedil;」, 「ç」) 
sInput = Replace(sInput, 「&Ccedil;」, 「Ç」) 
sInput = Replace(sInput, 「&eacute;」, 「é」) 
sInput = Replace(sInput, 「&Eacute;」, 「É」) 
sInput = Replace(sInput, 「&egrave;」, 「è」) 
sInput = Replace(sInput, 「&Egrave;」, 「È」) 
sInput = Replace(sInput, 「&ecirc;」, 「ê」) 
sInput = Replace(sInput, 「&Ecirc;」, 「Ê」) 
sInput = Replace(sInput, 「&euml;」, 「ë」) 
sInput = Replace(sInput, 「&Euml;」, 「Ë」) 
sInput = Replace(sInput, 「&iacute;」, 「í」) 
sInput = Replace(sInput, 「&Iacute;」, 「Í」) 
sInput = Replace(sInput, 「&igrave;」, 「ì」) 
sInput = Replace(sInput, 「&Igrave;」, 「Ì」) 
sInput = Replace(sInput, 「&icirc;」, 「î」) 
sInput = Replace(sInput, 「&Icirc;」, 「Î」) 
sInput = Replace(sInput, 「&iuml;」, 「ï」) 
sInput = Replace(sInput, 「&Iuml;」, 「Ï」) 
sInput = Replace(sInput, 「&ntilde;」, 「ñ」) 
sInput = Replace(sInput, 「&Ntilde;」, 「Ñ」) 
sInput = Replace(sInput, 「&oacute;」, 「ó」) 
sInput = Replace(sInput, 「&Oacute;」, 「Ó」) 
sInput = Replace(sInput, 「&ograve;」, 「ò」) 
sInput = Replace(sInput, 「&Ograve;」, 「Ò」) 
sInput = Replace(sInput, 「&ocirc;」, 「ô」) 
sInput = Replace(sInput, 「&Ocirc;」, 「Ô」) 
sInput = Replace(sInput, 「&oslash;」, 「ø」) 
sInput = Replace(sInput, 「&Oslash;」, 「Ø」) 
sInput = Replace(sInput, 「&otilde;」, 「õ」) 
sInput = Replace(sInput, 「&Otilde;」, 「Õ」) 
sInput = Replace(sInput, 「&ouml;」, 「ö」) 
sInput = Replace(sInput, 「&Ouml;」, 「Ö」) 
sInput = Replace(sInput, 「&szlig;」, 「ß」) 
sInput = Replace(sInput, 「&uacute;」, 「ú」) 
sInput = Replace(sInput, 「&Uacute;」, 「Ú」) 
sInput = Replace(sInput, 「&ugrave;」, 「ù」) 
sInput = Replace(sInput, 「&Ugrave;」, 「Ù」) 
sInput = Replace(sInput, 「&ucirc;」, 「û」) 
sInput = Replace(sInput, 「&Ucirc;」, 「Û」) 
sInput = Replace(sInput, 「&uuml;」, 「ü」) 
sInput = Replace(sInput, 「&Uuml;」, 「Ü」) 
sInput = Replace(sInput, 「&yuml;」, 「ÿ」) 
sInput = Replace(sInput, 「」, 「´」) 
sInput = Replace(sInput, 「」, 「`」) 

‘replace all the remaining HTML Tags 
With RegEx 
.Global = True 
.IgnoreCase = True 
.MultiLine = True 
.Pattern = 「<[^>]+>」 ‘Regular Expression for HTML Tags. 

End With 
sOut = RegEx.Replace(sInput, 「」) 
StripHTML = sOut 
Set RegEx = Nothing 
End Function 
+0

-1不發佈您的功能。如果你的鏈接中斷,那麼知識就會丟失。 – DeanOC 2013-06-18 03:37:38

+0

增加了功能 – 2013-06-18 15:11:09

+1

+1對於添加功能:) – DeanOC 2013-06-18 21:02:27