チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
PowerPoint における表は、データを整理・表示するための非常に有用なツールです。ビジネスプレゼンテーションや財務報告書を作成する際に、表を使用してデータを表示して、文書の内容をよりわかりやすく魅力的なものにすることができます。この記事では、Spire.Presentation for Java を使用して PowerPoint スライドに表を追加する方法を示します。
まず、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.8.1</version>
</dependency>
</dependencies>
指定した PowerPoint スライドに表を作成するには、ISlide.getShapes().appendTable() メソッドを使用できます。Spire.Presentation for Java では、表のスタイルをフォーマットすることもできます。以下は詳細な手順です。
import com.spire.presentation.*;
public class AddTable {
public static void main(String[] args) throws Exception {
//Presentationインスタンスを作成する
Presentation presentation = new Presentation();
//最初のスライドを取得する
ISlide slide = presentation.getSlides().get(0);
//表内の行と列の数やサイズを指定するために、2つのdouble arrayを定義する
Double[] widths = new Double[]{130d, 130d, 120d, 120d, 120d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};
//指定された数とサイズの行と列を持つ表をスライドに追加する
ITable table = slide.getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 300, 90, widths, heights);
//表のデータを指定する
String[][] dataStr = new String[][]
{
{"国名", "首都", "大陸", "面積", "人口"},
{"ベネズエラ", "カラカス", "南アメリカ", "912,047", "19,700,000"},
{"ボリビア", "ラパス", "南アメリカ", "1,098,575", "7,300,000"},
{"ブラジル", "ブラジリア", "南アメリカ", "8,511,196", "15,040,0000"},
{"カナダ", "オタワ", "北アメリカ", "9,976,147", "26,500,000"},
{"チリ", "サンティアゴ", "南アメリカ", "756,943", "13,200,000"},
{"コロンビア", "ボゴタ", "南アメリカ", "1,138,907", "33,000,000"},
{"キューバ", "ハバナ", "北アメリカ", "114,524", "10,600,000"},
};
//配列をループし、表にデータを入力する
for (int i = 0; i < 8; 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);
presentation.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
セルの結合は、隣接するセルを1つの大きなセルに結合することを意味します。これにより、複数の列や行にわたるタイトルセルを作成し、より柔軟なデザインが可能になります。一方、セルの分割は、セルを複数の小さなセルに分割することを意味し、詳細なレイアウトの作成や多様なコンテンツの収容に役立ちます。PowerPointでは、ユーザーは表の構造とレイアウトを調整するために、表のセルを結合したり分割したりすることができます。この記事では、Spire.Presentation for Java を使用して PowerPoint で表のセルを結合および分割する方法を示します。
まず、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>
Spire.Presentation for Java は、特定のセルを取得して結合するための ITable.get(int columnIndex, int rowIndex) および ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) メソッドをユーザーに提供します。詳細な手順は次のとおりです。
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();
}
}
Spire.Presentation for Java では、ITable.get(int columnIndex, int rowIndex) メソッドと Cell.split(int RowCount, int ColunmCount) メソッドを呼び出すことで、特定のセルを取得して小さなセルに分割することもできます。詳細な手順は次のとおりです。
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();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。