addTable

addTable

BASIC / ADVANCED / PREMIUM

Adds tables into the Excel spreadsheet.

Description
public addTable($contents, $position, $tableStyles = array(), $options = array())

This method allows adding a table in the active sheet.

Parameters

contents

A multidimensional array with the contents and styles to be added. Check addCell to see all available options.

position

Cell position in the active sheet: A1, C3, AB7...

tableStyles

The possible keys and values of this array are:

Key Type Description
bandedColumn bool Banded column option. Default as false.
bandedRow bool Banded row option. Default as true.
firstColumn bool First column option. Default as false.
headerRow bool Header row option. Default as false.
lastColumn bool Last column option. Default as false.
tableStyle string Table style name. MS Excel includes built-in table style names: TableStyleMedium2, TableStyleLight3...
totalRow bool Total row option. Default as false.

options

The possible keys and values of this array are:

Key Type Description
columnNames array Custom column names. If not set, 'Column ' is used to generate names.
columnTotals array
  • 'cellStyles' => check addCell to see all available options.
  • 'contentStyles' => check addCell to see all available options.
  • 'type' => (string) 'formula', 'function', 'label'.
  • 'value' => (string) formula (string), function (average, count, countNums, max, min, stdDev, sum, var), label (string). To be used with the previous 'type' option.
description string Alternative text description.
filters array

custom (array):

  • 'andMode' => (bool). if true, enable 'and' option. Default as false.
  • 'operator' => (string) equal, greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual, notEqual.
  • 'value' => (string) ? (any single character), * (any series of characters).

values (array)

tableName string Custom table name. If not set a new table name is generated automatically.
title string Alternative text title.
Return values

Array with the position range added by the method.

Exceptions

Table name exists in the spreadsheet.

Code samples

Example #1

The resulting XLSX looks like:

Example #2

The resulting XLSX looks like:

Example #3

The resulting XLSX looks like:

Example #4

The resulting XLSX looks like:

Release notes
  • phpxlsx 2.5:
    • title and description options.
    • apply content types to contents and totals.
    • return the position range.
  • phpxlsx 2.0:
    • filters.
  • phpxlsx 1.0:
    • new method.