Wednesday, July 30, 2014

Repeat table rows in word document using OPEN XML SDK

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();

            }

repeat column in table in word document using Open XML SKD

// Create Grid
TableGrid tg = t.Descendants<TableGrid>().FirstOrDefault();
                            GridColumn gc = new GridColumn() { Width = "1024" };
                            tg.AppendChild(gc);
// Get all rows and append cell to each row
                            IEnumerable<TableRow> trs = t.Descendants<TableRow>();
                            foreach (TableRow tr in trs)
                            {

                                tr.AppendChild(new TableCell(new Paragraph(new Run(new Text("")))));
                            }

Repeat rows and columns in word document using OPEN XML SDK

Repeat 3 columns / 5 rows in existing table

private static void ProcessTable(WordprocessingDocument myDoc)
        {
            RevisionAccepter.AcceptRevisions(myDoc);
            // Get all tables
            IEnumerable<Table> tables = myDoc.MainDocumentPart.Document.Body.Descendants<Table>();
            foreach (Table t in tables)
            {

                // Get last row of table                   
                TableRow lastRow = t.Elements<TableRow>().Last();
                // Check last row's first element (table cell) - TO check if need to repeate table VERTICAL
                if (lastRow.Descendants<TableCell>().First().InnerText.Contains("{{"))
                {
                    #region  REPEAT VERTICAL
                    int i = 5;
                    while (i > 0)
                    {
                        // Clone last row for repetition
                        TableRow rowCopy = (TableRow)lastRow.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 (tc.InnerText != "")
                            {
                                // Gel all text elements of TableCell
                                IEnumerable<Text> texts = tc.Descendants<Text>();
                                //Clear all text elements' inner text
                                foreach (Text text in texts)
                                {
                                    text.Text = "";
                                }
                                //// Find the first paragraph in the table cell.
                                Paragraph p = tc.Elements<Paragraph>().First();
                                //// Find the first run in the paragraph.
                                Run r = p.Elements<Run>().First();
                                //// Set the text for the run.
                                Text t1 = r.Elements<Text>().First();
                                // Set first text element
                                t1.Text = "$Row test$";
                            }
                            else
                            {
                                tc.Append(new Paragraph(new Run(new Text("$Row test$ - BLANK"))));
                            }
                        }
                        t.AppendChild(rowCopy);
                        i -= 1;
                    }
                    t.RemoveChild(lastRow);

                    #endregion
                }
                else
                {
                    // Get first row of table
                    TableRow firstRow = t.Elements<TableRow>().First();
                    int firstRowCellCount = firstRow.Descendants<TableCell>().Count();
                    // means start repeating from second column
                    if (firstRowCellCount > 1)
                    {
                        // table contains more than one columns
                        // check first column first cell text
                        if (firstRow.Descendants<TableCell>().ElementAt(1).InnerText.Contains("{{"))
                        {
                            // REPEAT HORIZONTALLY
                            // Get all table rows
                            IEnumerable<TableRow> trs = t.Descendants<TableRow>();
                            // Get tableGrid
                            TableGrid tg = t.Descendants<TableGrid>().FirstOrDefault();

                            int i = 3;
                            while (i > 0)
                            {
                                // add GridColumn to tableGrid                                  
                                GridColumn gc = new GridColumn() { Width = "250" };
                                tg.AppendChild(gc);

                                // Append table cell to table row
                                foreach (TableRow tr in trs)
                                {
                                    // clone tablecell - first cell of second row ( first row second column )
                                    TableCell tc2 = (TableCell)tr.Descendants<TableCell>().ElementAt(1).Clone();
                                    if (tc2.InnerText != "")
                                    {
                                        // Gel all text elements of TableCell
                                        IEnumerable<Text> texts = tc2.Descendants<Text>();
                                        ////Clear all text elements' inner text
                                        foreach (Text text in texts)
                                        {
                                            text.Text = "";
                                        }
                                        // Find the first paragraph in the table cell.
                                        Paragraph p = tc2.Elements<Paragraph>().First();
                                        // Find the first run in the paragraph.
                                        Run r = p.Elements<Run>().First();
                                        // Set the text for the run.
                                        Text t1 = r.Elements<Text>().First();
                                        t1.Text = "column test!!";
                                        tr.AppendChild(tc2);
                                    }
                                    else
                                    {
                                        TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("$column test$ - BLANK"))));
                                        tr.AppendChild(tc3);
                                    }
                                    ////tr.Descendants<TableCell>().ElementAt(1).Remove();
                                }
                                i--;
                            }
                            // REMOVE SECOND COLUMN AFTER PROCESSING
                            foreach (TableRow tr in trs)
                            {
                                // clone tablecell - first cell of second row ( first row second column )
                                tr.Descendants<TableCell>().ElementAt(1).Remove();
                            }
                        }
                    }
                }
            }

            myDoc.MainDocumentPart.Document.Save();
        }