4

Trying to style a table using a predefined style but nothing is working. I've tried with a a newly created document and one created from a saved template. Using the SDK Productivity tool I can see the style is there in the template but it's not being applied. I've tried appended the style or setting it directly and neither seem to work.

    public static void CreateWordprocessingDocument(string fileName) {

        string[,] data = {
            {"Texas", "TX"},
            {"California", "CA"},
            {"New York", "NY"},
            {"Massachusetts", "MA"}
        };

        using (var wordDocument = WordprocessingDocument.Open(fileName, true)) {

            // We need to change the file type from template to document.
            wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document);

            var body = wordDocument.GetDocument().Body;

            Table table = new Table();

            TableProperties props = new TableProperties();
            TableStyle tableStyle = new TableStyle { Val = "Light Shading Accent 1" };
            props.TableStyle = tableStyle;
            //props.Append(tableStyle);
            table.AppendChild(props);

            for (var i = 0; i <= data.GetUpperBound(0); i++) {
                var tr = new TableRow();
                for (var j = 0; j <= data.GetUpperBound(1); j++) {
                    var tc = new TableCell();
                    tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
                    tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto }));
                    tr.Append(tc);
                }
                table.Append(tr);
            }
            body.Append(table);
            wordDocument.GetDocument().Save();
        }
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Brad Patton
  • 4,007
  • 4
  • 34
  • 44
  • I have the exact same code except for this line. Not sure if that would make a difference. table.AppendChild( tPr ); – Tarveen May 13 '15 at 19:44
  • The cast should not make a difference (just tested to confirm). Are your tables appearing styled? – Brad Patton May 13 '15 at 19:53
  • Yes, they do. I can apply the style manually to the table too. – Tarveen May 13 '15 at 19:57
  • I can apply most of the properties (like border, color, etc) manually as well and that works fine. But I creating a lot of tables and would like to be able to use a predefined style to both simplify the code and allow them to be changed easier. – Brad Patton May 13 '15 at 20:02
  • What do you see in the xml file with the productivity tools? I see something like .... – Tarveen May 13 '15 at 20:23
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 14 '15 at 04:57

1 Answers1

7

Finally figured it out. I was using the style name and not the style id. So the line where the style is declared should look like:

TableStyle tableStyle = new TableStyle { Val = "LightShading-Accent1" };
Brad Patton
  • 4,007
  • 4
  • 34
  • 44