Tuesday, 12 November 2019 06:49

Add or Delete Rows and Columns from PowerPoint Table in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to add or delete rows and columns in a PowerPoint table using Spire.Presentation for Java.

Here is a screenshot of the sample PowerPoint file:

Add or Delete Rows and Columns from PowerPoint Table in Java

Add a row and a column

import com.spire.presentation.*;

public class AddRowAndColumn {

    public static void main(String[] args) throws Exception {

        //load the sample PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the table in the document
        ITable table = null;
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;

                //add the last row to the end of the table as a new row
                int rowCount = table.getTableRows().getCount();
                TableRow row = table.getTableRows().get(rowCount - 1);
                table.getTableRows().append(row);

                //get the new row and set the text for each cell
                rowCount = table.getTableRows().getCount();
                row = table.getTableRows().get(rowCount - 1);
                row.get(0).getTextFrame().setText("America");
                row.get(1).getTextFrame().setText("Washington");
                row.get(2).getTextFrame().setText("North America");
                row.get(3).getTextFrame().setText("9372610");

                //add the last column to the end of the table as a new column
                int colCount = table.getColumnsList().getCount();
                TableColumn column =table.getColumnsList().get(colCount-1);
                table.getColumnsList().add(column);

                //get the new column and set the text for each cell
                colCount = table.getColumnsList().getCount();
                column = table.getColumnsList().get(colCount-1);
                column.get(0).getTextFrame().setText("Population");
                column.get(1).getTextFrame().setText("32370000");
                column.get(2).getTextFrame().setText("7350000");
                column.get(3).getTextFrame().setText("15040000");
                column.get(4).getTextFrame().setText("26500000");
                column.get(5).getTextFrame().setText("329740000");
            }
        }
        
        //save the document
        presentation.saveToFile("output/AddRowAndColumn.pptx", FileFormat.PPTX_2013);
    }
}

Add or Delete Rows and Columns from PowerPoint Table in Java

Delete a row and a column

import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

public class DeleteRowAndColumn {

    public static void main(String[] args) throws Exception {

        //load the sample PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the table in the document
        ITable table = null;
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;
                
                //delete the second column
                table.getColumnsList().removeAt(1, false);
                
                //delete the second row
                table.getTableRows().removeAt(1, false);
            }
        }
        
        //save the document
        presentation.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013);
    }
}

Add or Delete Rows and Columns from PowerPoint Table in Java

Additional Info

  • tutorial_title: