Wednesday, July 30, 2014

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

No comments:

Post a Comment