Tables in PowerPoint are a valuable tool for organizing and presenting data in a clear and concise manner. When creating business presentations or financial reports, you can insert tables to present data effectively and make your presentations more impactful and attractive to your audience. This article will demonstrate how to programmatically add a table to a presentation slide using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your 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>9.10.2</version> </dependency> </dependencies>
Insert a Table in PowerPoint in Java
To create a table in a specified PowerPoint slide, you can use the ISlide.getShapes().appendTable() method. With Spire.Presentation for Java, you are also allowed to format the style of the table. The following are the detailed steps.
- Create a Presentation instance.
- Get a specified slide using Presentation.getSlides().get() method.
- Define two double arrays to specify the number and size of rows and columns in the table.
- Add a table with the specified number and size of rows and columns to the slide using ISlide.getShapes().appendTable() method.
- Define some data and then then fill the table with the data using ITable.get().getTextFrame().setText() method.
- Set the text font and alignment of the table.
- Set a built-in table style using ITable.setStylePreset() method.
- Save the result document using Presentation.saveToFile() method.
- Java
import com.spire.presentation.*; public class AddTable { 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 two double arrays to specify the number and size of rows and columns in the table 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 with the specified number and size of rows and columns to the slide ITable table = slide.getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights); //Specify table data 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 the text font table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Calibri")); //Set the text alignment of the table table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER); } } //Set the table style table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1); //Save the result document presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.