Create Table in Word document using Apache POI

Introduction

In this tutorial I will show you how to create Table in Word document using Apache POI API. Table is great representation when you have to display data in tabular format because table consists of rows and columns for displaying data uniformly. I will use both Apache POI 3.x and 4.x version to make the example works. You may also try to use other version of Apache POI and let me know if it works.

We will create here a Java application to table in word document using apache poi library. Using apache poi library is very easy for any kind of activities in word document. Here I will show you both maven and gradle build tools to build our application.

Prerequisites

Eclipse 4.12, JDK 8 or 12, Maven 3.6.1, Gradle 5.6, Apache POI 3.15 to 4.1.1

Creating Project

You basically need to create a maven project in eclipse with below details:

Group Id: com.roytuts
Artifact Id: word-table-apache-poi

If you are creating a gradle based project then project name is word-table-apache-poi.

Updating Build File

Maven File

Once the project is created and eclipse finishes build then you need to modify the pom.xml file as shown below.

Here we will add apache poi API as a dependency for working with microsoft word document or even you can work with open source word document.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.roytuts</groupId>
	<artifactId>word-table-apache-poi</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8 or 12</java.version>
		<poi.version>3.15 or 4.1.1</poi.version>
	</properties>
	
	<dependencies>
		<!-- apache poi for xlsx, docx etc reading/writing -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>${poi.version}</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Gradle File

If you want to use gradle build script to create your project then use below build script to setup the project. Here also we have used the same apache poi version as you have seen in the above maven based pom.xml file.

plugins {
    id 'java-library'
}

sourceCompatibility = 12 or 1.8
targetCompatibility = 12 or 1.8

repositories {
    jcenter()
}

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.1 or 3.15'
}

Creating Table using Java

We know how to create table in word document manually from an option given in the word document but here we will see how to create table in word document using apache poi in Java program.

Related Posts:

Create Table in Word

Create below class to create Table in word file.

In the below source code we create object of XPFWDocument type and we create a table with 3 rows and 4 columns.

We first create header texts in each column of the first row. We decorate the text as bold.

Then we write text for columns id, first name, last name, email at each row.

We also create two another paragraphs right after the table with sample text. We decorate the paragraph texts with strike-through and underlined respectively.

package com.roytuts.word.table.apache.poi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;

public class WordDocxTable {

	public static void main(String[] args) {
		createTableWord("WordDocxTable.docx");
	}

	public static void createTableWord(final String fileName) {
		XWPFDocument doc = new XWPFDocument();
		try {
			// create table with 3 rows and 4 columns
			XWPFTable table = doc.createTable(3, 4);
			
			// write to first row, first column
			XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
			p1.setAlignment(ParagraphAlignment.CENTER);
			XWPFRun r1 = p1.createRun();
			r1.setBold(true);
			r1.setText("ID");
			
			// write to first row, second column
			XWPFParagraph p2 = table.getRow(0).getCell(1).getParagraphs().get(0);
			p2.setAlignment(ParagraphAlignment.CENTER);
			XWPFRun r2 = p2.createRun();
			r2.setBold(true);
			r2.setText("First Name");
			
			// write to first row, third column
			XWPFParagraph p3 = table.getRow(0).getCell(2).getParagraphs().get(0);
			p3.setAlignment(ParagraphAlignment.CENTER);
			XWPFRun r3 = p3.createRun();
			r3.setBold(true);
			r3.setText("Last Name");
			
			// write to first row, fourth column
			XWPFParagraph p4 = table.getRow(0).getCell(3).getParagraphs().get(0);
			p4.setAlignment(ParagraphAlignment.CENTER);
			XWPFRun r4 = p4.createRun();
			r4.setBold(true);
			r4.setText("Email");
			
			// write to second row
			table.getRow(1).getCell(0).setText("1000");
			table.getRow(1).getCell(1).setText("Soumitra");
			table.getRow(1).getCell(2).setText("Roy");
			table.getRow(1).getCell(3).setText("email@email.com");
			
			// write to third row
			table.getRow(2).getCell(0).setText("1001");
			table.getRow(2).getCell(1).setText("John");
			table.getRow(2).getCell(2).setText("Joe");
			table.getRow(2).getCell(3).setText("email@email.com");
			
			// create a paragraph with Strike-Through text
			XWPFParagraph p5 = doc.createParagraph();
			
			// left alignment
			p5.setAlignment(ParagraphAlignment.LEFT);
			
			// wrap words
			p5.setWordWrapped(true);
			
			// XWPFRun object defines a region of text with a common set of
			// properties
			XWPFRun r5 = p5.createRun();
			String t5 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
			r5.setText(t5);
			
			// make StrikeThrough
			r5.setStrikeThrough(true);
			
			// create a paragraph with Underlined text
			XWPFParagraph p6 = doc.createParagraph();
			
			// left alignment
			p6.setAlignment(ParagraphAlignment.LEFT);
			
			// wrap words
			p6.setWordWrapped(true);
			
			// XWPFRun object defines a region of text with a common set of
			// properties
			XWPFRun r6 = p6.createRun();
			String t6 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
			r6.setText(t6);
			
			// make Underlined
			r6.setUnderline(UnderlinePatterns.SINGLE);
			
			OutputStream out = null;
			try {
				out = new FileOutputStream(fileName);
				doc.write(out);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} finally {
			try {
				doc.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Testing the Application

Run the above class to see the output in WordDocxTable.docx file. The file is created under root directory of the project.

You will get the output in the generated word document similar to the below image:

create table in word document using apache poi

Hope you got an idea how to create table in word document using apache poi library.

Source Code

Download

Thanks for reading.

Leave a Comment