none
Word Document TableCell Style RRS feed

  • Question

  • I am creating a Word document using the Open XML SDK. I am generating tables in this Word document and would like to apply some styles.

    Prior to using the Open XML SDK, we were using PowerShell's Word.Application ComObject to generate tables and apply styles. Styles were applied to on the table elements and another style to individual cells.

    I am attempting to replicate that same approach using the SDK in C#.

    The following code successfully applies the Style to the table.

    TableProperties tblPr = new TableProperties();
    TableStyle tblStyle = new TableStyle();
    tblStyle.Val = "TableGrid";
    tblPr.Append(tblStyle);
    table.Append(tblPr);        

    From here, I am generating each cell using the SDK's TableCell class. This works great, except I cannot apply a style. I am trying to mimic the approach above but for an individual cell. Rather than TableProperties, I am using TableCellProperties. Please note that the style being used is not standard, "TableText Col Head". I have verified I am successfully making this style available in the code via the following.

    foreach (Style templateStyle in wordTemplate.MainDocumentPart.StyleDefinitionsPart.Styles.Elements<Style>())
    {
        string s = templateStyle.StyleName.Val;
        bool addStyle = (s.Equals("Table Grid")) || (s.Equals("TableText Col Head")) || (s.Equals("TableText"));
        if (addStyle)
        {
            part.Styles.Append(templateStyle.CloneNode(true));
        }                            
    }

    This ensures that the value "TableText Col Head" is available to be applied. The code that fails to apply this style to the TableCell object is below.

    var tc = new TableCell();
    TableCellProperties tblCellPr = new TableCellProperties();
    TableStyle newStyle = new TableStyle();
    newStyle.Val = "TableText Col Head";
    tblCellPr.Append(newStyle);
    tc.Append(tblCellPr);


    I couldn't find any examples online of applying a TableStyle object onto a TableCellProperties object.

    Any insight would be greatly appreciated. Thank you.




    Friday, August 25, 2017 6:48 PM

Answers

  • Terry, thanks for the response. I realized I was trying to apply a Table style onto a cell and that's not how the SDK works. When I opened up Word and reviewed the style I noticed it was of type "Paragraph". Searching into this further I was able to find some code examples of applying a ParagraphProperty.

    For those who discover this post later with a similar dilemma, hopefully, the details below will help you.

    By default, the SDK does not import the default Word styles that you see when you open it as a desktop application. You must reference a template and add those styles. For example, the following code makes the style available to the current document and saves the StyleId as a variable which is used to apply the style.

    public TableProperties getDefaultTableStyle(WordprocessingDocument document)
    {
        var doc = document.MainDocumentPart.Document;
    
        StyleDefinitionsPart part = doc.MainDocumentPart.StyleDefinitionsPart;
        if (part == null)
        {
            part = AddStylesPartToPackage(document);
        }
    
    	using (WordprocessingDocument wordTemplate = WordprocessingDocument.Open(@"c:\sample.dotx", false))
    	{
    		foreach (Style templateStyle in wordTemplate.MainDocumentPart.StyleDefinitionsPart.Styles.Elements<Style>())
    		{
    			string styleValue = templateStyle.StyleName.Val;
    			bool addStyle = styleValue.Equals("Table Grid");
    			if (addStyle)
    			{
    				part.Styles.Append(templateStyle.CloneNode(true));
    				cellStyleId = templateStyle.StyleId;
    			}
    		}
    	}
    }

    The following code applies the style to a header cell.

    var tc = new TableCell();
    tcPr = new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" });
    tc.Append(tcPr);
    
    string headerName = "Example Header";
    Paragraph headerParagraph = tc.AppendChild(new Paragraph());
    Run headerRun = headerParagraph.AppendChild(new Run());
    if (headerParagraph.Elements<ParagraphProperties>().Count() == 0)
    {
        headerParagraph.PrependChild<ParagraphProperties>(new ParagraphProperties());
    }
    ParagraphProperties pPr = headerParagraph.Elements<ParagraphProperties>().First();
    pPr.ParagraphStyleId = new ParagraphStyleId() { Val = cellStyleId };
    headerRun.Append(new Text(headerName));
    headerRow.Append(tc);

    Hope this helps.


    • Edited by Jonathan Vogel Tuesday, August 29, 2017 1:09 PM Corrected variable name in code
    • Marked as answer by Jonathan Vogel Wednesday, August 30, 2017 6:03 PM
    Tuesday, August 29, 2017 1:08 PM

All replies

  • Hi Jonathan Vogel,

    What do you mean applying a TableStyle object onto a TableCellProperties object? Could you share a screenshot to explain what's your excepted result? I also suggest you share your whole code how to create the table so we could try to reproduce your issue. You could share  screenshot via One Drive and put link here.

    Best Regards,

    Terry


    Tuesday, August 29, 2017 1:03 AM
  • Terry, thanks for the response. I realized I was trying to apply a Table style onto a cell and that's not how the SDK works. When I opened up Word and reviewed the style I noticed it was of type "Paragraph". Searching into this further I was able to find some code examples of applying a ParagraphProperty.

    For those who discover this post later with a similar dilemma, hopefully, the details below will help you.

    By default, the SDK does not import the default Word styles that you see when you open it as a desktop application. You must reference a template and add those styles. For example, the following code makes the style available to the current document and saves the StyleId as a variable which is used to apply the style.

    public TableProperties getDefaultTableStyle(WordprocessingDocument document)
    {
        var doc = document.MainDocumentPart.Document;
    
        StyleDefinitionsPart part = doc.MainDocumentPart.StyleDefinitionsPart;
        if (part == null)
        {
            part = AddStylesPartToPackage(document);
        }
    
    	using (WordprocessingDocument wordTemplate = WordprocessingDocument.Open(@"c:\sample.dotx", false))
    	{
    		foreach (Style templateStyle in wordTemplate.MainDocumentPart.StyleDefinitionsPart.Styles.Elements<Style>())
    		{
    			string styleValue = templateStyle.StyleName.Val;
    			bool addStyle = styleValue.Equals("Table Grid");
    			if (addStyle)
    			{
    				part.Styles.Append(templateStyle.CloneNode(true));
    				cellStyleId = templateStyle.StyleId;
    			}
    		}
    	}
    }

    The following code applies the style to a header cell.

    var tc = new TableCell();
    tcPr = new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" });
    tc.Append(tcPr);
    
    string headerName = "Example Header";
    Paragraph headerParagraph = tc.AppendChild(new Paragraph());
    Run headerRun = headerParagraph.AppendChild(new Run());
    if (headerParagraph.Elements<ParagraphProperties>().Count() == 0)
    {
        headerParagraph.PrependChild<ParagraphProperties>(new ParagraphProperties());
    }
    ParagraphProperties pPr = headerParagraph.Elements<ParagraphProperties>().First();
    pPr.ParagraphStyleId = new ParagraphStyleId() { Val = cellStyleId };
    headerRun.Append(new Text(headerName));
    headerRow.Append(tc);

    Hope this helps.


    • Edited by Jonathan Vogel Tuesday, August 29, 2017 1:09 PM Corrected variable name in code
    • Marked as answer by Jonathan Vogel Wednesday, August 30, 2017 6:03 PM
    Tuesday, August 29, 2017 1:08 PM
  • Hi Jonathan Vogel,

    I'm glad to hear that you have solved your issue. I would suggest you mark your solution as answer to help other use this forum efficiently. Thanks for your understanding.

    Best Regards,

    Terry

    Wednesday, August 30, 2017 7:50 AM