チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.Doc for Java»»Java:Word で表を作成する方法
2022-06-10

Java:Word で表を作成する方法

表は、Word 文書で表の形式のデータを表示するための一般的な方法の一つです。これは、大量の情報を整理するのに大いに役立ちます。この記事では、Spire.Doc for Java を使用して、表を作成する方法、表にデータを入力する方法、および表のセルに書式を適用する方法をご紹介します。

Spire.Doc for Java をインストールします

まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

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

Word で簡単な表を作成する

次のテーブルに、表の作成とフォーマットを担当するコアクラスとメソッドの一部を示します。

名前 説明
Table Class Word文書の表を表します。
TableRow Class テーブルの行を表します。
TableCell Class テーブル内の特定のセルを表します。
Section.addTbale() Method 指定されたセクションに新しいテーブルを追加します。
Table.resetCells() Method 行番号と列番号をリセットします。
Table.getRows() Method テーブルの行を取得します。
TableRow.setHeight() Method 指定した行の高さを設定します。
TableRow.getCells() Method セルコレクションを返します。
TableRow.getFormat() Method 指定された行の形式を取得します。

以下は、Word文書に簡単な表を作成する手順です。

  • Document オブジェクトを作成し、それにセクションを追加します。
  • ヘッダー行と他の行のデータを準備し、それぞれ1次元の文字列配列と2次元の文字列配列に格納します。
  • Section.addTable() メソッドを使用して、セクションにテーブルを追加します。
  • ヘッダー行にデータを挿入し、行の高さ、背景色、テキストの配置など、行の書式を設定します。
  • 残りの行にデータを挿入し、これらの行に書式を適用します。
  • Document.saveToFile() メソッドを使用して、ドキュメントを別のファイルに保存します。
  • 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) {

        //Documentオブジェクトを作成する
        Document document = new Document();

        //セクションを追加する
        Section section = document.addSection();

        //表のデータを定義する
        String[] header = {"国家名", "首都", "大陸", "地域番号", "人口"};
        String[][] data =
                {
                        new String[]{"アルゼンチン", "ブエノスアイレス", "南米", "2777815", "32300003"},
                        new String[]{"ボリビア", "ラパス", "南米", "1098575", "7300000"},
                        new String[]{"ブラジル", "ブラジリア", "南米", "8511196", "150400000"},
                        new String[]{"カナダ", "オタワ", "北米", "9976147", "26500000"},
                        new String[]{"チリ", "サンティアゴ", "南米", "756943", "13200000"},
                        new String[]{"コロンビア", "ボゴタ", "南米", "1138907", "33000000"},
                        new String[]{"キューバ", "ハバナ", "北米", "114524", "10600000"},
                        new String[]{"エクアドル", "キト", "南米", "455502", "10600000"},
                        new String[]{"エルサルバドル", "サンサルバドル", "北米", "20865", "5300000"},
                        new String[]{"ガイアナ", "ジョージタウン", "南米", "214969", "800000"},

                };

        //表を追加する
        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 txtRange = p.appendText(header[i]);
            txtRange.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);
                dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
            }
        }

        //セルの背景色を設定する
        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("output/CreateTable.docx", FileFormat.Docx_2013);
    }
}

Java:Word で表を作成する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 674 times