0

I am trying to generate excel file and my data is in the form of HTML string. The html string has hyperlinks(anchor tags) in them. the column 1 has a hyperlink and some text after that. however, when viewed in Excel, it makes all the text in that cell as hyperlink. Is it possible to restrict that. Open the below file in browser and it works fine. When opened in excel, it hyperlinks everything in that cell.

<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<td>
<p><a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
This is a sample text</p>
</td>
<td>column 2</td>
</tr>
</table>
</body>
</html>

enter image description here

enter code here

enter image description here

dontnetnewbie
  • 159
  • 4
  • 15
  • 1
    How are you bringing the HTML into Excel? Programmatically VBA? Or just opening the file with File > Open? Why would you generate an HTML file to open with anything but a browser? More information please. – ashleedawg Dec 06 '17 at 19:47
  • Just now saw this message. Thanks for your response. Well, My data is in list of jave pojos. I am iterating the list and putting each member value in the pojo into a html cell and for each object in the list. And the entire string is convered inside html tag. Baiscally, I am generating a huge html table string. After that I write that to a xls file and open this document in microsfot excel. – dontnetnewbie Dec 08 '17 at 20:57

1 Answers1

0

Are you just opening the .html file directly in Excel? If so, Excel is doing some conversion/interpreting work that we can't exactly see. It will be trial and error if this is the process you're using.

It is likely interpreting that <td> all as one cell, so it's styling the entire cell with the hyperlink. If you put "This is a sample text" in its own <td> so it's in a different cell, that is one way to fix it. You'll have three columns instead of two, but visually it would accomplish what you want.

If you want it in the same cell, I'm not certain if that is possible based on how Excel is interpreting it. I tried all sorts of things (putting the sample text in its own span or p tags, etc) but those placed it in a different row, but it seems like if it goes in the same cell as the link, Excel is going to style the entire cell that way.

If you use a method that gives you more control over the conversion (VBA, etc) then you may have more options.

B. Scott
  • 166
  • 1
  • 5
  • Thanks for your reply. I am trying to generate an excel document and my data is in html format with table tr and tds tags. The end user enters a comment in our application using an HTML editor. Or he could copy a text from any website and paste it into our HTML editor and saves. So this data goes into a column in excel. So basically, I am generating a huge HTML string with table tags and then saving that into a HTML or xls file and opening that from Microsoft Excel. Can you let me know about the VBA option and any sample code to handle this? I can try and incorporate that as well. – dontnetnewbie Dec 06 '17 at 20:20
  • The VBA side of things would be out of the scope of what I know personally, but you might be able to find some things online. For instance here's a stackoverflow discussion about it: https://stackoverflow.com/questions/3206775/export-html-table-to-excel-using-jquery-or-java You can continue to do it the way you are, opening it directly in Excel, but it's just going to be messy and require a little work to get it to display the way you want or you might run into limitations with the conversion (styling, layout, etc) since Excel is doing the conversion work for you in this case. – B. Scott Dec 06 '17 at 20:39