Как добавить строки в таблицу в word (OpenXML)?

Сформировал примерный документ в ворде. Добавил шапку таблицы и несколько строк. Получил c# код при помощи проги OpenXML SDK2.0, в коде удалил созданные строки из таблицы кроме строки с шапкой.

Нужный вид таблицы:

Код:

//TableRow[] tableRow = new TableRow[selectedStudent.Questions.Count];

        for (int i = 0; i < selectedStudent.Questions.Count; i++)
        {
            TableRow tableRow2 = new TableRow() { RsidTableRowAddition = "00954C83", RsidTableRowProperties = "00174B9E", ParagraphId = "199B0298", TextId = "77777777" };

            TableCell tableCell5 = new TableCell();

            TableCellProperties tableCellProperties5 = new TableCellProperties();
            TableCellWidth tableCellWidth5 = new TableCellWidth() { Width = "7508", Type = TableWidthUnitValues.Dxa };

            tableCellProperties5.Append(tableCellWidth5);

            Paragraph paragraph5 = new Paragraph() { RsidParagraphMarkRevision = "00954C83", RsidParagraphAddition = "00954C83", RsidParagraphProperties = "00F26699", RsidRunAdditionDefault = "00F26699", ParagraphId = "325F4E45", TextId = "5F466EE7" };

            ParagraphProperties paragraphProperties5 = new ParagraphProperties();
            Justification justification5 = new Justification() { Val = JustificationValues.Center };

            ParagraphMarkRunProperties paragraphMarkRunProperties5 = new ParagraphMarkRunProperties();
            RunFonts runFonts12 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
            FontSize fontSize12 = new FontSize() { Val = "24" };
            FontSizeComplexScript fontSizeComplexScript12 = new FontSizeComplexScript() { Val = "24" };

            paragraphMarkRunProperties5.Append(runFonts12);
            paragraphMarkRunProperties5.Append(fontSize12);
            paragraphMarkRunProperties5.Append(fontSizeComplexScript12);

            paragraphProperties5.Append(justification5);
            paragraphProperties5.Append(paragraphMarkRunProperties5);

            Run run8 = new Run();

            RunProperties runProperties8 = new RunProperties();
            RunFonts runFonts13 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
            FontSize fontSize13 = new FontSize() { Val = "24" };
            FontSizeComplexScript fontSizeComplexScript13 = new FontSizeComplexScript() { Val = "24" };

            runProperties8.Append(runFonts13);
            runProperties8.Append(fontSize13);
            runProperties8.Append(fontSizeComplexScript13);
            Text text5 = new Text();
            text5.Text = selectedStudent.Questions.First().Question;

            run8.Append(runProperties8);
            run8.Append(text5);

            paragraph5.Append(paragraphProperties5);
            paragraph5.Append(run8);

            tableCell5.Append(tableCellProperties5);
            tableCell5.Append(paragraph5);

            TableCell tableCell6 = new TableCell();

            TableCellProperties tableCellProperties6 = new TableCellProperties();
            TableCellWidth tableCellWidth6 = new TableCellWidth() { Width = "2336", Type = TableWidthUnitValues.Dxa };
            Shading shading1 = new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "00B050" };

            tableCellProperties6.Append(tableCellWidth6);
            tableCellProperties6.Append(shading1);

            Paragraph paragraph6 = new Paragraph() { RsidParagraphMarkRevision = "00954C83", RsidParagraphAddition = "00954C83", RsidParagraphProperties = "00F26699", RsidRunAdditionDefault = "00954C83", ParagraphId = "0D4A70E1", TextId = "77777777" };

            ParagraphProperties paragraphProperties6 = new ParagraphProperties();
            Justification justification6 = new Justification() { Val = JustificationValues.Center };

            ParagraphMarkRunProperties paragraphMarkRunProperties6 = new ParagraphMarkRunProperties();
            RunFonts runFonts14 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
            FontSize fontSize14 = new FontSize() { Val = "24" };
            FontSizeComplexScript fontSizeComplexScript14 = new FontSizeComplexScript() { Val = "24" };

            paragraphMarkRunProperties6.Append(runFonts14);
            paragraphMarkRunProperties6.Append(fontSize14);
            paragraphMarkRunProperties6.Append(fontSizeComplexScript14);

            paragraphProperties6.Append(justification6);
            paragraphProperties6.Append(paragraphMarkRunProperties6);

            paragraph6.Append(paragraphProperties6);

            tableCell6.Append(tableCellProperties6);
            tableCell6.Append(paragraph6);
            table1.Append(tableRow2);
        }

Если создание TableRow и всего остального выношу из цикла, то после компиляции строка появляется в нужном виде и месте в вордвоском документе. Собственно, в этом вопрос, как динамически добавлять строки в таблицу?


Ответы (1 шт):

Автор решения: Нетерпеливый игуан

Возможно, перед table1.Append(tableRow2); вы пропустили tableRow2.Append(tableCell6);

→ Ссылка