チュートリアル

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

チュートリアル».NET»Spire.Presentation for .NET»»C#/VB.NET:PowerPoint でテーブルのセルを結合および分割する方法
2023-09-19

C#/VB.NET:PowerPoint でテーブルのセルを結合および分割する方法

PowerPoint でセルの結合と分割は、テーブルのレイアウトと構造を調整するために主に使用される共通の機能です。セルの結合は、隣接するセルを1つの大きなセルに結合します。これにより、複数の列または行にわたるタイトルセルを作成できます。一方、セルの分割は、セルを複数の小さなセルに分割することを意味し、詳細なレイアウトの作成や異なるコンテンツの配置に役立ちます。この記事では、Spire.Presentation for .NET を使用して、PowerPoint でテーブルのセルを結合および分割する方法を示します。

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

まず、Spire.Presentation for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。

PM> Install-Package Spire.Presentation

PowerPoint でテーブルのセルを結合する

Spire.Presentation for .NET は、ITable[int columnIndex, int rowIndex] プロパティと ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) メソッドを使用して、特定のセルを取得し結合することをサポートしています。詳細な手順は以下の通りです。

  • Presentation クラスのオブジェクトを作成します。
  • Presentation.LoadFromFile() メソッドを使用してサンプルファイルをロードします。
  • すべての図形をループして、最初のスライドからテーブルを取得します。
  • ITable[int columnIndex, int rowIndex] プロパティで特定のセルを取得します。ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) メソッドを使用してそれらを結合します。
  • Presentation.SaveToFile() メソッドを使用して結果ファイルを保存します。
  • C#
  • VB.NET
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

C#/VB.NET:PowerPoint でテーブルのセルを結合および分割する方法

PowerPoint でテーブルのセルを分割する

Spire.Presentation for .NET は、ITable[int columnIndex, int rowIndex] プロパティと Cell.Split(int RowCount, int ColunmCount) メソッドを使用して、特定のセルを取得し、それをより小さなセルに分割することもサポートしています。詳細な手順は以下の通りです。

  • Presentation クラスのオブジェクトを作成します。
  • Presentation.LoadFromFile() メソッドを使用してサンプルファイルをロードします。
  • すべての図形をループして、最初のスライドからテーブルを取得します。
  • ITable[int columnIndex, int rowIndex] プロパティで特定のセルを取得します。Cell.Split(int RowCount, int ColumnCount) メソッドを使用してそのセルを2 行と 2 列に分割します。
  • Presentation.SaveToFile() メソッドを使用して結果ファイルを保存します。
  • C#
  • VB.NET
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

C#/VB.NET:PowerPoint でテーブルのセルを結合および分割する方法

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

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

Read 293 times