Wiki

Clone wiki

Aspose for OpenXML / Change text in a table

OpenXML SDK

class Program
    {
        static void Main(string[] args)
        {
            ChangeTextInCell("Change text in a table.doc", "The text from the OpenXML API example");
        }
        // Change the text in a table in a word processing document.
        public static void ChangeTextInCell(string filepath, string txt)
        {
            // Use the file name and path passed in as an argument to 
            // open an existing document.            
            using (WordprocessingDocument doc =
                WordprocessingDocument.Open(filepath, true))
            {
                // Find the first table in the document.
                Table table =
                    doc.MainDocumentPart.Document.Body.Elements<Table>().First();

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

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

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

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

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

Aspose.Words

class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document("Change text in a table.doc");

            // Get the first table in the document.
            Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

            // Replace any instances of our string in the last cell of the table only.
            table.Rows[1].Cells[2].Range.Replace("Mr", "test", true, true);
            doc.Save("Change text in a table.doc");
        }
    }

Download

Updated