addCell

addCell

BASIC / ADVANCED / PREMIUM

Adds cell contents into the Excel spreadsheet.

Description
public addCell($contents, $position, $cellStyles = array(), $options = array())

This method allows adding contents into cells setting a position.

Parameters

contents

This could be:

  • a string
  • an array with the following keys and values:
Key Type Description
text string The content to be inserted.
bold bool If true the content will be shown in bold characters.
color string Hexadecimal color value: 'FF0000', '000000'...
font string Font family: 'Arial', 'Calibri'...
fontSize int Font size in points.
italic bool If true displays the content in italics.
strikethrough bool If true displays text in strikethrough.
subscript bool If true displays text in subscript.
superscript bool If true displays text in superscript.
underline string Underlines text: single, double.
  • a multidimensional array to add rich text contents

position

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

cellStyles

Key Type Description
backgroundColor string Hexadecimal color value: 'FFFF00', 'CCCCCC'...
border string Border type: thin, thick, dashed, double, mediumDashDotDot, hair... This value can be overridden for each side with 'borderTop', 'borderRight', 'borderBottom', 'borderLeft' and 'borderDiagonal' properties.
borderColor string Hexadecimal color value: 'FFFF00', 'CCCCCC'... This value can be overridden for each side with 'borderColorTop', 'borderColorRight', 'borderColorBottom', 'borderColorLeft' and 'borderColorDiagonal' properties.
cellStyleName string Cell style name.
  • Custom style name.
  • Preset style names: Bad, Calculation, Check Cell, Comma, Currency, Explanatory Text, Good, Heading 1, Heading 2, Heading 3, Heading 4, Input, Linked Cell, Neutral, Normal, Note, Output, Percent, Warning Text.
Applying a cell style name ignores other cell styles.
horizontalAlign string left, center, right.
indent int Indent value.
isFunction bool Set the content as function. Default as false. If not set, if the content starts with = , isFunction is set as true (set isFunction as false to avoid this).
locked bool Locked property.
rotation int Orientation degrees.
shrinkToFit bool If true enables shrink to fit.
textDirection string context, ltr, rtl.
type string Cell type: general (default), number, currency, accounting, date, time, percentage, fraction, scientific, text, special, boolean.
typeOptions array Keys and values:
  • formatCode (string)
verticalAlign string top, center, bottom.
wrapText bool If true enables wrap text.

options

Key Type Description
insertMode string Insert mode if the position contains an existing content: 'replace', 'ignore'. Default as replace.
raw bool Add the raw content if true. Default as false.
useCellStyles bool Use existing cell styles. Default as false.
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:

Example #5

The resulting XLSX looks like:

Example #6

The resulting XLSX looks like:

Release notes
  • phpxlsx 1.0:
    • new method.