Exporting Rich Text Content to Word Documents in Java using Apache POI

Introduction

Generating Word documents from Java applications is a common requirement in enterprise systems. Apache POI provides robust APIs for creating and manipulating Microsoft Office documents. This guide demonstrates how to export rich text content to a Word document programatically.

Adding Maven Dependencies

Include the Apache POI dependancy in your project's pom.xml file:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.4</version>
</dependency>

The poi-ooxml artifact is required for working with the newer .docx file format.

Implementation Example

The following code creates a Word document with styled text content:

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;

public class WordDocumentGenerator {

    private static final String OUTPUT_PATH = "generated_report.docx";

    public void createDocument(String title, String body) throws IOException {
        XWPFDocument doc = new XWPFDocument();
        
        // Create title paragraph with bold styling
        XWPFParagraph titlePara = doc.createParagraph();
        XWPFRun titleRun = titlePara.createRun();
        titleRun.setBold(true);
        titleRun.setFontSize(16);
        titleRun.setText(title);
        
        // Create body paragraph
        XWPFParagraph bodyPara = doc.createParagraph();
        XWPFRun bodyRun = bodyPara.createRun();
        bodyRun.setText(body);
        
        // Write to file
        try (FileOutputStream fos = new FileOutputStream(OUTPUT_PATH)) {
            doc.write(fos);
        }
        doc.close();
    }

    public static void main(String[] args) {
        WordDocumentGenerator generator = new WordDocumentGenerator();
        try {
            generator.createDocument(
                "Project Report",
                "This document was generated using Apache POI library."
            );
            System.out.println("Word document created successfully.");
        } catch (IOException e) {
            System.err.println("Failed to create document: " + e.getMessage());
        }
    }
}

Key Components

  • XWPFDocument: Represents the entire Word document structure.
  • XWPFParagraph: Defines a parargaph within the document.
  • XWPFRun: Contains text content with specific formatting properties such as bold, italic, font size, and color.

Rich Text Formatting Options

The XWPFRun class provides various methods for text styling:

XWPFRun run = paragraph.createRun();
run.setBold(true);
run.setItalic(true);
run.setUnderline(UnderlinePatterns.SINGLE);
run.setFontSize(14);
run.setFontFamily("Arial");
run.setColor("FF0000"); // Red color
run.setText("Styled text content");

After executing the program, a .docx file will be generated in the project directory containing the formatted text content.

Tags: Apache POI java Word document XWPFDocument Rich Text Export

Posted on Fri, 30 Oct 2026 16:40:47 +0000 by herschen