Exemplo n.º 1
0
        public void FillTable(OpenXmlElement element, QResult query)
        {
            var row = FindParent <Word.TableRow>(element);
            var prg = row.Descendants <Word.Paragraph>().FirstOrDefault();

            //element.Remove();
            Word.TableRow prow = null;
            foreach (object[] data in query.Values)
            {
                Word.TableCell cell = row.GetFirstChild <Word.TableCell>();
                foreach (object value in data)
                {
                    if (cell != null)
                    {
                        var paragraph = cell.Descendants <Word.Paragraph>().FirstOrDefault();
                        if (paragraph == null)
                        {
                            paragraph = (Word.Paragraph)prg.Clone();
                            cell.Append(paragraph);
                        }
                        Word.Run run = paragraph.Descendants <Word.Run>().FirstOrDefault();
                        if (run != null)
                        {
                            ReplaceString(run, value.ToString());
                        }
                        else
                        {
                            Word.Text text = new Word.Text()
                            {
                                Text = value.ToString(), Space = SpaceProcessingModeValues.Preserve
                            };
                            run = new Word.Run();
                            run.Append(text);
                            paragraph.Append(run);
                        }

                        cell = cell.NextSibling <Word.TableCell>();
                    }
                }
                if (row.Parent == null)
                {
                    prow.InsertAfterSelf <Word.TableRow>(row);
                }
                prow = row;
                row  = (Word.TableRow)row.Clone();
            }
        }