Java creates a table in a Word document

In Word documents, tables can be used to make the content of text more concise and clear, while at the same time making the display of data clearer and more intuitive. This article will show you how to use Java code to create a table in a Word document and set the background color of the cells.

** Tools used: ** Free Spire.Doc for Java (Free version)

** How to import JAR file ** ** Method 1: ** Download and unzip the Free Spire.Doc for Java package and import the Spire.Doc.jar package from the lib folder into your Java application.

Method 2: If you are using maven, you need to add the following dependency to your pom.xml file:


** Java code example: **

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 Word document
        Document document = new Document();
        //Added section
        Section section = document.addSection();

        //Tabular data
        String[] header = {"name", "sex", "Department", "Job number"};
        String[][] data =
                        new String[]{"Winny", "Female", "Accountant", "0109"},
                        new String[]{"Lois", "Female", "Salesperson", "0111"},
                        new String[]{"Jois", "male", "Technical staff", "0110"},
                        new String[]{"Moon", "Female", "Salesperson", "0112"},
                        new String[]{"Vinit", "Female", "Support staff", "0113"},

        //Add table
        Table table = section.addTable(true);

        //Set the number of rows and columns in the table
        table.resetCells(data.length + 1, header.length);

        //Set the first row as a 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 range1 = p.appendText(header[i]);

        //Add data to the remaining 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++) {
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);

        //Set the background color of the cell
        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("Table.docx", FileFormat.Docx_2013);

** Output document: ** table.jpg

Recommended Posts

Java creates a table in a Word document
Java creates a Word document
Java creates a pie chart in Excel
How to print a Java Word document
Find a subset in Java
Java creates an Excel document
Java joins and splitting table cells in Word documents
3 Implement a simple interpreter in Java
I created a PDF in Java.
A simple sample callback in Java
[Java] Add WordArt to Word document
Get stuck in a Java primer
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
[Java] Realize multi-column effect of Word document
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Implement something like a stack in Java
Split a string with ". (Dot)" in Java
Creating a matrix class in Java Part 1
Add footnotes to Word documents in Java
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
Two ways to start a thread in Java + @
Read a string in a PDF file with Java
Create a CSR with extended information in Java
A story about the JDK in the Java 11 era
Partization in Java
How to display a web page in Java
[Android / Java] Operate a local database in Room
Measure the size of a folder in Java
Code to escape a JSON string in Java
Try to create a bulletin board in Java
Changes in Java 11
Rock-paper-scissors in Java
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
Let's create a super-simple web framework in Java
Delete all records in a MySQL database table
I made a rock-paper-scissors game in Java (CLI)
A bat file that uses Java in windows
Java tips-Create a Spring Boot project in Gradle
A quick review of Java learned in class
I tried to output multiplication table in Java
(Ruby on Rails6) Creating data in a table
Java adds and removes watermarks in word documents
Java 11 document summary
I wrote a prime factorization program in Java
Pi in Java
Change paragraph text color in Java Word documents
FizzBuzz in Java
Machine learning (DeepLeaning4j) in Java Try to learn a document and extract words that are highly related to a specific word
How to clear all data in a particular table
I made a simple calculation problem game in Java
How to create a Java environment in just 3 seconds
Make "I'm not a robot" in Java EE (Jakarta EE)
A quick review of Java learned in class part4
Write a class that can be ordered in Java