Working with Tables

Working with Tables

Aspose.Cells FOSS for Java supports Excel tables (also known as ListObjects or structured references). You can create new tables from cell ranges, inspect existing tables, configure header and totals rows, apply built-in table styles, access individual columns, and convert tables back to normal ranges. The primary entry point is ListObjectCollection, accessed via Worksheet.getListObjects().


Overview

Tables on a worksheet are managed through the ListObjectCollection returned by Worksheet.getListObjects(). Each ListObject represents one Excel table and exposes its display name, comment, cell range bounds, column collection, style settings, and header/totals row visibility.

A new table is created with ListObjectCollection.add(startRow, startColumn, endRow, endColumn, hasHeaders), which returns the zero-based index of the newly created table. An alternative overload accepts cell-name strings: ListObjectCollection.add(startCellName, endCellName, hasHeaders).


Key Classes

ClassDescription
ListObjectRepresents a single Excel table. Provides access to name, comment, range, columns, style, and header/totals visibility.
ListObjectCollectionCollection of all tables on a worksheet. Supports count, index/name access, add, remove, and style-type conversion helpers.
ListColumnRepresents one column within a table. Exposes column ID, name, and totals-row calculation settings.
ListColumnCollectionOrdered collection of columns in a table. Iterable with count and index access.
TableStyleTypeEnum identifying a built-in Excel table style (Light 1–21, Medium 1–28, Dark 1–11, or Custom/None).
TotalsCalculationEnum for the aggregation function on a table’s totals row (SUM, COUNT, AVERAGE, MAX, MIN, STD_DEV, VAR, COUNT_NUMS, CUSTOM, NONE).

Accessing Tables on a Worksheet

Retrieve the ListObjectCollection from any Worksheet via Worksheet.getListObjects(). Use ListObjectCollection.getCount() to determine how many tables exist. Individual tables are accessed by zero-based index through ListObjectCollection.get(index) or by display name through ListObjectCollection.get(name).

Workbook wb = new Workbook("input.xlsx");
Worksheet ws = wb.getWorksheets().get(0);
ListObjectCollection tables = ws.getListObjects();

for (int i = 0; i < tables.getCount(); i++) {
    ListObject table = tables.get(i);
    System.out.println("Table: " + table.getDisplayName()
        + " rows: " + table.getStartRow() + "-" + table.getEndRow());
}

Creating a New Table

Use ListObjectCollection.add() to insert a table over a cell range. The hasHeaders parameter indicates whether the first row of the range contains column headers.

Workbook wb = new Workbook();
Worksheet ws = wb.getWorksheets().get(0);

// Populate data
ws.getCells().get("A1").putValue("Product");
ws.getCells().get("B1").putValue("Quantity");
ws.getCells().get("A2").putValue("Widget");
ws.getCells().get("B2").putValue(100);

// Create table over A1:B2 with headers in row 0
int tableIndex = ws.getListObjects().add(0, 0, 1, 1, true);
ListObject table = ws.getListObjects().get(tableIndex);
table.setDisplayName("SalesTable");

Inspecting Table Properties

Once you hold a ListObject reference you can read:

  • ListObject.getDisplayName() — the table’s display name string.
  • ListObject.getComment() — an optional comment associated with the table.
  • ListObject.getStartRow() / ListObject.getStartColumn() — top-left cell of the table range.
  • ListObject.getEndRow() / ListObject.getEndColumn() — bottom-right cell of the table range.
  • ListObject.isShowHeaderRow() — whether the header row is visible.
  • ListObject.isShowTotals() — whether the totals row is visible.
  • ListObject.getListColumns() — the ListColumnCollection for the table.

Styling Tables

Apply a built-in table style using ListObject.setTableStyleType(TableStyleType). The TableStyleType enum includes Light (1–21), Medium (1–28), and Dark (1–11) variants plus NONE and CUSTOM.

ListObject table = ws.getListObjects().get(0);
table.setTableStyleType(TableStyleType.TABLE_STYLE_MEDIUM_2);

Additional style toggles control banded rows, banded columns, and first/last column emphasis:

  • ListObject.setShowTableStyleRowStripes(boolean)
  • ListObject.setShowTableStyleColumnStripes(boolean)
  • ListObject.setShowTableStyleFirstColumn(boolean)
  • ListObject.setShowTableStyleLastColumn(boolean)

You can also set a custom style name with ListObject.setTableStyleName(String).

Helper methods ListObjectCollection.parseTableStyleType(String) and ListObjectCollection.tableStyleTypeName(TableStyleType) convert between the enum and its string representation.


