チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
PowerPoint のテーブルは、ユーザーが明確、簡潔、そして視覚的に魅力的な方法でデータを表示および整理できる強力なツールです。 テーブルを使用すると、複雑な情報を効果的に他の人に伝達し、理解しやすくすることもできます。この記事では、Spire.Presentation for .NET を使用して C# で PowerPoint プレゼンテーションにテーブルを挿入する方法を示します。
まず、Spire.Presentation for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Presentation
ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights) メソッドを使用して、PowerPoint プレゼンテーションの特定のスライドにテーブルを追加できます。詳細な手順は次のとおりです。
using Spire.Presentation;
namespace InsertTable
{
internal class Program
{
static void Main(string[] args)
{
//Presentation クラスのインスタンスを初期化する
Presentation presentation = new Presentation();
//PowerPoint プレゼンテーションをロードする
presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
//最初のスライドを取得する
ISlide slide = presentation.Slides[0];
//テーブル内の行数、列数、サイズを指定するために使用される 2 つの double 配列、幅と高さを定義する
double[] widths = new double[] { 100, 150, 150, 100, 100 };
double[] heights = new double[] { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
//指定された行数と列数とサイズを持つテーブルをスライド上の特定の位置に追加する
ITable table = slide.Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 90, widths, heights);
//テーブルのデータを 2 次元文字列配列に格納する
string[,] data = new string[,]{
{"名前","首都","大陸","面積","人口"},
{"ベネズエラ","カラカス","南アメリカ","912047","19700000"},
{"ボリビア","ラパス","南アメリカ","1098575","7300000"},
{"ブラジル","ブラジリア","南アメリカ","8511196","150400000"},
{"カナダ","オタワ","北米","9976147","26500000"},
{"チリ","サンティアゴ","南アメリカ","756943","13200000"},
{"コロンビア","ボゴタ","南アメリカ","1138907","33000000"},
{"キューバ","ハバナ","北米","114524","10600000"},
{"エクアドル","キト","南アメリカ","455502","10600000"},
{"パラグアイ","アスンシオン","南アメリカ","406576","4660000"},
{"ペルー","リマ","南アメリカ","1285215","21600000"},
{"ジャマイカ","キングストン","北米","11424","2500000"},
{"メキシコ","メキシコシティ","北米","1967180","88600000"}
};
//文字列配列をループし、テーブルの各セルにデータを割り当てる
for (int i = 0; i < 13; i++)
for (int j = 0; j < 5; j++)
{
//テーブルの各セルにデータを入力する
table[j, i].TextFrame.Text = data[i, j];
//フォント名とフォントサイズを設定する
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Yu Mincho");
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 14;
}
//テーブルの最初の行の配置を中央に設定する
for (int i = 0; i < 5; i++)
{
table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
}
//テーブルにスタイルを適用する
table.StylePreset = TableStylePreset.MediumStyle2Accent6;
//プレゼンテーションをファイルに保存する
presentation.SaveToFile("C:\Users\Administrator\Desktop\InsertTable.pptx", FileFormat.Pptx2013);
presentation.Dispose();
}
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
PowerPoint でセルの結合と分割は、テーブルのレイアウトと構造を調整するために主に使用される共通の機能です。セルの結合は、隣接するセルを1つの大きなセルに結合します。これにより、複数の列または行にわたるタイトルセルを作成できます。一方、セルの分割は、セルを複数の小さなセルに分割することを意味し、詳細なレイアウトの作成や異なるコンテンツの配置に役立ちます。この記事では、Spire.Presentation for .NET を使用して、PowerPoint でテーブルのセルを結合および分割する方法を示します。
まず、Spire.Presentation for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Presentation
Spire.Presentation for .NET は、ITable[int columnIndex, int rowIndex] プロパティと ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) メソッドを使用して、特定のセルを取得し結合することをサポートしています。詳細な手順は以下の通りです。
using Spire.Presentation;
namespace MergeCells
{
class Program
{
static void Main(string[] args)
{
//Presentationクラスのオブジェクトを作成する
Presentation presentation = new Presentation();
//PowerPointプレゼンテーションをロードする
presentation.LoadFromFile("sample.pptx");
//すべての図形をループして、最初のスライドからテーブルを取得する
ITable table = null;
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
table = (ITable)shape;
//[0,0]から[4,0]にセルを結合する
table.MergeCells(table[0, 0], table[4, 0], false);
}
}
//結果ファイルを保存する
presentation.SaveToFile("MergeCells.pptx", FileFormat.Pptx2010);
presentation.Dispose();
}
}
}
Imports Spire.Presentation
Namespace MergeCells
Class Program
Private Shared Sub Main(ByVal args() As String)
'Presentationクラスのオブジェクトを作成する
Dim presentation As Presentation = New Presentation
'PowerPointプレゼンテーションをロードする
presentation.LoadFromFile("sample.pptx")
'すべての図形をループして、最初のスライドからテーブルを取得する
Dim table As ITable = Nothing
For Each shape As IShape In presentation.Slides(0).Shapes
If (TypeOf shape Is ITable) Then
table = CType(shape,ITable)
'[0,0]から[4,0]にセルを結合する
table.MergeCells(table(0, 0), table(4, 0), false)
End If
Next
'結果ファイルを保存する
presentation.SaveToFile("MergeCells.pptx", FileFormat.Pptx2010)
presentation.Dispose
End Sub
End Class
End Namespace
Spire.Presentation for .NET は、ITable[int columnIndex, int rowIndex] プロパティと Cell.Split(int RowCount, int ColunmCount) メソッドを使用して、特定のセルを取得し、それをより小さなセルに分割することもサポートしています。詳細な手順は以下の通りです。
using Spire.Presentation;
namespace SplitCells
{
class Program
{
static void Main(string[] args)
{
//Presentationクラスのオブジェクトを作成する
Presentation presentation = new Presentation();
//PowerPointプレゼンテーションをロードする
presentation.LoadFromFile("sample.pptx");
//すべての図形をループして、最初のスライドからテーブルを取得する
ITable table = null;
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
table = (ITable)shape;
//セル[2,2]を2行2列に分割する
table[2, 2].Split(2, 2);
}
}
//結果ファイルを保存する
presentation.SaveToFile("SplitCells.pptx", FileFormat.Pptx2013);
presentation.Dispose();
}
}
}
Imports Spire.Presentation
Namespace SplitCells
Class Program
Private Shared Sub Main(ByVal args() As String)
'Presentationクラスのオブジェクトを作成する
Dim presentation As Presentation = New Presentation
'PowerPointプレゼンテーションをロードする
presentation.LoadFromFile("sample.pptx")
'すべての図形をループして、最初のスライドからテーブルを取得する
Dim table As ITable = Nothing
For Each shape As IShape In presentation.Slides(0).Shapes
If (TypeOf shape Is ITable) Then
table = CType(shape,ITable)
'セル[2,2]を2行2列に分割する
table(2, 2).Split(2, 2)
End If
Next
'結果ファイルを保存する
presentation.SaveToFile("SplitCells.pptx", FileFormat.Pptx2013)
presentation.Dispose
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。