How to Work with Tables in Java

How to Work with Tables in Java

This guide shows how to create tables on PowerPoint slides using Aspose.Slides FOSS for Java. Call slide.getShapes().addTable(x, y, colWidths, rowHeights) to add a table, access cells via table.getRows().get(row).get(col), and apply text formatting through IPortionFormat on each cell.

Step-by-Step Guide

Step 1: Add the Maven Dependency

Add the following dependency to your pom.xml to include Aspose.Slides FOSS in your project:

<dependency>
  <groupId>org.aspose.slides.foss</groupId>
  <artifactId>aspose-slides-foss</artifactId>
  <version>1.0.0</version>
</dependency>

Step 2: Create or Open a Presentation

Use try-with-resources to construct a Presentation, access the first slide via prs.getSlides().get(0), and save to PPTX when done:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ITable;
import org.aspose.slides.foss.ICell;
import org.aspose.slides.foss.IParagraphCollection;
import org.aspose.slides.foss.IPortionFormat;
import org.aspose.slides.foss.NullableBool;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.Color;

try (Presentation prs = new Presentation()) {
    ISlideCollection slides = prs.getSlides();
    ISlide slide = slides.get(0);
    // ... add table ...
    prs.save("table.pptx", SaveFormat.PPTX);
}

Step 3: Define Column Widths and Row Heights

Tables require explicit column widths and row heights in points (1 point = 1/72 inch). A standard slide is 720 points wide and 540 points tall.

double[] colWidths = {200.0, 150.0, 150.0};   // 3 columns
double[] rowHeights = {45.0, 40.0, 40.0};     // 3 rows

Step 4: Add the Table

slide.getShapes().addTable(x, y, colWidths, rowHeights) creates the table at position (x, y):

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ITable;
import org.aspose.slides.foss.ICell;
import org.aspose.slides.foss.IParagraphCollection;
import org.aspose.slides.foss.IPortionFormat;
import org.aspose.slides.foss.NullableBool;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.Color;

try (Presentation prs = new Presentation()) {
    ISlideCollection slides = prs.getSlides();
    ISlide slide = slides.get(0);

    double[] colWidths = {200.0, 150.0, 150.0};
    double[] rowHeights = {45.0, 40.0, 40.0};
    ITable table = slide.getShapes().addTable(50, 100, colWidths, rowHeights);

    prs.save("table.pptx", SaveFormat.PPTX);
}

Step 5: Set Cell Text

Access cells via table.getRows().get(rowIndex).get(colIndex) and assign text through .getTextFrame().setText(). Row and column indices are zero-based, so the header row is index 0 and data rows start at index 1:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ITable;
import org.aspose.slides.foss.ICell;
import org.aspose.slides.foss.IParagraphCollection;
import org.aspose.slides.foss.IPortionFormat;
import org.aspose.slides.foss.NullableBool;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.Color;

try (Presentation prs = new Presentation()) {
    ISlideCollection slides = prs.getSlides();
    ISlide slide = slides.get(0);

    double[] colWidths = {200.0, 150.0, 150.0};
    double[] rowHeights = {45.0, 40.0, 40.0};
    ITable table = slide.getShapes().addTable(50, 100, colWidths, rowHeights);

    // Header row (row 0)
    String[] headers = {"Product", "Units Sold", "Revenue"};
    for (int col = 0; col < headers.length; col++) {
        table.getRows().get(0).get(col).getTextFrame().setText(headers[col]);
    }

    // Data rows
    String[][] data = {
        {"Widget A", "1,200", "$24,000"},
        {"Widget B", "850", "$17,000"},
    };
    for (int row = 0; row < data.length; row++) {
        for (int col = 0; col < data[row].length; col++) {
            table.getRows().get(row + 1).get(col)
                .getTextFrame().setText(data[row][col]);
        }
    }

    prs.save("sales-table.pptx", SaveFormat.PPTX);
}

Step 6: Format Header Cell Text

Apply bold font formatting to header cells by accessing the first paragraph’s IPortionFormat via cell.getTextFrame().getParagraphs().get(0).getPortions().get(0).getPortionFormat() and calling fmt.setFontBold(NullableBool.TRUE):

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ITable;
import org.aspose.slides.foss.ICell;
import org.aspose.slides.foss.IParagraphCollection;
import org.aspose.slides.foss.IPortionFormat;
import org.aspose.slides.foss.NullableBool;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.Color;

for (int col = 0; col < headers.length; col++) {
    ICell cell = table.getRows().get(0).get(col);
    IParagraphCollection paragraphs = cell.getTextFrame().getParagraphs();
    if (paragraphs.size() > 0 && paragraphs.get(0).getPortions().size() > 0) {
        IPortionFormat fmt = paragraphs.get(0).getPortions().get(0)
            .getPortionFormat();
        fmt.setFontBold(NullableBool.TRUE);
        fmt.getFillFormat().setFillType(FillType.SOLID);
        fmt.getFillFormat().getSolidFillColor().setColor(
            Color.fromArgb(255, 255, 255, 255)
        );
    }
}

Complete Working Example

The following self-contained program creates a regional revenue table with a bold header row, populates four data rows, and saves the result as a PPTX file:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ITable;
import org.aspose.slides.foss.ICell;
import org.aspose.slides.foss.IParagraphCollection;
import org.aspose.slides.foss.IPortionFormat;
import org.aspose.slides.foss.NullableBool;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.Color;

public class CreateTable {
    public static void main(String[] args) {
        String[][] dataRows = {
            {"North", "$1.2M", "+8%"},
            {"South", "$0.9M", "+4%"},
            {"East",  "$1.5M", "+12%"},
            {"West",  "$0.7M", "+2%"},
        };
        String[] headers = {"Region", "Revenue", "Growth"};

        try (Presentation prs = new Presentation()) {
            ISlideCollection slides = prs.getSlides();
            ISlide slide = slides.get(0);

            double[] colWidths = {180.0, 140.0, 120.0};
            double[] rowHeights = new double[dataRows.length + 1];
            rowHeights[0] = 45.0;
            for (int i = 1; i < rowHeights.length; i++) {
                rowHeights[i] = 38.0;
            }

            ITable table = slide.getShapes().addTable(
                60, 80, colWidths, rowHeights
            );

            // Header row
            for (int col = 0; col < headers.length; col++) {
                ICell cell = table.getRows().get(0).get(col);
                cell.getTextFrame().setText(headers[col]);
                if (cell.getTextFrame().getParagraphs().size() > 0
                    && cell.getTextFrame().getParagraphs().get(0)
                        .getPortions().size() > 0) {
                    cell.getTextFrame().getParagraphs().get(0)
                        .getPortions().get(0).getPortionFormat()
                        .setFontBold(NullableBool.TRUE);
                }
            }

            // Data rows
            for (int row = 0; row < dataRows.length; row++) {
                for (int col = 0; col < dataRows[row].length; col++) {
                    table.getRows().get(row + 1).get(col)
                        .getTextFrame().setText(dataRows[row][col]);
                }
            }

            prs.save("regional-revenue.pptx", SaveFormat.PPTX);
        }
        System.out.println("Saved regional-revenue.pptx");
    }
}

Common Issues and Fixes

IndexOutOfBoundsException when accessing table.getRows().get(row).get(col)

Row and column indices are zero-based. If you defined rowHeights with 3 elements, valid row indices are 0, 1, 2.

Cell text does not appear in the saved file

Always assign through .getTextFrame().setText(value). Access cells as table.getRows().get(rowIndex).get(colIndex).getTextFrame().setText("value").

Table position is off the slide

Check that x + sum(colWidths) <= 720 and y + sum(rowHeights) <= 540 for a standard slide.


Frequently Asked Questions

Can I merge table cells?

Cell merging (mergeCells) is declared in the API but raises UnsupportedOperationException in this edition.

Can I apply a table-wide background color?

Apply fill formatting to each individual cell:

for (int row = 0; row < table.getRows().size(); row++) {
    for (int col = 0; col < table.getColumns().size(); col++) {
        ICell cell = table.getRows().get(row).get(col);
        cell.getCellFormat().getFillFormat().setFillType(FillType.SOLID);
        cell.getCellFormat().getFillFormat().getSolidFillColor().setColor(
            Color.fromArgb(255, 240, 248, 255)
        );
    }
}

Can I set a table style preset?

Yes. Use table.setStylePreset(TableStylePreset.MEDIUM_STYLE_2_ACCENT_1) to apply a built-in table style.


See Also

 English