LoginSignup
1
0

Javaを使用してPowerPointテーブルを生成および操作する方法

Last updated at Posted at 2022-03-15

テーブルを通してデータを表示することはプレーンテキストにより明確であるため、表はPowerPointプレゼンテーションに表示されることがよくあります。 この投稿では、無料のPowerPoint API(Free Spire.Presentation for Java)を使用してJavaアプリケーションでPowerPointテーブルを生成および操作する方法を紹介します。

jarファイルをインポートします

開始する前に、jarファイルをインポートする必要があります。Free Spire.Presentation for Javaをダウンロードして解凍し、jarパッケージをlibフォルダーからJavaアプリケーションにインポートします。

コード一覧

  1. テーブルを作成する
import com.spire.presentation.*;
public class AddTable {
    public static void main(String[] args) throws Exception {
        //PowerPointドキュメントを作成する
        Presentation presentation = new Presentation();

        Double[] widths = new Double[]{150d, 100d, 100d, 100d, 100d};
        Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d};

        //一番目のスライドにテーブルを追加する
        ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);

        String[][] dataStr = new String[][]
                {
                        {"会社", "一月", "二月", "三月", "計"},
                        {"札幌分会社", "7", "7", "5", "19"},
                        {"福岡分会社", "6", "4", "7", "17"},
                        {"大阪分会社", "8", "7", "9", "24"},
                        {"計", "21", "18", "21", "60"},
                };

        //テーブルのスタイルを設定する
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                table.get(j, i).getTextFrame().setText(dataStr[i][j]);
                //フォントとテキストの配置を設定する
                table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Yu Mincho"));
                table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
            }
        }

        //テーブルのスタイルを設定する
        table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);

        //ドキュメントを保存する
        presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
    }
}

01.png

  1. テーブルを操作する
    既存のテーブルを操作するには、最初にテーブルオブジェクトを取得する必要があります。
//PowerPointドキュメントをロードする
Presentation ppt = new Presentation();
ppt.loadFromFile("AddTable.pptx");
//テーブルオブジェクトを取得する
ITable table = (ITable) ppt.getSlides().get(0).getShapes().get(0);

一部の一般的な操作
行と列を追加する

table.getTableRows().append(table.getTableRows().get(0));
table.getColumnsList().add(table.getColumnsList().get(0));

行と列を挿入する

table.getTableRows().insert(0, table.getTableRows().get(0));
table.getColumnsList().insert(0, table.getColumnsList().get(0));

行の高さと列の幅を設定する

table.getTableRows().get(0).setHeight(50);
table.getColumnsList().get(0).setWidth(100);

行と列を削除する

table.getTableRows().removeAt(0, false);
table.getColumnsList().removeAt(0, false);

セルを結合する

table.mergeCells(table.get(0,0), table.get(0,1), false);

セルを分割する

table.get(0,0).Split(3,2);

セルを画像で塗りつぶす

table.get(0,0).getFillFormat().setFillType(FillFormatType.PICTURE);
table.get(0,0).getFillFormat().getPictureFill().getPicture().setUrl((new java.io.File("bkg.jpg")).getAbsolutePath());

セルを色で塗りつぶす

table.get(0,1).getFillFormat().setFillType(FillFormatType.SOLID);
table.get(0,1).getFillFormat().getSolidColor().setColor(Color.blue);

テーブルボーダー

table.setTableBorder(TableBorderType.All, 1, Color.black);

操作が完了したら、ドキュメントを保存するのを忘れずに

ppt.saveToFile("ManipulateTable.pptx", FileFormat.PPTX_2013);

今回のPowerPointテーブルを生成および操作する方法についての紹介はここまででした、最後まで読んでいただきありがとうございます。

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