Working with Table Columns

Access individual columns through ListObject.getListColumns(), which returns a ListColumnCollection. Each ListColumn exposes:

  • ListColumn.getId() — the one-based OOXML column identifier.
  • ListColumn.getName() / ListColumn.setName(String) — the column header text.
  • ListColumn.getTotalsCalculation() / ListColumn.setTotalsCalculation(TotalsCalculation) — the aggregation function for the totals row.
  • ListColumn.getTotalsRowLabel() / ListColumn.setTotalsRowLabel(String) — a custom label in the totals row.
  • ListColumn.getTotalsRowFormula() / ListColumn.setTotalsRowFormula(String) — a custom formula for the totals row.
ListObject table = ws.getListObjects().get(0);
ListColumnCollection columns = table.getListColumns();
for (int i = 0; i < columns.getCount(); i++) {
    ListColumn col = columns.get(i);
    System.out.println("Column " + col.getId() + ": " + col.getName());
}

Configuring the Totals Row

Enable the totals row with ListObject.setShowTotals(true), then set per-column aggregation functions using the TotalsCalculation enum.

ListObject table = ws.getListObjects().get(0);
table.setShowTotals(true);

ListColumn qtyColumn = table.getListColumns().get(1);
qtyColumn.setTotalsCalculation(TotalsCalculation.SUM);

ListColumn labelColumn = table.getListColumns().get(0);
labelColumn.setTotalsRowLabel("Total");

Resizing and Converting Tables

Resize a table to cover a different cell range using ListObject.resize(startRow, startColumn, endRow, endColumn, hasHeaders).

Convert a table back to a normal cell range with ListObject.convertToRange(). This removes the table structure while preserving the cell data and formatting.

Remove a table entirely from the collection with ListObjectCollection.removeAt(index).


AutoFilter on Tables

Show or remove the column drop-down auto-filter arrows on a table:

  • ListObject.showAutoFilter() — display filter arrows on the header row.
  • ListObject.removeAutoFilter() — hide the filter arrows.

API Reference Summary

ListObject

MethodReturn typeDescription
getDisplayName()StringDisplay name of the table.
setDisplayName(name)voidSets the display name.
getComment()StringOptional comment for the table.
setComment(comment)voidSets the table comment.
getStartRow()intStart row index of the table range.
getStartColumn()intStart column index of the table range.
getEndRow()intEnd row index of the table range.
getEndColumn()intEnd column index of the table range.
isShowHeaderRow()booleanWhether the header row is visible.
setShowHeaderRow(show)voidShows or hides the header row.
isShowTotals()booleanWhether the totals row is visible.
setShowTotals(show)voidShows or hides the totals row.
getTableStyleType()TableStyleTypeThe built-in table style.
setTableStyleType(type)voidApplies a built-in table style.
getTableStyleName()StringCustom table style name.
setTableStyleName(name)voidSets a custom table style name.
getListColumns()ListColumnCollectionThe table’s column collection.
resize(...)voidResizes the table to a new range.
convertToRange()voidConverts the table to a normal range.
showAutoFilter()voidDisplays auto-filter arrows.
removeAutoFilter()voidHides auto-filter arrows.

ListObjectCollection

MethodReturn typeDescription
getCount()intNumber of tables on the worksheet.
get(index)ListObjectReturns the table at the given zero-based index.
get(name)ListObjectReturns the table with the given display name.
add(startRow, startColumn, endRow, endColumn, hasHeaders)intCreates a new table; returns its index.
add(startCellName, endCellName, hasHeaders)intCreates a new table from cell names; returns its index.
removeAt(index)voidRemoves the table at the given index.
parseTableStyleType(name)TableStyleTypeConverts a string to a TableStyleType enum value.
tableStyleTypeName(type)StringConverts a TableStyleType enum to its string name.

ListColumn

MethodReturn typeDescription
getId()intOne-based OOXML column identifier.
getName()StringColumn header text.
setName(name)voidSets the column header text.
getTotalsCalculation()TotalsCalculationTotals row aggregation function.
setTotalsCalculation(calc)voidSets the totals row aggregation.
getTotalsRowLabel()StringCustom label in the totals row.
setTotalsRowLabel(label)voidSets the totals row label.
getTotalsRowFormula()StringCustom formula for the totals row.
setTotalsRowFormula(formula)voidSets the totals row formula.

Limitations

  • Table data bindings are not separately modifiable; the table covers the range specified at creation time. Use resize() to adjust the covered range.
  • Custom table styles must be defined in the workbook before referencing them by name via setTableStyleName().

See Also