Can Excel Interpret a Cell as HTML

Can Excel interpret a cell as HTML?

Unfortunately the answer is no.

Excel has two HTML options:

  • Open a HTML file, which will sort of render the HTML, sort of, but won't contain any actual HTML in cells
  • Store HTML in cells, but as unformatted text.

You could, maybe possibly, come up with a macro that lets you enter HTML into a cell, then saves that HTML as a document, opens it up in another instance of Excel, then grabs that formatted HTML and places it in the original document; that way you would have two columns, one with the HTML, and one with the output. It would be very unsightly though. Don't do it :0)

HTML Text with tags to formatted text in an Excel cell

Yes it is possible. In fact let Internet Explorer do the dirty work for you.

MY ASSUMPTIONS

  1. I am assuming that the html text is in Cell A1 of Sheet1. You can also use a variable instead.
  2. 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

Sample Image

How to read/interpret html tags in Excel within a single cell?

How about just using the HTML file class?

Let's say your sample strings are in Range A1:A6 on Worksheets("Sheet1"):

Sub test()
Dim i As Integer

For i = 1 To 6 'Rows 1 to 6 hold your HTML formatted values

With CreateObject("htmlfile")
.Open
.write Worksheets("Sheet1").Range("A" & i).Value
.Close
Worksheets("Sheet1").Range("B" & i).Value = .body.outerText
End With

Next i
End Sub

Any way to convert excel cell value in to html format?

You can use this method to get whether each character is bold or not.

A good strategy I think is to use a StringBuilder to build your HTML:

    StringBuilder html = new StringBuilder();
for (int index = 1; index <= cell.Text.ToString().Length; index++)
{
//cell here is a Range object
Characters ch = cell.get_Characters(index, 1);
bool bold = (bool) ch.Font.Bold;
if(bold){
if (html.Length == 0)
html.Append("<b>");
html.Append(ch.Text);
}
}
if (html.Length !=0) html.Append("</b>")

That strategy is only for your text with only one bold word. (Just figure out an algorithm that will close open tags and make a new StringBuilder when a non-bold character is read or when the entire cell has been read)

Format HTML table cell so that Excel formats as text?

You can apply formatting to the cells for numbers, text, dates, etc.

See my previous answer on this: HTML to Excel: How can tell Excel to treat columns as numbers?

(adjusted snippet)

If you add a CSS Class to your page:

.num {
mso-number-format:General;
}
.text{
mso-number-format:"\@";/*force text*/
}

And slap those classes on your TD's, does it work?

<td class="num">34</td>
<td class="num">17.0</td>
<td class="text">067</td>

Is it possible to get a specific cell value from an Excel spreadsheet using JavaScript?

You can convert your Excel table to an *.csv file!

You can read the *.csv file in Javascript with the following code (I copied it from this thread: How to read data From *.CSV file using javascript?):

$(document).ready(function() {
$.ajax({
type: "GET",
url: "data.txt",
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
var record_num = 5; // or however many elements there are in each row
var allTextLines = allText.split(/\r\n|\n/);
var entries = allTextLines[0].split(',');
var lines = [];

var headings = entries.splice(0,record_num);
while (entries.length>0) {
var tarr = [];
for (var j=0; j<record_num; j++) {
tarr.push(headings[j]+":"+entries.shift());
}
lines.push(tarr);
}
// alert(lines);

}

How to format excel cell 'text' when opening an HTML content

I have found the answer for changing in html page. Please check the link.
Make <td> with CSS style mso-number-format:"\@";

how to convert a column of text with html tags to formatted text in vba in excel

Your code is working just for the first line because you are getting and setting only the first line :

'get the A1 cell value
.document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value
'set the B1 cell value
ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("B1")

To apply your code for all the lines you have to execute it inside a loop.

So your code becomes :

Sub Sample()

Dim Ie As Object

'get the last row filled
lastRow = Sheets("Sheet1").Range("A" & Sheets("Sheet1").Rows.Count).End(xlUp).Row
'loop to apply the code for all the lines filled
For Row = 1 To lastRow
Set Ie = CreateObject("InternetExplorer.Application")
With Ie
.Visible = False
.Navigate "about:blank"
.document.body.InnerHTML = Sheets("Sheet1").Range("A" & Row).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("B" & Row)
'update to cell you want converted HTML pasted in
.Quit
End With
Set Ie = Nothing
Next

End Sub



Related Topics



Leave a reply



Submit