0

I'm trying to apply a table style to a table in openxml word.

I've looked at several examples, but I'm stumped on why the tablestyle is not showing up... I've checked the other posts like Apply a TableStyle to a Word Table

This is the code:

public Table CreateTable(string tableStyle, Headings headingsList)
{
    Table table = new Table();

    // Set the Style
    TableProperties tblProps = new TableProperties();
    TableStyle tblStyle = new TableStyle() { Val = "LightGrid-Accent4" };
    TableWidth tblWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct};

    // TODO: Requires a class and remove hardcoding
    TableLook tblLook = new TableLook() { FirstRow = true, LastRow = false, FirstColumn = false, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true };

    tblProps.TableStyle = tblStyle;
    tblProps.TableWidth = tblWidth;
    table.Append(tblProps);

    TableRow tr = new TableRow();
    foreach (Heading heading in headingsList)
    {
        TableCell tc = new TableCell();
        TableCellWidth tcw = new TableCellWidth() { Width = heading.Width.ToString(), Type = TableWidthUnitValues.Pct };
        tc.Append(tcw);

        tc.Append(new Paragraph(new Run(new Text(heading.Title))));
        tr.Append(tc);
    }
    table.Append(tr);

    return table;
}

I've looked at the OpenXML tool and had a look at the generated document which also looks fine. In the document, however, the table isn't formatted.

Any ideas would be appreciated.

Community
  • 1
  • 1
Rendition
  • 494
  • 2
  • 12
  • 1
    Are you creating a brand new file or updating an existing file? It could be that the style you are referring to doesn't exist within the document - see [my answer here](http://stackoverflow.com/questions/25056927/unable-to-use-existing-paragraph-styles-in-open-xml/25058393#25058393) for more details. – petelids Mar 10 '17 at 12:20
  • Thanks. I'm updating an exiting file. The table style I was applying was an existing one. I used the id from the open xml tool (reflect code) so it does exist. Interestingly when I created a custom style it found it and applied it. I'm still investigating why the built-in styles don't seem to work. – Rendition Mar 10 '17 at 20:26

0 Answers0