Repeat table rows in word document using OPEN XML SDK
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("formdemo.docx", true, new OpenSettings()))
{
IEnumerable<Table> tables =
myDoc.MainDocumentPart.Document.Body.Descendants<Table>();
foreach (Table t in tables)
{
// Get last row
TableRow theRow =
t.Elements<TableRow>().Last();
if (theRow.Descendants<TableCell>().First().InnerText.Contains("{{"))
{
int i = 2;
while (i > 0)
{
// Clone last row
TableRow rowCopy = (TableRow)theRow.CloneNode(true);
// Get all cells of last row
IEnumerable<TableCell> tcc =
rowCopy.Descendants<TableCell>();
// iterate through all cells of last row
foreach (TableCell tc in tcc)
{
if (string.IsNullOrEmpty(tc.InnerText)
== false)
{
//Paragraph p1 = tc.Descendants<Paragraph>().First();
//Paragraph p1Clone = (Paragraph)p1.CloneNode(true);
//Run r1 = p1Clone.Descendants<Run>().First();
//Text t1 = p1Clone.Descendants<Text>().First();
//p1Clone.RemoveAllChildren<Run>();
////t1.RemoveAllChildren();
//t1.Text = "Test!!";
tc.RemoveAllChildren();
//tc.Append(p1Clone);
tc.Append(new Paragraph(new Run(new Text("123"))));
}
}
t.AppendChild(rowCopy);
i -= 1;
}
t.RemoveChild(theRow);
}
}
myDoc.MainDocumentPart.Document.Save();
}
No comments:
Post a Comment