Wednesday, July 30, 2014

Read table row and perform total in Word document using Open Xml

Read Table row and perform total of row cells and append table cell at end for total


                                    #region HORIZONTAL TOTAL
                                    // append last Column to total
                                    GridColumn gc1 = new GridColumn();
                                    tg.AppendChild(gc1);

                                    Hashtable hashTable1 = new Hashtable();
                                    IEnumerable<TableRow> trs1 = t.Descendants<TableRow>();
                                    int iRowsCount1 = 0;
                                    // storing values in hashtable
                                    while (iRowsCount1 < trs1.Count())
                                    {
                                        hashTable1.Add(iRowsCount1, 0);
                                        IEnumerable<TableCell> tcc = t.Descendants<TableRow>().ElementAt(iRowsCount1).Descendants<TableCell>();
                                        TableCell tcClone = (TableCell)t.Descendants<TableRow>().ElementAt(iRowsCount1).Descendants<TableCell>().Last().CloneNode(true);

                                        foreach (TableCell tc in tcc)
                                        {
                                            int value;
                                            if (int.TryParse(tc.InnerText, out value))
                                            {
                                                hashTable1[iRowsCount1] = hashTable1[iRowsCount1] + "+" + tc.InnerText;
                                            }
                                        }
                                        // values are stored in hashtable
                                        if (tcClone.InnerText != "")
                                        {
                                            // Gel all text elements of TableCell
                                            IEnumerable<Text> texts = tcClone.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 = tcClone.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();
                                            if (hashTable1[iRowsCount1].ToString() == "0")
                                            {
                                                t1.Text = "";
                                            }
                                            else
                                            {
                                                RunProperties rp = new RunProperties();
                                                Bold bold = new Bold();
                                                rp.Append(bold);
                                                r.Append(rp);
                                                t1.Text = "Formula(" + hashTable1[iRowsCount1].ToString() + ")";
                                            }
                                            t.Descendants<TableRow>().ElementAt(iRowsCount1).AppendChild(tcClone);
                                        }
                                        iRowsCount1++;


                                    }

                                    #endregion
                                }
                            }

                        }

No comments:

Post a Comment