この記事では、C# を使用して PowerPoint で表を作成する方法について説明します。環境を確立するためのすべての詳細、テーブルを作成して入力するための段階的なプロセス、およびC# を使用してスライドにテーブルを挿入する方法を示す実行可能なサンプル コードを提供します。また、表内のテキストの書式を設定し、結果のプレゼンテーションを PPT、PPTX、または MS PowerPoint でサポートされているその他の形式でディスクに保存する方法も学びます。
C# を使用して PowerPoint で表を作成する手順
- Aspose.Slides for .NET を追加する環境を確立して、テーブルを追加します
- Presentation クラスを使用して新しいプレゼンテーションを作成し、最初のスライドにアクセスします
- AddTable() メソッドを使用して、行と列の定義された高さを持つテーブルをスライドに追加します
- 新しく追加されたテーブルの各行とセルを反復処理します
- 各セルにテキストを設定し、そのフォントを設定します
- プレゼンテーションを PPT 形式で保存する
これらの手順では、C# を使用して PowerPoint で表を作成する方法 について説明します。まず、プレゼンテーションを作成し、既定で図形のコレクションを含む最初のスライドにアクセスできます。次の手順では、行の高さと列の幅を表す整数配列と共に、セル内のテキストの左上位置の X 座標と Y 座標を指定してテーブルを作成します。最後の手順では、出力ファイルを保存する前に、ITextFrame クラス オブジェクトを作成し、要件に従ってテキスト パラグラフの書式設定を設定します。
C# を使用して PowerPoint にテーブルを追加するコード
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to add table in a slide using C# | |
{ | |
// Initialize license | |
License lic = new License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Instantiate a new presentation | |
Presentation presentation = new Presentation(); | |
// Access the first slide from the default collection | |
ISlide sld = presentation.Slides[0]; | |
// Specify the rows heights and columns widths | |
double[] columnsWidths = { 45, 45, 45 }; | |
double[] rowsHeights = { 45, 26, 26, 26, 26 }; | |
// Insert a new table | |
Aspose.Slides.ITable table = sld.Shapes.AddTable(55, 55, columnsWidths, rowsHeights); | |
// Fill the table and set the font | |
foreach (IRow row in table.Rows) | |
{ | |
foreach (ICell cell in row) | |
{ | |
// Access the cell's text frame | |
ITextFrame textFormat = cell.TextFrame; | |
// Set text in the cell | |
textFormat.Text = "Data " + cell.FirstRowIndex.ToString() + cell.FirstColumnIndex.ToString(); | |
// Set text font | |
textFormat.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 10; | |
textFormat.Paragraphs[0].ParagraphFormat.Bullet.Type = BulletType.None; | |
} | |
} | |
// Save the presentation on the disk | |
presentation.Save("PresentationTable.ppt", SaveFormat.Ppt); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
このチュートリアルでは、C# を使用してプレゼンテーションにテーブルを挿入する方法について説明しました。 PowerPoint プレゼンテーションを保護するプロセスを知りたい場合は、C# で PowerPoint プレゼンテーションを保護する方法 の記事を参照してください。