Share via


Render HTML inside Excel, using Open XML

Question

Friday, January 30, 2015 11:41 AM

Hello guys,

I am creating excel file using, Open XML.

The data will be coming from database, and can contain HTML tags.

e.g. <b>some sample <i style='color:red'> text </i></b>

I want the HTML to be rendered inside excel sheet, so that user can view well formatted excel..

Please, Help me out

All replies (2)

Monday, February 2, 2015 7:10 AM âś…Answered | 1 vote

Hi J_Prasanna,

I'm afraid that it's not possible with OpenXML SDK, but it's possible if you use Excel PIA along with Internet Explorer. Use the Internet Explorer COM object to render the HTML content, then copy the document, use the Paste method of the Worksheet object to paste the text with format.

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

The code is similar if you use managed project.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Saturday, February 7, 2015 7:57 PM

There isn't any EASY way (which I've always found frustrating!) but try this link:

http://html2openxml.codeplex.com/

Basically the code brute forces it's way through the html and translates to openXml. I've used this with both Word and Excel via OpenXML.

Paul