Java creates tables in Word

Posted by cmp241 on Tue, 21 Apr 2020 17:03:39 +0200

In Word documents, tables can make the text content more concise and clear, and also can make the data display more clear and intuitive. This article describes how to use Java code to create a table in a Word document and set the background color of its cells.

 

Jar file import method

Method 1:

Download free Free Spire.Doc for Java Package and extract it, then import the spirit.doc.jar package into your Java application from the lib folder. (see the figure below after importing successfully)

 

 

Method 2:

Import through Maven warehouse installation. Please refer to the link for detailed operation steps:

https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

 

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) {
        //Establish Word File
        Document document = new Document();
        //Add a section
        Section section = document.addSection();

        //data
        String[] header = {"Full name", "Gender", "department", "Job number"};
        String[][] data =
                {
                        new String[]{"Winny", "female", "comprehensive", "0109"},
                        new String[]{"Lois", "female", "comprehensive", "0111"},
                        new String[]{"Jois", "male", "technology", "0110"},
                        new String[]{"Moon", "female", "Sale", "0112"},
                        new String[]{"Vinit", "female", "logistics", "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 the table header and add data
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(20);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.gray);
        for (int i = 0; i < header.length; i++) {
            row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            Paragraph p = row.getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange range1 = p.appendText(header[i]);
            range1.getCharacterFormat().setFontName("Arial");
            range1.getCharacterFormat().setFontSize(12f);
            range1.getCharacterFormat().setBold(true);
        }

        //Add data to remaining rows
        for (int r = 0; r < data.length; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            dataRow.getRowFormat().setBackColor(Color.white);
            for (int c = 0; c < data[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }

        //Set cell background color
        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 document
        document.saveToFile("Create table.docx", FileFormat.Docx_2013);
    }
}

 

Create a table rendering:

Topics: Java Maven