Source for file Format.php
Documentation is available at Format.php
* Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
* The majority of this is _NOT_ my code. I simply ported it from the
* PERL Spreadsheet::WriteExcel module.
* The author of the Spreadsheet::WriteExcel module is John McNamara
* I _DO_ maintain this code, and John McNamara has nothing to do with the
* porting of this code to PHP. Any questions directly related to this
* class library should be directed to me.
* PHPExcel_Writer_Excel5_Writer: A library for generating Excel Spreadsheets
* Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* Class for generating Excel XF records (formats)
* @author Xavier Noguer <xnoguer@rezebra.com>
* @package PHPExcel_Writer_Excel5
* The index given by the workbook when creating a new format.
* Index to the FONT record.
* Height of font (1/20 of a point)
* Bit specifiying if the font is italic.
* Index to the cell's color
* The text underline property
* Bit specifiying if the font has strikeout.
* Bit specifiying if the font has outline.
* Bit specifiying if the font has shadow.
* 2 bytes specifiying the script type for the font.
* Byte specifiying the font family.
* Byte specifiying the font charset.
* An index (2 bytes) to a FORMAT record (number format).
* Bit specifying if formulas are hidden.
* Bit specifying if the cell is locked.
* The three bits specifying the text horizontal alignment.
* Bit specifying if the text is wrapped at the right border.
* The three bits specifying the text vertical alignment.
* 1 bit, apparently not used.
* Integer specifying the text rotation. (-90 to +90 or -165 indicating stacked text)
* The cell's foreground color.
* The cell's background color.
* The cell's background fill pattern.
* Style of the bottom border of the cell
* Color of the bottom border of the cell.
* Style of the top border of the cell
* Color of the top border of the cell.
* Style of the left border of the cell
* Color of the left border of the cell.
* Style of the right border of the cell
* Color of the right border of the cell.
* @param integer $index the XF index for the format.
* @param array $properties array with properties to be set on initialization.
$this->_BIFF_version = $BIFF_version;
$this->_diag_color = 0x40;
// Set properties passed to PHPExcel_Writer_Excel5_Workbook::addFormat()
foreach ($properties as $property => $value)
$method_name = 'set'. ucwords($property);
$this->$method_name($value);
* Generate an Excel BIFF XF record (style or cell).
* @param string $style The type of the XF record ('style' or 'cell').
* @return string The XF record
// Set the type of the XF record and some of the attributes.
// Flags to indicate if attributes have been set.
// Zero the default border colour if the border has not been set.
$record = 0x00E0; // Record identifier
if ($this->_BIFF_version == 0x0500) {
$length = 0x0010; // Number of bytes to follow
if ($this->_BIFF_version == 0x0600) {
$ifnt = $this->font_index; // Index to FONT record
if ($this->_BIFF_version == 0x0500) {
//$align |= $this->_rotation << 8;
$align |= $atr_num << 10;
$align |= $atr_fnt << 11;
$align |= $atr_alc << 12;
$align |= $atr_bdr << 13;
$align |= $atr_pat << 14;
$align |= $atr_prot << 15;
$icv = $this->_fg_color; // fg and bg pattern colors
$fill = $this->_pattern; // Fill and border line style
$border1 = $this->_top; // Border line style and color
$border1 |= $this->_left << 3;
$border1 |= $this->_right << 6;
$header = pack("vv", $record, $length);
$data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
} elseif ($this->_BIFF_version == 0x0600) {
$used_attrib = $atr_num << 2;
$used_attrib |= $atr_fnt << 3;
$used_attrib |= $atr_alc << 4;
$used_attrib |= $atr_bdr << 5;
$used_attrib |= $atr_pat << 6;
$used_attrib |= $atr_prot << 7;
$icv = $this->_fg_color; // fg and bg pattern colors
$border1 = $this->_left; // Border line style and color
$border1 |= $this->_right << 4;
$border1 |= $this->_top << 8;
$diag_tl_to_rb = 0; // FIXME: add method
$diag_tr_to_lb = 0; // FIXME: add method
$border1 |= $diag_tl_to_rb << 30;
$border1 |= $diag_tr_to_lb << 31;
$border2 |= $this->_diag_color << 14;
$border2 |= $this->_diag << 21;
$header = pack("vv", $record, $length);
//BIFF8 options: identation, shrinkToFit and text direction
$data = pack("vvvC", $ifnt, $ifmt, $style, $align);
//$data .= pack("CCC", $rotation, $biff8_options, $used_attrib);
$data .= pack("CCC", $angle, $biff8_options, $used_attrib);
$data .= pack("VVv", $border1, $border2, $icv);
* Generate an Excel BIFF FONT record.
* @return string The FONT record
$dyHeight = $this->_size * 20; // Height of font (1/20 of a point)
$icv = $this->_color; // Index to color palette
$bls = $this->_bold; // Bold style
$encoding = 0; // TODO: Unicode support
$record = 0x31; // Record identifier
if ($this->_BIFF_version == 0x0500) {
$length = 0x0F + $cch; // Record length
} elseif ($this->_BIFF_version == 0x0600) {
$reserved = 0x00; // Reserved
$grbit = 0x00; // Font attributes
$header = pack("vv", $record, $length);
if ($this->_BIFF_version == 0x0500) {
$data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
$bCharSet, $reserved, $cch);
} elseif ($this->_BIFF_version == 0x0600) {
$data = pack("vvvvvCCCCCC", $dyHeight, $grbit, $icv, $bls,
$bCharSet, $reserved, $cch, $encoding);
* Returns a unique hash key for a font.
* Used by PHPExcel_Writer_Excel5_Workbook::_storeAllFonts()
* The elements that form the key are arranged to increase the probability of
* generating a unique key. Elements that hold a large range of numbers
* (eg. _color) are placed between two binary elements such as _italic
* @return string A key for this font
* Returns the index used by PHPExcel_Writer_Excel5_Worksheet::_XF()
* @return integer The index for the XF record
* Used in conjunction with the set_xxx_color methods to convert a color
* string into a number. Color range is 0..63 but we will restrict it
* to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
* @param string $name_color name of the color (i.e.: 'blue', 'red', etc..). Optional.
* @return integer The color index
// Return the default color, 0x7FFF, if undef,
// or the color string converted to an integer,
if (isset ($colors[$name_color])) {
return($colors[$name_color]);
// or the default color if string is unrecognised,
// or an index < 8 mapped into the correct range,
// return($name_color + 8);
// or the default color if arg is outside range,
// or an integer in the valid range
* @param string $location alignment for the cell ('left', 'right', etc...).
return; // Ignore numbers
if ($location == 'left') {
if ($location == 'centre') {
if ($location == 'center') {
if ($location == 'right') {
if ($location == 'fill') {
if ($location == 'justify') {
if ($location == 'merge') {
if ($location == 'equal_space') { // For T.K.
if ($location == 'top') {
if ($location == 'vcentre') {
if ($location == 'vcenter') {
if ($location == 'bottom') {
if ($location == 'vjustify') {
if ($location == 'vequal_space') { // For T.K.
* Set cell horizontal alignment.
* @param string $location alignment for the cell ('left', 'right', etc...).
return; // Ignore numbers
if ($location == 'left') {
if ($location == 'centre') {
if ($location == 'center') {
if ($location == 'right') {
if ($location == 'fill') {
if ($location == 'justify') {
if ($location == 'merge') {
if ($location == 'equal_space') { // For T.K.
* Set cell vertical alignment.
* @param string $location alignment for the cell ('top', 'vleft', 'vright', etc...).
return; // Ignore numbers
if ($location == 'top') {
if ($location == 'vcentre') {
if ($location == 'vcenter') {
if ($location == 'bottom') {
if ($location == 'vjustify') {
if ($location == 'vequal_space') { // For T.K.
* This is an alias for the unintuitive setAlign('merge')
* Sets the boldness of the text.
* Bold has a range 100..1000.
* 0 (400) is normal. 1 (700) is bold.
* @param integer $weight Weight for the text, 0 maps to 400 (normal text),
1 maps to 700 (bold text). Valid range is: 100-1000.
It's Optional, default is 1 (bold).
$weight = 0x2BC; // Bold text
$weight = 0x190; // Normal text
$weight = 0x190; // Lower bound
$weight = 0x190; // Upper bound
/************************************
* FUNCTIONS FOR SETTING CELLS BORDERS
* Sets the width for the bottom border of the cell
* @param integer $style style of the cell border. 1 => thin, 2 => thick.
* Sets the width for the top border of the cell
* @param integer $style style of the cell top border. 1 => thin, 2 => thick.
* Sets the width for the left border of the cell
* @param integer $style style of the cell left border. 1 => thin, 2 => thick.
* Sets the width for the right border of the cell
* @param integer $style style of the cell right border. 1 => thin, 2 => thick.
* Set cells borders to the same style
* @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
/*******************************************
* FUNCTIONS FOR SETTING CELLS BORDERS COLORS
* Sets all the cell's borders to the same color
* @param mixed $color The color we are setting. Either a string (like 'blue'),
* or an integer (range is [8...63]).
* Sets the cell's bottom border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
* Sets the cell's top border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
* Sets the cell's left border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
* Sets the cell's right border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
* Sets the cell's foreground color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
if ($this->_pattern == 0) { // force color to be seen
* Sets the cell's background color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
if ($this->_pattern == 0) { // force color to be seen
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
* Sets the fill pattern attribute of a cell
* @param integer $arg Optional. Defaults to 1. Meaningful values are: 0-18,
* 0 meaning no background.
* Sets the underline of the text
* @param integer $underline The value for underline. Possible values are:
* 1 => underline, 2 => double underline.
* Sets the font style as italic
* @param integer $size The font size (in pixels I think).
* Sets the orientation of the text
* @param integer $rotation The rotation angle for the text (-90 to 90, -165 stacked text)
// throw new Exception("Invalid value for angle.".
// " Possible values are: 0, 90, 270 and -1 ".
// "for stacking top-to-bottom.");
* Sets the numeric format.
* It can be date, time, currency, etc...
* @param integer $num_format The numeric format.
* Sets font as strikeout.
* Sets outlining for a font.
* Sets the script type of the text
* @param integer $script The value for script type. Possible values are:
* 1 => superscript, 2 => subscript.
* Unlocks a cell. Useful for unprotecting particular cells of a protected sheet.
* Sets the font family name.
* @param string $fontfamily The font family name. Possible values are:
* 'Times New Roman', 'Arial', 'Courier'.
|