How to create a table in word in Java

Tables are both a visual communication mode and a means of organizing data. Various forms are widely used in communication, scientific research and data analysis activities. So how to create a Word table through a Java application? Don't worry, this article will introduce you to create a Word table through Java application in detail. The following are the ideas and specific steps I have organized, and the Java code is attached for your reference.

Programming Environment

First, you’re required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link.
method 1:
Introduced manually. Download Free Spire.Doc for Java locally, unzip it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:



If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.com/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>

</dependencies>

Specific Steps:
  • Create a Document object, and add a section to it.
  • Prepare the data for the header row and other rows, storing them in a one-dimensional string array and a two-dimensional string array respectively.
  • Add a table to the section using Section.addTable() method.
  • Insert data to the header row, and set the row formatting, including row height, background color, and text alignment.
  • Insert data to the rest of the rows, and apply formatting to these rows.
  • Save the document to another file using Document.saveToFile() method.
Full Code:
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();

//Define the data for 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"},

};

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

//Set the first row as table header
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 txtRange = p.appendText(header[i]);
txtRange.getCharacterFormat().setBold(true);
}

//Add data to the rest of 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);
dataRow.getCells().get(c).addParagraph().appendText(data[r][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 to file
document.saveToFile("output/CreateTable.docx", FileFormat.Docx_2013);
}
}

Effective Shot:


conclusion

In this post, you have learned how to remove Duplicate Values When Merging Cells in Word in Java. Not only that, we also have other functions, such as,Java: Remove Duplicate Values When Merging Cells in Word , Insert Images to a Table in Word in Java and so on. Apart from that, if you'd like to learn more, you can visit  this link to explore more about for Spire.Doc for Java.


Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application

Java: How to encrypt or decrypt PDF documents?