この短いチュートリアルでは、Java を使用してスライドに表を挿入する方法について説明します。アプリケーションを作成するための IDE を設定するために必要な詳細と、このタスクを実行するための詳細な手順を共有します。最後に、Java を使用して PowerPoint で表を作成する方法を示す実行可能なサンプル コードを取得し、結果のプレゼンテーションを PPTX、PPT、またはその他の形式でディスクに保存します。 MS PowerPoint やその他のサードパーティ製ツールをインストールせずに、サポートされている形式に変換できます。
Java を使用して PowerPoint に表を追加する手順
- Aspose.Slides を使用してテーブルを挿入するように IDE を設定します
- Presentation クラスを使用して新しいプレゼンテーションを作成し、最初のスライドにアクセスします
- addTable() メソッドを使用し、テキストの位置とセルの高さと幅を指定して、選択したスライドに新しい表を追加します
- すべての行を反復処理し、フォント設定とともにテキストを設定します
- テーブルを含むディスクにプレゼンテーションを保存します
前述の手順では、Java を使用して PowerPoint で表を作成する方法 を説明しています。タスクを達成するために必要なすべての必要なクラスとメソッドが識別されます。表がスライドに追加され、そのセルにサンプル テキストが入力されるプロセスは簡単です。
Java を使用して PowerPoint でテーブルを作成するコード
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package testslides; | |
import com.aspose.slides.BulletType; | |
import com.aspose.slides.ICell; | |
import com.aspose.slides.IRow; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ITable; | |
import com.aspose.slides.ITextFrame; | |
import com.aspose.slides.License; | |
import com.aspose.slides.Presentation; | |
public class AddTable { | |
public static void main(String[] args) throws Exception {//handle exception during adding any tablle | |
String path = "/Users/mudassirkhan/Documents/KnowledgeBase/TestData/"; | |
// Load the product license to create a table | |
License slidesTablelic = new License(); | |
slidesTablelic.setLicense(path + "Conholdate.Total.Product.Family.lic"); | |
// Create a new presentation | |
Presentation presentationForTable = new Presentation(); | |
// Access the first slide in the presentation | |
ISlide slide = presentationForTable.getSlides().get_Item(0); | |
// Define the arrays containing column widths and row heights | |
double[] dColumnsWidths = { 55, 55, 55 }; | |
double[] dRowsHeights = { 55, 36, 36, 36, 36 }; | |
// Add a new table with desired rows and columns | |
ITable newTable = slide.getShapes().addTable(60, 60, dColumnsWidths, dRowsHeights); | |
// Traverse through all the rows | |
for (IRow tableRow : newTable.getRows()) | |
{ | |
// Traverse through all the cells in the current row | |
for (ICell tableCell : tableRow) | |
{ | |
// Access the respective text frame of the cell | |
ITextFrame txtFormat = tableCell.getTextFrame(); | |
// Add some text to the cell | |
int iFirstRowIndex = tableCell.getFirstRowIndex(); | |
int iFirstColumnIndex = tableCell.getFirstColumnIndex(); | |
txtFormat.setText( "Text " + iFirstRowIndex + " " + iFirstColumnIndex); | |
// Set the font related property for the newly added text | |
txtFormat.getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setFontHeight(10); | |
txtFormat.getParagraphs().get_Item(0).getParagraphFormat().getBullet().setType(BulletType.None); | |
} | |
} | |
// Save the presentation with table | |
presentationForTable.save("PresentationTable.pptx", com.aspose.slides.SaveFormat.Pptx); | |
System.out.println("Done"); | |
} | |
} |
このコードは、Java を使用して PowerPoint で表を作成する方法 を示しています。さまざまな機能を使用するために複数のインターフェイス クラスが使用されます。たとえば、ISlide はスライドへのアクセスに使用され、ITable クラスはテーブルの操作に使用され、IRow クラスと ICell クラスは個々のセルの操作に使用されます。 ITextFrame クラスは、セル内のテキストを設定し、セルの書式設定を設定するために使用されるメイン インターフェイスです。
このチュートリアルでは、Java を使用して PowerPoint プレゼンテーションで表を作成する方法 を学びました。プレゼンテーションから HTML を作成するプロセスを知りたい場合は、Java を使用して HTML で PowerPoint スライドを作成する方法 の記事を参照してください。