OpenXML - format HTML Text in table Cell in C# RRS feed

  • Question

  • Hi,

    I am using OpenXML , to generate Word Document using Visual Studio 2013 - C#.

    In TableCell content, I want to append the rendered(Formatted) HTML text.

    But while document, its displaying the cell content with tags.

    For e.g. the HTML text is  ---

    <p>Hello. Today, we will be learning about the morphology and physiology of Bacteria. At the end of this lecture, students would have learnt <o:p></o:p></p><p><span style="color:red">ANCHOR TO PRESS PLAY<o:p></o:p></span></p><p>1. The morphology&nbsp; &amp; classification of bacteria <o:p></o:p></p><p>2. Components of a bacterial cell<o:p></o:p></p><p>3. Staining and observing bacteria under a microscope<o:p></o:p></p><p>4. Physiological conditions required for growth of bacteria<o:p></o:p></p><p>5. Growth phases of bacteria<o:p></o:p></p><p>6. Classification of bacteria based on temperature requirements, and <o:p></o:p></p><p>7.&nbsp; Cell-wall deficient bacteria</p><p><span style="color: red; font-family: Calibri, sans-serif; font-size: 11pt; line-height: 115%;">ANCHOR TO PRESS NEXT</span></p>

    And I am assigning the text to table cell as,

    TableRow tr = new TableRow();
    TableCell tc2 = new TableCell();

    //Val2 consist of HTML Text
    tc2.Append(new Paragraph(new Run(new Text(WebUtility.HtmlDecode(val2)))));

     // Append the table cell to the table row.

    However after document creation, it display's the HTML text in cell as is, And I want to display the formatted text, i.e. for a text it should display text bold, or with style as specified in HTML Text.

    Kindly Let me know the correct way.


    Thursday, December 17, 2015 1:41 PM


  • Hi Mahajan,

    If you want to insert html string into word document by OpenXML, I suggest you try the code below:

                using (WordprocessingDocument doc = WordprocessingDocument.Open(@"D:\Html.docx", true))
                    string altChunkId = "myId";
                    MainDocumentPart mainDocPart = doc.MainDocumentPart;
                    var run = new Run(new Text("test"));
                    var p = new Paragraph(new ParagraphProperties(
                         new Justification() { Val = JustificationValues.Center }),
                    var body = mainDocPart.Document.Body;
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(@"<html><head></head><body><h1 style=""color:red"">HELLO</h1></body></html>"));      
                    // Uncomment the following line to create an invalid word document.
                    // MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<h1>HELLO</h1>"));
                    // Create alternative format import part.
                    AlternativeFormatImportPart formatImportPart =
                          AlternativeFormatImportPartType.Html, altChunkId);
                    //ms.Seek(0, SeekOrigin.Begin);
                    // Feed HTML data into format import part (chunk).
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = altChunkId;

    Best Regards,


    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.

    Friday, December 18, 2015 5:24 AM