Java Word 添加内容

简介

Microsoft Word 是一款功能强大的文字处理软件,广泛应用于办公和学术场景中。在 Java 程序中,我们可以使用 Apache POI 这个开源库来操作 Word 文档。本文将介绍如何使用 Java 和 Apache POI 来添加内容到 Word 文档中。

准备工作

在开始之前,我们需要确保已经安装好 Java 开发环境,并且下载并导入 Apache POI 的 JAR 包。可以通过以下方式添加 Apache POI 的依赖:

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

创建 Word 文档

首先,我们需要创建一个空的 Word 文档。可以使用以下代码来创建一个空的 Word 文档:

import org.apache.poi.xwpf.usermodel.*;

public class CreateWordDocument {
    public static void main(String[] args) {
        // 创建一个新的空白文档
        XWPFDocument document = new XWPFDocument();

        // 保存文档到文件
        try {
            FileOutputStream out = new FileOutputStream("document.docx");
            document.write(out);
            out.close();
            System.out.println("文档创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行上述代码后,会在当前目录下生成一个名为 document.docx 的空白 Word 文档。

添加文本内容

接下来,我们可以向 Word 文档中添加文本内容。可以使用以下代码来添加文本内容:

import org.apache.poi.xwpf.usermodel.*;

public class AddTextToDocument {
    public static void main(String[] args) {
        // 打开现有文档
        try {
            FileInputStream in = new FileInputStream("document.docx");
            XWPFDocument document = new XWPFDocument(in);

            // 创建段落
            XWPFParagraph paragraph = document.createParagraph();

            // 创建文本
            XWPFRun run = paragraph.createRun();
            run.setText("Hello, World!");

            // 保存文档到文件
            FileOutputStream out = new FileOutputStream("document.docx");
            document.write(out);
            out.close();
            System.out.println("文本添加成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行上述代码后,打开 document.docx 文档,将会看到文档中添加了一个段落,并包含了文本 "Hello, World!"。

添加表格

除了文本内容,我们还可以向 Word 文档中添加表格。可以使用以下代码来添加一个简单的表格:

import org.apache.poi.xwpf.usermodel.*;

public class AddTableToDocument {
    public static void main(String[] args) {
        // 打开现有文档
        try {
            FileInputStream in = new FileInputStream("document.docx");
            XWPFDocument document = new XWPFDocument(in);

            // 创建表格
            XWPFTable table = document.createTable(3, 3);

            // 填充表格内容
            for (int row = 0; row < 3; row++) {
                for (int col = 0; col < 3; col++) {
                    XWPFTableCell cell = table.getRow(row).getCell(col);
                    cell.setText("Row " + row + ", Col " + col);
                }
            }

            // 保存文档到文件
            FileOutputStream out = new FileOutputStream("document.docx");
            document.write(out);
            out.close();
            System.out.println("表格添加成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行上述代码后,打开 document.docx 文档,将会看到文档中添加了一个 3x3 的表格,并填充了内容。

流程图

下面是一个示例的代码添加流程图:

st=>start: 开始
op1=>operation: 创建空白文档
op2=>operation: 创建段落
op3=>operation: 添加文本
op4=>operation: 保存文档
e=>end: 结束

st->op1->op2->op3->op4->e

总结

通过使用 Java 和 Apache POI,我们可以方便地