Wednesday, July 30, 2014

Table with totals in a Word document using Open Xml


Table with totals in a Word document using Open Xml 
Assuming you have table object in word document

#region Append Last TOTAL Row
                            IEnumerable<GridColumn> gcc = t.Descendants<GridColumn>();
                            Hashtable hashTable = new Hashtable();
                            int iTmpColumns = 0;
                            while (iTmpColumns < gcc.Count())
                            {
                                hashTable.Add(iTmpColumns, 0);
                                iTmpColumns++;
                            }

                            int iTmpTableRows = 0;
                            if (initialTableRowCounts == 2)
                            {
                                iTmpTableRows = 1;
                            }
                            while (iTmpTableRows < t.Descendants<TableRow>().Count())
                            {
                                // process hash table and inset values
                                int tmp = 0;
                                while (tmp < hashTable.Count)
                                {
                                    TableCell tc = t.Descendants<TableRow>().ElementAt(iTmpTableRows).Descendants<TableCell>().ElementAt(tmp);
                                    int value;
                                    if (int.TryParse(tc.InnerText, out value))
                                    {
                                        hashTable[tmp] = hashTable[tmp] + "+" + tc.InnerText;
                                    }
                                    tmp++;
                                }
                                //                              
                                iTmpTableRows++;
                            }

                            // Clone last row for repetition
                            TableRow rowCopy1 = (TableRow)lastRow.CloneNode(true);
                            // Get all cells of last row
                            IEnumerable<TableCell> tcc2 = rowCopy1.Descendants<TableCell>();
                            // iterate through all cells of last row
                            int iTmpCellCount = 0;
                            while (iTmpCellCount < tcc2.Count())
                            {
                                TableCell tc = tcc2.ElementAt(iTmpCellCount);
                                // 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 = "Formula(" + hashTable[iTmpCellCount].ToString() + ")";
                                iTmpCellCount++;
                            }
                            t.AppendChild(rowCopy1);

                            #endregion

No comments:

Post a Comment