Create Table in Word document using Apache POI

In this tutorial I will show you how to create Table in a Word document using Apache POI API.

Prerequisites

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven 3 installed and configured
Apache POI dependency in pom.xml

Modify the pom.xml file as shown below

<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>apache-poi-word</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>apache-poi-word</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<poi.version>3.15</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>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Create below class to create Table in word file

package com.roytuts.apache.poi.word;

import java.io.FileOutputStream;
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 DocxSimpleTable {

	public static void main(String[] args) throws Exception {
		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("contact@roytuts.com");

			// write to third row
			table.getRow(2).getCell(0).setText("1001");
			table.getRow(2).getCell(1).setText("Soumitra");
			table.getRow(2).getCell(2).setText("Sarkar");
			table.getRow(2).getCell(3).setText("contact@roytuts.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 = new FileOutputStream("DocxTable.docx");
			try {
				doc.write(out);
			} finally {
				out.close();
			}
		} finally {
			doc.close();
		}
	}

}

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

Thanks for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *