Example #1
0
        public void  ChangeLastNameForSecondRow(string pFileName, string pTextValue)
        {
            var fileName = Path.Combine(DocumentFolder, pFileName);

            if (!File.Exists(fileName))
            {
                CreateDocumentWithTableFromArray(fileName);
            }

            // Open our document, second parameter indicates edit more
            using (var document = WordprocessingDocument.Open(fileName, true))
            {
                // Get the sole table in the document
                Table table = document.MainDocumentPart.Document.Body.Elements <Table>().First();

                // Find the second row in the table.
                TableRow row = table.Elements <TableRow>().ElementAt(1);

                // Find the second cell in the row.
                TableCell cell = row.Elements <TableCell>().ElementAt(1);

                // Find the first paragraph in the table cell.
                Paragraph p = cell.Elements <Paragraph>().First();

                // Find the first run in the paragraph.
                Run run = p.Elements <Run>().First();

                // Set the text for the run.
                Text text = run.Elements <Text>().First();
                text.Text = pTextValue;

                document.Save();
            }
        }
Example #2
0
        //:::::WORD DOCUMENT PROCESSING PART:::::
        public static void AddToTable(String fileName, String tableName, int elemNo, String txt, int elemRow = 0)
        {
            List <WordP.Table> tables = null;

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true))
            {
                tables = new List <WordP.Table>(wordDoc.MainDocumentPart.Document.Body.Elements <WordP.Table>());
                tables.ForEach(table =>
                {
                    WordP.TableProperties tblProperties = table.GetFirstChild <WordP.TableProperties>();
                    if (tblProperties != null)
                    {
                        WordP.TableCaption caption = tblProperties.GetFirstChild <WordP.TableCaption>();
                        if (caption != null)
                        {
                            if (caption.Val.HasValue && caption.Val.Value.Equals(tableName))
                            {
                                WordP.TableRow row = table.Elements <WordP.TableRow>().ElementAt(elemNo);

                                // Find the third cell in the row.
                                WordP.TableCell cell = row.Elements <WordP.TableCell>().ElementAt(elemRow);

                                // Find the first paragraph in the table cell.
                                WordP.Paragraph p = cell.Elements <WordP.Paragraph>().First();

                                // Find the first run in the paragraph.
                                WordP.Run r = p.Elements <WordP.Run>().First();

                                // Set the text for the run.
                                WordP.Text t = r.Elements <WordP.Text>().First();
                                t.Text       = txt;

                                // you have got your table. process the rest.
                            }
                        }
                    }
                });
            }
        }