JavaでPowerPointの表を作成、操作、削除する


テーブルは、PowerPointでデータを提示するための最も有用なツールの一つです.観客が簡単に読むことができますし、分析するように行と列のデータを整理します.この記事では、Spire.Presentation for Javaライブラリを使用してJavaでPowerPointの表を作成、操作、および削除する方法を示します.

依存関係を追加する


あなたはいずれかの尖塔の瓶をダウンロードすることができます.このwebsiteからJavaに対するプレゼンテーション、またはMavenベースのプロジェクトのPOMに次の構成を追加することでMavenからインストールします.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>5.3.0</version>    
    </dependency>    
</dependencies>

JavaでPowerPointでテーブルを作成する


PowerPointスライドにテーブルを追加する主な手順は次のとおりです.
  • プレゼンテーションインスタンスを作成し、プレゼンテーションを使用して目的のスライドを取得します.getSlide ()getメソッドを返します.
  • は、列幅の二重配列および行高さの二重配列を定義する.
  • ISLIDEを使用してスライドに定義済みの列幅と行の高さを持つテーブルを追加します.getshapes ().AppendTable ( x , y , columnwidth , rowheights )メソッド.
  • はデータの二次元ストリング配列を定義します.
  • すべてのテーブル行と列を通して
  • ループし、テーブルを使用して各テーブルセルにデータを割り当てます.入手してください.gettext ().settext ()メソッド.
  • Tableを使用したテーブルスタイルを設定します.setStylePreset ()メソッド.
  • プレゼンテーションを使用して結果ドキュメントを保存します.savetofile ()メソッド.
  • import com.spire.presentation.*;
    
    public class CreateTable {
        public static void main(String []args) throws Exception {
            //Create a Presentation instance
            Presentation presentation = new Presentation();
            //Get the first slide
            ISlide slide = presentation.getSlides().get(0);
    
            //Define 5 columns with widths
            Double[] widths = new Double[]{100d, 100d, 150d, 100d, 100d};
            //Define 8 rows with heights
            Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};
    
            //Add a 5 x 8 table to the first slide
            ITable table = slide.getShapes().appendTable(90, 90, widths, heights);
    
            //Define data
            String[][] data = new String[][]
                    {
                            {"Name", "Capital", "Continent", "Area", "Population"},
                            {"Venezuela", "Caracas", "South America", "912047", "19700000"},
                            {"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                            {"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                            {"Canada", "Ottawa", "North America", "9976147", "26500000"},
                            {"Chile", "Santiago", "South America", "756943", "13200000"},
                            {"Colombia", "Bagota", "South America", "1138907", "33000000"},
                            {"Cuba", "Havana", "North America", "114524", "10600000"},
                    };
    
            //Loop through table rows and columns
            for (int row = 0; row < table.getTableRows().getCount(); row++) {
                for (int col = 0; col < table.getColumnsList().getCount(); col++) {
                    //Set text alignment of the first table row to Center
                    table.get(col, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
                    //Assign data to each table cell
                    table.get(col, row).getTextFrame().setText(data[row][col]);
                    //Set font name and height
                    table.get(col, row).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial"));
                    table.get(col, row).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(12);
                }
            }
    
            //Set table style
            table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);
    
            //Save the result document
            presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
        }
    }
    

    JavaでPowerPointで既存のテーブルを操作する


    スライドの既存のテーブルを操作するには、まず最初にアクセスする必要があります.スライドが1つのテーブルだけを含む場合は、スライド内のすべての図形をループすることができます.ただし、1つ以上のテーブルが含まれている場合は、別のテキストを使用して目的のテーブルを取得します.
    既存のテーブルへのアクセス方法とマージ固有のセルの表示方法を次のコード例に示します
    import com.spire.presentation.FileFormat;
    import com.spire.presentation.ISlide;
    import com.spire.presentation.ITable;
    import com.spire.presentation.Presentation;
    
    public class MergeCells {
        public static void main(String []args) throws Exception {
    
            //Create a Presentation instance
            Presentation presentation = new Presentation();
            //Load a PowerPoint document
            presentation.loadFromFile("AddTable.pptx");
    
            //Get the first slide
            ISlide slide = presentation.getSlides().get(0);
    
            //Create an ITable object and set it to null
            ITable table = null;
    
            //Loop through all shapes in the first slide
            for (Object shape : slide.getShapes()) {
                //Get the table
                if (shape instanceof ITable) {
                    table = (ITable) shape;
                    //Merge the 2nd cell and the 3rd cell of the first table column 
                    table.mergeCells(table.get(0, 1), table.get(0, 2), false);
                    //Merge the 5th cells of the 4th and the 5th table columns 
                    table.mergeCells(table.get(3, 4), table.get(4, 4), true);
                }
            }
    
            //Save the result document
            presentation.saveToFile("MergeCells.pptx", FileFormat.PPTX_2013);
        }
    }
    

    JavaでPowerPointからテーブルを削除する


    テーブルを削除するには、現在の図形が有益なオブジェクトであることがわかった場合は、スライド内のすべての図形をループすることができますiSlideを呼び出します.getshape.remove ()メソッドを使用して、スライドから削除します.次のコード例を参照してください.
    import com.spire.presentation.*;
    
    public class RemoveTable {
        public static void main(String []args) throws Exception {
            //Create a Presentation instance
            Presentation presentation = new Presentation();
            //Load a PowerPoint document
            presentation.loadFromFile("AddTable.pptx");
    
            //Get the first slide
            ISlide slide = presentation.getSlides().get(0);
    
            //Create an ITable object and set it to null
            ITable table = null;
    
            //Loop through all shapes in the first slide
            for (int i = slide.getShapes().getCount()-1; i >=0; i--) {
                IShape shape = slide.getShapes().get(i);
                //If any table is found
                if (shape instanceof ITable) {
                    slide.getShapes().remove(shape);
                }
            }
    
            //Save the result document
            presentation.saveToFile("RemoveTable.pptx", FileFormat.PPTX_2013);
        }
    }