Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a row to an existing table in a Word Document (open XML)

I need to open an existing Word document (.docx) with an existing table (with, for example, 3 columns) and add a new row to that table. Is there any way of doing this? I am using Open XML

I am creating the table like this (for the first time):

Table tbl = new Table();

// Set the style and width for the table.
TableProperties tableProp = new TableProperties();
TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };

// Make the table width 100% of the page width.
TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };

// Apply
tableProp.Append(tableStyle, tableWidth);
tbl.AppendChild(tableProp);

// Add 3 columns to the table.
TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
tbl.AppendChild(tg);

// Create 1 row to the table.
TableRow tr1 = new TableRow();

// Add a cell to each column in the row.
TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("1"))));
TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("2"))));
TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("3"))));
tr1.Append(tc1, tc2, tc3);

// Add row to the table.
tbl.AppendChild(tr1);
return tbl;
like image 975
Nicole Avatar asked Mar 25 '13 18:03

Nicole


People also ask

How do I add a row to an existing table in Word?

Click where you want in your table to add a row or column and then click the Layout tab (this is the tab next to the Table Design tab on the ribbon). To add rows, click Insert Above or Insert Below and to add columns, click Insert Left or Insert Right.

How do I add a row to an existing table?

Click in a cell above or below where you want to add a row. Under Table Tools, on the Layout tab, do one of the following: To add a row above the cell, click Insert Above in the Rows and Columns group. To add a row below the cell, click Insert Below in the Rows and Columns group.

Why can't I add a row to a table in Word?

Go to the Table Tools category and click the Layout tab. Then, under the Rows & Columns group, click either the Insert Above or Insert Below options to add a new row.


1 Answers

Here you go,

Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>())
{
    t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
}

Use LINQ to get the proper table.

EDIT:

Say you want to get the table that has 4 columns.

Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>().Where(tbl => tbl.GetFirstChild<TableRow>().Descendants<TableCell>().Count() == 4))
{
    t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
}

Say you want to get the table that contains the word "mytable".

Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>().Where(tbl => tbl.InnerText.Contains("myTable")))
{
    t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
}
like image 98
jn1kk Avatar answered Sep 20 '22 22:09

jn1kk