Practical phpxlsx

Adding contents

Introduction

To explain how the library works, let's create a new XLSX and add different contents to it.

New XLSX

As a first step, create the XLSX, instantiating the class CreateXlsx:

Now, add the new contents for this $xlsx object. Contents are added to the active sheet.

Sheets

The default XLSX includes one sheet. To add a new sheet, call addSheet:

For this spreadsheet sample, insert a new sheet:

A default name is added to the new sheet, that can be changed using the 'name' option:

Cells

After at least one sheet exists in the spreadsheet, new contents can be added using addCell:

This method includes the following parameters:

  • $contents: string or array with contents and styles
  • $position: cell position in the active sheet (A1, C3, AB7...)
  • $cellStyles: array with cell styles to be applied
  • $options: extra options

Add to the active sheet two cell contents with styles at positions 'A1', 'A2' and 'C3':

The cell format type is detected automatically if the 'type' option is not set. The following code adds numeric and boolean format types autodetecting them:

The cell format type can be changed using the 'type' option, so a custom format can be used instead of the autodetected one:

To set a new cell value keeping existing styles, the addCell method can be used:

Or setCellValue, that is an alias of the previous code:

Instead of adding each cell content individually with addCell, the addCellRange method can be used to add a range of cell contents:

The available options of the method are the same as addCell, but $contents that is always an array with the new contents.

These contents can be added horizontally from the chosen position, each array value fills one cell:

Contents can also be added vertically from the chosen position:

Horizontally and vertically contents can be added at the same time:

New contents are always added to the active sheet. An XLSX only has one active sheet.

phpxlsx reads and uses the active sheet from the XLSX: the first sheet when creating a new XLSX from scratch or the active sheet of the workbook when using an XLSX template.

phpxlsx includes an internal active sheet value that can be changed using the setActiveSheet method. This internal active sheet value doesn't change the active sheet of the workbook, so it can be used to add contents to sheets keeping the preset active sheet.

After creating a new sheet, the active sheet can be changed to the new one. The following code adds contents to the active sheet (the first one), inserts a new sheet and then changes the internal active sheet to the second sheet to include another content:

Functions

addFunction is the method for adding functions:

This method includes the following parameters:

  • $function: function to be added
  • $position: cell position in the current active sheet (A1, C3, AB7...)
  • $contentStyles: array with the content styles to be applied
  • $cellStyles: array with the cell styles to be applied
  • $options: extra options

Let's add two numbers and a SUM function:

Breaks

To add breaks, use addBreak:

You just need to choose the type of break, 'row' or 'col', and the numeric position.

For example, to add row and col breaks:

Links

To add a link, use the addLink method:

This method includes the following parameters:

  • $link: URL or #location
  • $position: cell position in the current active sheet (A1, C3, AB7...)
  • $contents: array with the text contents and styles
  • $cellStyles: array with the cell styles to be applied
  • $options: extra options

Here's a simple example:

Tips and tricks

The value of the internal active sheet can be returned using the getActiveSheet method.

A link fills the whole cell. The same cell can't contain a link and a text content without the link.

The addCellRange method returns the range of the added contents.

If a content is added in a cell position where another content exists, the new content replaces the old one. The 'insertMode' option allows changing this default behavior choosing 'replace' (default) or 'ignore'.

setActiveSheet allows using -1 as position to choose the last sheet.

Defined names can be added using addDefinedName.

Next - Working with templates