Create Table in Word Document Using Java

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class CreateTable {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();

//Data used to create table
String[] header = {"Name", "Capital", "Continent", "Area", "Population"};
String[][] data =
new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
new String[]{"Colombia", "Bogota", "South America", "1138907", "33000000"},
new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
new String[]{"America", "Washington", "North America", "9363130", "249200000"},
new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}

//Add table
Table table = section.addTable(true);
table.resetCells(data.length + 1, header.length);

//Set the first row as table header and add data
TableRow row = table.getRows().get(0);
for (int i = 0; i < header.length; i++) {
Paragraph p = row.getCells().get(i).addParagraph();
TextRange txtRange = p.appendText(header[i]);

//Add data to the rest of rows
for (int r = 0; r < data.length; r++) {
TableRow dataRow = table.getRows().get(r + 1);
for (int c = 0; c < data[r].length; c++) {

//Set background color for cells
for (int j = 1; j < table.getRows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = table.getRows().get(j);
for (int f = 0; f < row2.getCells().getCount(); f++) {
row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230));

//save the document
document.saveToFile("CreateTable.docx", FileFormat.Docx_2013);




Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Power of Light Probes in Unity

What is Style in Coding and Programming Languages?

Where Did Tom, the Founder of Myspace, Bizarrely Vanish To?

Securely using secrets in a pipeline — HashiCorp Vault + JWT Auth

Adventures in GameDev with GameDevHQ Day 5: Wrapping things up with GIT

Interview with Bozhidar Batsov

TDD Conference 2021 — TDD Manifesto — Daniel Moka

Your mission

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

Experience with Spring batch and docker

Java/ Add Footer to PowerPoint Document

Build Your First Spring Boot Project!

Rest API Responses with Spring MVC