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

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