チュートリアル

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

チュートリアル»Java»Spire.Presentation for Java»»Java:PowerPoint で表のセルを結合および分割する方法
2023-08-30

Java:PowerPoint で表のセルを結合および分割する方法

セルの結合は、隣接するセルを1つの大きなセルに結合することを意味します。これにより、複数の列や行にわたるタイトルセルを作成し、より柔軟なデザインが可能になります。一方、セルの分割は、セルを複数の小さなセルに分割することを意味し、詳細なレイアウトの作成や多様なコンテンツの収容に役立ちます。PowerPointでは、ユーザーは表の構造とレイアウトを調整するために、表のセルを結合したり分割したりすることができます。この記事では、Spire.Presentation for Java を使用して PowerPoint で表のセルを結合および分割する方法を示します。

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

まず、Spire.Presentation 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.presentation</artifactId>
        <version>8.7.3</version>
    </dependency>
</dependencies>

PowerPoint で表のセルを結合する

Spire.Presentation for Java は、特定のセルを取得して結合するための ITable.get(int columnIndex, int rowIndex) および ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) メソッドをユーザーに提供します。詳細な手順は次のとおりです。

  • Presentation クラスのオブジェクトを作成します。
  • Presentation.loadFromFile() メソッドを使用してサンプルファイルをロードします。
  • ITable 変数を宣言します。
  • すべての形状をループして、最初のスライドから表を取得します。
  • ITable.get(int columnIndex, int rowIndex) メソッドを使用して特定のセルを取得し、ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) メソッドを使用してそれらを結合します。
  • Presentation.saveToFile() メソッドを使用して結果ファイルを保存します。
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

public class MergeCells {

    public static void main(String[] args) throws Exception {

        //Presentationクラスのオブジェクトを作成する
        Presentation presentation = new Presentation();

        //サンプルファイルをロードする
        presentation.loadFromFile("sample.pptx");

        //ITable 変数を宣言する
        ITable table = null;

        //すべての形状をループして、最初のスライドから表を取得する
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;

                //セルを[0、0]から[3、0]までのセルを結合する
                table.mergeCells(table.get(0, 0), table.get(3, 0), false);

                //セルを[0、1]から[0、5]までのセルを結合する
                table.mergeCells(table.get(0, 1), table.get(0, 5), false);
            }
        }

        //結果ファイルを保存する
        presentation.saveToFile("MergeCells.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Java:PowerPoint で表のセルを結合および分割する方法

PowerPoint で表のセルを分割する

Spire.Presentation for Java では、ITable.get(int columnIndex, int rowIndex) メソッドと Cell.split(int RowCount, int ColunmCount) メソッドを呼び出すことで、特定のセルを取得して小さなセルに分割することもできます。詳細な手順は次のとおりです。

  • Presentation クラスのオブジェクトを作成します。
  • Presentation.loadFromFile() メソッドを使用してサンプルファイルをロードします。
  • ITable 変数を宣言します。
  • すべての形状をループして、最初のスライドから表を取得します。
  • ITable.get(int columnIndex, int rowIndex) メソッドを使用して特定のセルを取得し、Cell.split(int RowCount, int ColumnCount) メソッドを使用して、そのセルを2 行と 2 列に分割します。
  • Presentation.saveToFile() メソッドを使用して結果ファイルを保存します。
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

public class SplitCells {

    public static void main(String[] args) throws Exception {

        //Presentationクラスのオブジェクトを作成する
        Presentation presentation = new Presentation();

        //サンプルファイルをロードする
        presentation.loadFromFile("sample.pptx");

        //ITable 変数を宣言する
        ITable table = null;

        //すべての形状をループして、最初のスライドから表を取得する
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;

                //セル[2,2]を2行2列に分割する
                table.get(2,2).split(2,2);
            }
        }

        //結果ファイルを保存する
        presentation.saveToFile("SplitCells.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Java:PowerPoint で表のセルを結合および分割する方法

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

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

Read 370 times