LoginSignup
14
15

More than 5 years have passed since last update.

Java POIでWord(.docx)ファイルを作る

Last updated at Posted at 2016-02-18

はじめに

事前に以下のライブラリを用意します。

今回のサンプルは以下のjarがあれば動作します。

  • poi-3.16.jar
  • poi-ooxml-3.16.jar
  • poi-ooxml-schemas-3.16.jar
  • xmlbeans-2.6.0.jar
  • commons-collections4-4.1.jar

実装例

今回のサンプルでは以下の機能を確認します。

  • 複数の段落を作る
  • 段落にスタイルの異なる文字列を複数配置する
  • 表を作る
  • 表のセルの中に複数の段落を作る
  • 表のセルの中の段落にスタイルの異なる文字列を複数配置する

少しあっさりしていますがまずはこれぐらいで。
動作確認しやすいようにmainメソッドで実行できるようにしてあります。

DOCXWriteTest.java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;

/**
 *
 * @author tool-taro.com
 */
public class DOCXWriteTest {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        String outputFilePath = "out.docx";
        XWPFDocument document = null;
        XWPFTable table;
        XWPFParagraph paragraph;
        XWPFRun run;
        FileOutputStream fout = null;

        try {
            document = new XWPFDocument();

            //普通の段落を2つ作る
            for (int i = 0; i < 2; i++) {
                paragraph = document.createParagraph();

                //それぞれの段落の中に色の異なるテキストを2種配置する
                //setText内で\nを指定しても改行されないので注意、改行するには必ず段落を作る
                run = paragraph.createRun();
                run.setFontFamily("MS ゴシック");
                run.setText("黒のテキスト");

                run = paragraph.createRun();
                run.setFontFamily("MS ゴシック");
                run.setColor("ff0000");
                run.setText("赤のテキスト");
            }
            //2x2の表を作る
            table = document.createTable(2, 2);
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    //それぞれのセルの中に段落を2つ作る
                    for (int k = 0; k < 2; k++) {
                        //セルには初期状態で1つの段落がある(実装が変わるかもしれないので念のため存在数を確認して適切に処理)
                        if (table.getRow(i).getCell(j).getParagraphs().size() > k) {
                            paragraph = table.getRow(i).getCell(j).getParagraphs().get(k);
                        }
                        else {
                            paragraph = table.getRow(i).getCell(j).addParagraph();
                        }

                        //それぞれの段落の中に色の異なるテキストを2種配置する
                        run = paragraph.createRun();
                        run.setFontFamily("MS ゴシック");
                        run.setText("黒のテキスト");

                        run = paragraph.createRun();
                        run.setFontFamily("MS ゴシック");
                        run.setColor("ff0000");
                        run.setText("赤のテキスト");
                    }
                }
            }

            //ファイル出力
            fout = new FileOutputStream(outputFilePath);
            document.write(fout);
        }
        finally {
            if (fout != null) {
                try {
                    fout.close();
                }
                catch (IOException e) {
                }
            }
            if (document != null) {
                try {
                    document.close();
                }
                catch (IOException e) {
                }
            }
        }
    }
}

動作確認

$ javac DOCXWriteTest.java
$ java DOCXWriteTest

作成されたファイルはこんな感じになりました。
無題.png

環境

  • 開発

    • Windows 10 Pro
    • JDK 1.8.0_131
    • NetBeans IDE 8.2
  • 動作検証

    • CentOS Linux release 7.3
    • JDK 1.8.0_131

上記の実装をベースにWebツールも公開しています。
Diff(テキスト差分チェック)|Web便利ツール@ツールタロウ

14
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
15