How to Add a Row to a Table in Word using Java

This short tutorial explains how to add a row to a table in Word using Java. With the help of this tutorial you can also insert multiple rows in Word table using Java. At the end this output file is saved as DOCX however, you can save it in any Word file format.

Steps to Add a Row to a Table in Word using Java

  1. Configure your project to add Aspose.Words for Java from the Maven repository
  2. Open a word file containing a Table in it using the Document object
  3. Get the reference to the table in the Word file
  4. Create a new Row and add desired data in the columns
  5. Insert this row after the first row in the table
  6. Clone an existing row and clear its content
  7. Fill multiple rows with some data
  8. Add rows to existing table in Word at the end
  9. Save the file after adding rows to existing table

Using these steps we open the Word file containing a table and insert a row into it. Similarly we can add multiple rows to a table in Word using Java by filling sample data into multiple rows and adding these rows at the end of the table.

Code to Add New Row to Table in Word using Java

import com.aspose.words.License;
import com.aspose.words.Paragraph;
import com.aspose.words.Row;
import com.aspose.words.Run;
import com.aspose.words.Cell;
import com.aspose.words.Document;
import com.aspose.words.Table;
public class HowToAddARowToATableInWordUsingJava {
public static void main() throws Exception { //main() function for HowToAddARowToATableInWordUsingJava
// Instantiate a license to remove trial version watermark in the output Word file
License license = new License();
license.setLicense("Aspose.Words.lic");
// Open Word Document having a table in it
Document WordDocumentWithTable = new Document("MS Word.docx");
// Get the reference to the table by index
Table tableToAddRowsTo = WordDocumentWithTable.getFirstSection().getBody().getTables().get(0);
// Instantiate a new Row class object
Row row = new Row(WordDocumentWithTable);
// Add Cells to the collection of cells of the newly created row
for (int i = 0; i < tableToAddRowsTo.getRows().get(0).getCells().getCount(); i++)
{
Cell cell = new Cell(WordDocumentWithTable);
cell.appendChild(new Paragraph(WordDocumentWithTable));
cell.getFirstParagraph().getRuns().add(new Run(WordDocumentWithTable, "Text in Cell " + i));
row.getCells().add(cell);
}
// Insert the new Row after the first Row in the table
tableToAddRowsTo.getRows().insert(1, row);
// Deep Clone an existing Row from the Table
Row cloneOfRow = (Row)tableToAddRowsTo.getFirstRow().deepClone(true);
// Remove all content from all Cells in the cloned row
for (Cell cell : cloneOfRow)
{
cell.removeAllChildren();
cell.ensureMinimum();
}
// Add number of rows say 10 to the end of table
for (int iRowCount = 0; iRowCount < 10; iRowCount++)//You can set any number of rows instead of 10
{
Row emptyRow = (Row)cloneOfRow.deepClone(true);
tableToAddRowsTo.getRows().add(emptyRow);
}
WordDocumentWithTable.save("Added Rows to Table to MS Word.docx");
}
}

In this Java code, we used Document, Table and Row classes to access different elements in the Word document and added a row to an existing table in Word using Java. At the end of the code, sample is provided to add multiple rows in Word table using Java such that a row is multiple time added to the collection of rows in a loop for the demonstration purpose.

In this tutorial, we opened an existing file however if you want to create a new Word document refer to the article how to create Word document using Java. Note that we do not require MS Words or Interop to be available on the system to run the above code.

 English