可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is there a way to take HTML and import it to excel so that it is formatted as rich text (preferably by using VBA)? Basically, when I paste to an Excel cell, I'm looking to turn this:
This is a test. Will this text be bold or italic
into this:
This is a test. Will this text be bold or italic
回答1:
Yes it is possible :) In fact let Internet Explorer do the dirty work for you ;)
TRIED AND TESTED
MY ASSUMPTIONS
- I am assuming that the html text is in Cell A1 of Sheet1. You can also use a variable instead.
- If you have a column full of html values, then simply put the below code in a loop
CODE
Sub Sample() Dim Ie As Object Set Ie = CreateObject("InternetExplorer.Application") With Ie .Visible = False .Navigate "about:blank" .document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value .document.body.createtextrange.execCommand "Copy" ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("A1") .Quit End With End Sub
SNAPSHOT
HTH
Sid
回答2:
You can copy the HTML code to the clipboard and paste special it back as Unicode text. Excel will render the HTML in the cell. Check out this post http://www.dailydoseofexcel.com/archives/2005/02/23/html-in-cells-ii/
The relevant macro code from the post:
Private Sub Worksheet_Change(ByVal Target As Range) Dim objData As DataObject Dim sHTML As String Dim sSelAdd As String Application.EnableEvents = False If Target.Cells.Count = 1 Then If LCase(Left(Target.Text, 6)) = "" Then Set objData = New DataObject sHTML = Target.Text objData.SetText sHTML objData.PutInClipboard sSelAdd = Selection.Address Target.Select Me.PasteSpecial "Unicode Text" Me.Range(sSelAdd).Select End If End If Application.EnableEvents = True End Sub
回答3:
If the IE example doesn't work use this one. Anyway this should be faster than starting up an instance of IE.
Here is a complete solution based on
http://www.dailydoseofexcel.com/archives/2005/02/23/html-in-cells-ii/
Note, if your innerHTML is all numbers eg '12345', HTML formatting dosen't fully work in excel as it treats number differently? but add a character eg a trailing space at the end eg. 12345 + "& nbsp;" formats ok.
Sub test() Cells(1, 1).Value = "1a" & _ "234" Dim rng As Range Set rng = ActiveSheet.Cells(1, 1) Worksheet_Change rng, ActiveSheet End Sub Private Sub Worksheet_Change(ByVal Target As Range, ByVal sht As Worksheet) Dim objData As DataObject ' Set a reference to MS Forms 2.0 Dim sHTML As String Dim sSelAdd As String Application.EnableEvents = False If Target.Cells.Count = 1 Then Set objData = New DataObject sHTML = Target.Text objData.SetText sHTML objData.PutInClipboard Target.Select sht.PasteSpecial Format:="Unicode Text" End If Application.EnableEvents = True End Sub
回答4:
I know this thread is ancient, but after assigning the innerHTML, ExecWB worked for me:
.ExecWB 17, 0 'Select all contents in browser .ExecWB 12, 2 'Copy them
And then just paste the contents into Excel. Since these methods are prone to runtime errors, but work fine after one or two tries in debug mode, you might have to tell Excel to try again if it runs into an error. I solved this by adding this error handler to the sub, and it works fine:
Sub ApplyHTML() On Error GoTo ErrorHandler ... Exit Sub ErrorHandler: Resume 'I.e. re-run the line of code that caused the error Exit Sub End Sub
回答5:
I ran into the same error that BornToCode first identified in the comments of the original solution. Being unfamiliar with Excel and VBA it took me a second to figure out how to implement tiQU's solution. So I'm posting it as a "For Dummies" solution below
- First enable developer mode in Excel: Link
- Select the Developer Tab > Visual Basic
- Click View > Code
- Paste the code below updating the lines that require cell references to be correct.
- Click the Green Run Arrow or press F5
Sub Sample() Dim Ie As Object Set Ie = CreateObject("InternetExplorer.Application") With Ie .Visible = False .Navigate "about:blank" .document.body.InnerHTML = Sheets("Sheet1").Range("I2").Value 'update to the cell that contains HTML you want converted .ExecWB 17, 0 'Select all contents in browser .ExecWB 12, 2 'Copy them ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("J2") 'update to cell you want converted HTML pasted in .Quit End With End Sub
回答6:
You all have valid solutions, and with a handful of them you can implement exactly this.
tools need are regular expressions, linq, a search engine, vb.net or C# and the internet.
Search for "html table to dataset". Then search "dataset to excel without excel installed".
I think with those terms you might be able to put it together. ;)
But here is some of the solution.
Using sr As StreamReader = New StreamReader(fileName, Encoding.UTF8) result = sr.ReadToEnd() End Using result = result.Substring(result.IndexOf("") sb.AppendLine("") sb.AppendLine("") sb.AppendLine(" "") sb.AppendLine("Title") sb.AppendLine("") sb.AppendLine("") sb.Append(result) sb.AppendLine("") sb.AppendLine("") result = sb.ToString() File.Move(fileName, System.IO.Path.GetFileNameWithoutExtension(fileName) + ".txt") Dim ds As DataSet = GetTableAsDataSet.ConvertHTMLTablesToDataSet(result) If (DataSetToExcel.WriteXLSFile(fileName, ds) = True) Then
http://www.dotnetfunda.com/articles/show/51/convert-html-tables-to-a-dataset
http://www.codeproject.com/Tips/313731/How-to-convert-DataSet-to-Excel-workbook-xls-using
For sake of simplicity my input file is a html table that maps to excel right giving the right view. But a view is all it is. so i read it in strip off the meta styling crap and wrap it in valid html feed it in to get the data set and write the data set out. enjoy.
I think the regular expression could help you with gathering the other part of the html...
== ]*>(.*?)
Credits go to the authors of said code. I just put it together.