0
0

More than 3 years have passed since last update.

Java Word文書でテーブルを作成

Posted at

ワードで表(テーブル)を作成・編集ができるようになれば具体的かつ詳細な資料を作成することが可能です。今回は、Spire.Doc for Javaという使いやすいライブラリを活用して、Word文書でテーブルを作成する方法を紹介していきます。

下準備

1.E-iceblueの公式サイトからFree Spire.doc for Java無料版をダウンロードしてください。

f:id:lendoris:20210825151531p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.doc.jarを参照に追加してください。

f:id:lendoris:20210825151541p:plain


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", "女", "17", "0109"},
                        new String[]{"Lois", "女", "20", "0111"},
                        new String[]{"Jois", "男", "11", "0110"},
                        new String[]{"Moon", "女", "22", "0112"},
                        new String[]{"Vinit", "女", "11", "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("CreateTable.docx", FileFormat.Docx_2013);
    }
}


実行結果


f:id:lendoris:20210825152437p:plain


 

0
0
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
0
0