JavaはWord文書にテーブルを作成します


Word文書では、表を使用することにより、テキストの内容をより簡潔かつ明確にすることができ、同時にデータの表示をより明確かつ直感的にすることができます。この記事では、Javaコードを使用してWord文書にテーブルを作成し、セルの背景色を設定する方法を紹介します。

使用ツール: Free Spire.Doc for Java(無料版)

JARファイルのインポート方法
方法1: Free Spire.Doc for Javaパッケージをダウンロードして解凍し、Spire.Doc.jarパッケージをlibフォルダーからJavaアプリケーションにインポートします。

方法2: mavenを使用している場合は、pom.xmlファイルに次の依存関係を追加する必要があります。

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>2.7.3</version>
    </dependency>
</dependencies>

Javaコード例:

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class CreateTable {
    public static void main(String[] args) {
        //Word文書を作成する
        Document document = new Document();
        //sectionを追加
        Section section = document.addSection();

        //表形式のデータ
        String[] header = {"名前", "性別", "部門", "ジョブ番号"};
        String[][] data =
                {
                        new String[]{"Winny", "女性", "会計士", "0109"},
                        new String[]{"Lois", "女性", "販売員", "0111"},
                        new String[]{"Jois", "男性", "技術スタッフ", "0110"},
                        new String[]{"Moon", "女性", "販売員", "0112"},
                        new String[]{"Vinit", "女性", "支援スタッフ", "0113"},
                };

        //テーブルを追加
        Table table = section.addTable(true);

        //テーブルの行と列の数を設定する
        table.resetCells(data.length + 1, header.length);

        //最初の行をテーブルヘッダーとして設定し、データを追加する
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(20);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.gray);
        for (int i = 0; i < header.length; i++) {
            row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            Paragraph p = row.getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange range1 = p.appendText(header[i]);
            range1.getCharacterFormat().setFontName("Arial");
            range1.getCharacterFormat().setFontSize(12f);
            range1.getCharacterFormat().setBold(true);
        }

        //残りの行にデータを追加する
        for (int r = 0; r < data.length; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            dataRow.getRowFormat().setBackColor(Color.white);
            for (int c = 0; c < data[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }

        //セルの背景色を設定する
        for (int j = 1; j < table.getRows().getCount(); j++) {
            if (j % 2 == 0) {
                TableRow row2 = table.getRows().get(j);
                for (int f = 0; f < row2.getCells().getCount(); f++) {
                    row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230));
                }
            }
        }

        //ドキュメントを保存します
        document.saveToFile("Table.docx", FileFormat.Docx_2013);
    }
}

出力ドキュメント: