How to Work with Tables in Java

How to Work with Tables in Java

Aspose.Slides FOSS for Java supports creating tables on slides with configurable column widths and row heights. This guide shows how to add a table, populate it with data, and apply basic text formatting to cells.

Step-by-Step Guide

Step 1: Add the Maven Dependency

<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

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().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.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().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():

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().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 formatting to header cells using PortionFormat:

import org.aspose.slides.foss.*;

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

import org.aspose.slides.foss.*;

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()) {
            ISlide slide = prs.getSlides().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.getFillFormat().setFillType(FillType.SOLID);
        cell.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