Create Table in PowerPoint with a free Java API

If there’s a large amount of data that needs to be displayed in a PowerPoint document, you can add a table to it to make the data display more intuitive. This article will introduce how to create a table and set table style in a PowerPoint document using Free Spire.Presentation for Java.

Installation (2 Methods)
● Download the free API (Free Spire.Presentation for Java) and unzip it, then add the Spire.Presentation.jar file to your project as dependency.

● Directly add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Relevant code snippet

import com.spire.presentation.*;

public class AddTable {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();

Double[] widths = new Double[]{100d, 100d, 150d, 100d, 100d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};

//Add a table
ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);
String[][] dataStr = 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"},
{"Ecuador", "Quito", "South America", "455502", "10600000"},
{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
{"Peru", "Lima", "South America", "1285215", "21600000"},
{"Jamaica", "Kingston", "North America", "11424", "2500000"},
{"Mexico", "Mexico City", "North America", "1967180", "88600000"}
};
//Add data to table
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 5; j++) {
//Fill the table with data
table.get(j, i).getTextFrame().setText(dataStr[i][j]);

//Set font
table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial Narrow"));
}
}
//Set the alignment of the first table row to Center
for (int i = 0; i < 5; i++) {
table.get(i, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
}
//Set the style of table
table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);

//Save the document
presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
}
}

Sharing Java Code