A table is one of the most useful tools for presenting data in PowerPoint. It organizes data in rows and columns so that audiences can easily read and analyze them. This article will demonstrate how to create, manipulate and remove tables in PowerPoint in Java using Spire.Presentation for Java library.
Add Dependencies
You can either download the jar of Spire.Presentation for Java from this website or install it from maven by adding the following configurations to your maven-based project’s pom.xml file.
<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>
Create a Table in PowerPoint in Java
The following are the main steps to add a table to a PowerPoint slide:
- Create a Presentation instance and get the desired slide using Presentation.getSlides().get(slideIndex) method.
- Define a Double array of column widths and a Double array of row heights.
- Add a table with predefined column widths and row heights to the slide using ISlide.getShapes().appendTable(x, y, columnWidths, rowHeights) method.
- Define a two-dimensional String array of data.
- Loop through all table rows and columns, assign the data to each table cell using table.get(columnIndex, rowIndex).getTextFrame().setText() method.
- Set table style using ITable.setStylePreset() method.
- Save the result document using Presentation.saveToFile() method.
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);
}
}
Manipulate an Existing Table in PowerPoint in Java
To manipulate an existing table on a slide, you first need to access it. If the slide contains only one table, you can loop through all shapes in the slide, determine if the current shape is a ITable object, if yes, typecast it as a ITable object. However, if the slide contains more than one table, you’d better get the desired table using its alternative text.
The following code example shows how to access an existing table and merge specific cells:
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);
}
}
Remove a Table from PowerPoint in Java
To remove a table, you can loop through all shapes in the slide, if the current shape is found to be a ITable object, call ISlide.getShapes.remove() method to remove it from the slide. Refer to the following code example.
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);
}
}
Top comments (0)