Source for file Excel5.php
Documentation is available at Excel5.php
* Copyright (c) 2006 - 2008 PHPExcel
* 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 tshhe GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.6.3, 2008-08-25
// Original file header of ParseXL (used as the base for this class):
// --------------------------------------------------------------------------------
// Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
// trex005, and mmp11 (SourceForge.net)
// http://sourceforge.net/projects/phpexcelreader/
// Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
// Modelled moreso after Perl Excel Parse/Write modules
// Added Parse_Excel_Spreadsheet object
// Reads a whole worksheet or tab as row,column array or as
// associated hash of indexed rows and named column fields
// Added variables for worksheet (tab) indexes and names
// Added an object call for loading individual woorksheets
// Changed default indexing defaults to 0 based arrays
// Fixed date/time and percent formats
// Includes patches found at SourceForge...
// unicode patch by nobody
// unpack("d") machine depedency patch by matchy
// boundsheet utf16 patch by bjaenichen
// Renamed functions for shorter names
// General code cleanup and rigor, including <80 column width
// Included a testcase Excel file and PHP example calls
// Code works for PHP 5.x
// Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
// http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
// Decoding of formula conditions, results, and tokens.
// Support for user-defined named cells added as an array "namedcells"
// Patch code for user-defined named cells supports single cells only.
// NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
// external sheet reference structure
require_once 'PHPExcel.php';
/** PHPExcel_Reader_IReader */
require_once 'PHPExcel/Reader/IReader.php';
/** PHPExcel_Shared_OLERead */
require_once 'PHPExcel/Shared/OLERead.php';
* This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
* @package PHPExcel_Reader
* @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
const XLS_WorkbookGlobals = 0x5;
const XLS_Worksheet = 0x10;
const XLS_Type_BOF = 0x809;
const XLS_Type_EOF = 0x0a;
const XLS_Type_BOUNDSHEET = 0x85;
const XLS_Type_DIMENSION = 0x200;
const XLS_Type_ROW = 0x208;
const XLS_Type_DBCELL = 0xd7;
const XLS_Type_FILEPASS = 0x2f;
const XLS_Type_NOTE = 0x1c;
const XLS_Type_TXO = 0x1b6;
const XLS_Type_RK = 0x7e;
const XLS_Type_RK2 = 0x27e;
const XLS_Type_MULRK = 0xbd;
const XLS_Type_MULBLANK = 0xbe;
const XLS_Type_INDEX = 0x20b;
const XLS_Type_SST = 0xfc;
const XLS_Type_EXTSST = 0xff;
const XLS_Type_CONTINUE = 0x3c;
const XLS_Type_LABEL = 0x204;
const XLS_Type_LABELSST = 0xfd;
const XLS_Type_NUMBER = 0x203;
const XLS_Type_EXTSHEET = 0x17;
const XLS_Type_NAME = 0x18;
const XLS_Type_ARRAY = 0x221;
const XLS_Type_STRING = 0x207;
const XLS_Type_FORMULA = 0x406;
const XLS_Type_FORMULA2 = 0x6;
const XLS_Type_FORMAT = 0x41e;
const XLS_Type_XF = 0xe0;
const XLS_Type_BOOLERR = 0x205;
const XLS_Type_UNKNOWN = 0xffff;
const XLS_Type_NINETEENFOUR = 0x22;
const XLS_Type_MERGEDCELLS = 0xe5;
const XLS_Type_CODEPAGE = 0x42;
const XLS_Type_PROTECT = 0x0012;
const XLS_Type_PASSWORD = 0x0013;
const XLS_Type_COLINFO = 0x007d;
const XLS_Type_FONT = 0x0031;
const XLS_Type_EXTERNALBOOK = 0x01ae;
const XLS_Type_BLANK = 0x0201;
const XLS_Type_SHEETPR = 0x0081;
const XLS_Type_DEFAULTROWHEIGHT = 0x0225;
const XLS_Type_DEFCOLWIDTH = 0x0055;
const XLS_utcOffsetDays = 25569;
const XLS_utcOffsetDays1904 = 24107;
const XLS_SecInADay = 86400;
const XLS_DEF_NUM_FORMAT = "%s";
// $_formatRecords[] will eventually be replaced by $_numberFormat[]
// dvc: added list of names and their sheet associated indexes
// dvc: added for external sheets references
// dvc: fixed known date formats
// dvc: separated percent formats
// dvc: removed exponentials to format as default strings.
* Loads PHPExcel from file
* @param string $pFilename
public function load($pFilename)
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
$excel->removeSheetByIndex(0);
// Use ParseXL for the hard work.
'mb_convert_encoding' : 'iconv';
$res = $this->_ole->read($pFilename);
// oops, something goes wrong (Darko Miljanovic)
if($res === false) { // check error code
if($this->_ole->error == 1) { // bad file
throw new Exception('The filename ' . $pFilename . ' is not readable');
} elseif($this->_ole->error == 2) {
throw new Exception('The filename ' . $pFilename . ' is not recognised as an Excel file');
// check other error codes here (eg bad fileformat, etc...)
$substreamType = ord($this->_data[$pos + 6]) | ord($this->_data[$pos + 7]) << 8;
if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
if ($substreamType != self::XLS_WorkbookGlobals){
while ($code != self::XLS_Type_EOF){
* SST - Shared String Table
* This record contains a list of all strings used anywhere
* in the workbook. Each string occurs only once. The
* workbook uses indexes into the list to reference the
* -- "OpenOffice.org's Documentation of the Microsoft
// offset: 0; size: 4; total number of strings
// offset: 4; size: 4; number of unique strings
$limitpos = $spos + $length;
// loop through the Unicode strings (16-bit length)
for ($i = 0; $i < $uniqueStrings; $i++ ) {
if ($spos == $limitpos) {
// then we have reached end of SST record data
if ($opcode != self::XLS_Type_CONTINUE) {
// broken file, something is wrong
$limitpos = $spos + $conlength;
// Read in the number of characters in the Unicode string
$optionFlags = ord($this->_data[$spos]);
// bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
$asciiEncoding = (($optionFlags & 0x01) == 0) ;
// bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
$extendedString = ( ($optionFlags & 0x04) != 0); // Asian phonetic
// bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
$richString = ( ($optionFlags & 0x08) != 0);
if ($richString) { // Read in the crun
// number of Rich-Text formatting runs
$formattingRuns = ord($this->_data[$spos]) | (ord($this->_data[$spos + 1]) << 8);
// size of Asian phonetic setting
// read in the characters
$len = ($asciiEncoding) ? $numChars : $numChars * 2;
if ($spos + $len < $limitpos) {
// found countinue record
$retstr = substr($this->_data, $spos, $limitpos - $spos);
$bytesRead = $limitpos - $spos;
// remaining characters in Unicode string
$charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
// keep reading the characters
// length of continue record data
if ($opcode != self::XLS_Type_CONTINUE) {
// broken file, something is wrong
$limitpos = $spos + $conlength;
// option flags are repeated when Unicode string is split by a continue record
// OpenOffice.org documentation 5.21
if ($asciiEncoding && ($option == 0)) {
// 1st fragment compressed
// this fragment compressed
$len = min($charsLeft, $limitpos - $spos);
} elseif (!$asciiEncoding && ($option != 0)) {
// 1st fragment uncompressed
// this fragment uncompressed
$len = min($charsLeft * 2, $limitpos - $spos);
} elseif (!$asciiEncoding && ($option == 0)) {
// 1st fragment uncompressed
// this fragment compressed
$len = min($charsLeft, $limitpos - $spos);
for ($j = 0; $j < $len; $j++ ) {
$retstr .= $this->_data[$spos + $j]. chr(0);
// 1st fragment compressed
// this fragment uncompressed
for ($j = 0; $j < strlen($retstr); $j++ ) {
$newstr = $retstr[$j]. chr(0);
$len = min($charsLeft * 2, $limitpos - $spos);
//$retstr = ($asciiEncoding) ?
// $retstr : $this->_encodeUTF16($retstr);
// convert string according codepage and BIFF version
if($version == self::XLS_BIFF8) {
// SST only occurs in BIFF8, so why this part?
// list of formatting runs
for ($j = 0; $j < $formattingRuns; $j++ ) {
// first formatted character; zero-based
$charPos = $this->_getInt2d($this->_data, $spos + $j * 4);
$fontIndex = $this->_getInt2d($this->_data, $spos + 2 + $j * 4);
'fontIndex' => $fontIndex,
$spos += 4 * $formattingRuns;
// For Asian phonetic settings, we skip the extended string data
$spos += $extendedRunLength;
case self::XLS_Type_FILEPASS:
* This record is part of the File Protection Block. It
* contains information about the read/write password of the
* file. All record contents following this record will be
* -- "OpenOffice.org's Documentation of the Microsoft
case self::XLS_Type_EXTERNALBOOK:
case self::XLS_Type_EXTSHEET:
// external sheet references provided for named cells
if ($version == self::XLS_BIFF8) {
for ($x = 0; $x < $xcnt; $x++ ) {
ord($this->_data[$xpos + 5 + 6* $x]) << 8;
// this if statement is going to replace the above one later
if ($version == self::XLS_BIFF8) {
// offset: 0; size: 2; number of following ref structures
for ($i = 0; $i < $nm; $i++ ) {
// offset: 2 + 6 * $i; index to EXTERNALBOOK record
'externalBookIndex' => $this->_getInt2d($recordData, 2 + 6 * $i),
// offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
'firstSheetIndex' => $this->_getInt2d($recordData, 4 + 6 * $i),
// offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
'lastSheetIndex' => $this->_getInt2d($recordData, 6 + 6 * $i),
case self::XLS_Type_NAME:
* This record is part of a Link Table. It contains the name
* and the token array of an internal defined name. Token
* arrays of defined names contain tokens with aberrant
* -- "OpenOffice.org's Documentation of the Microsoft
$fpos = $npos + 14 + 1 + $nlen;
if ($ftoken == 58 && $opts == 0 && $flen == 7) {
case self::XLS_Type_FORMAT:
* This record contains information about a number format.
* All FORMAT records occur together in a sequential list.
* In BIFF2-BIFF4 other records referencing a FORMAT record
* contain a zero-based index into this list. From BIFF5 on
* the FORMAT record contains the index itself that will be
* -- "OpenOffice.org's Documentation of the Microsoft
//$indexCode = ord($this->_data[$pos + 4]) | ord($this->_data[$pos + 5]) << 8;
$indexCode = $this->_GetInt2d($recordData, 0);
if ($version == self::XLS_BIFF8) {
$numchars = ord($this->_data[$pos + 6]);
$formatString = substr($this->_data, $pos + 7, $numchars*2);
// now also stored in array _format[]
// _formatRecords[] will be removed from code later
case self::XLS_Type_FONT:
* This record contains formatting information for cells,
* rows, columns or styles.
* -- "OpenOffice.org's Documentation of the Microsoft
// dvc: reg exp changed to custom date/time format chars
// custom datetime format
// dvc: convert Excel formats to PHP date formats
// first remove escapes related to non-format characters
// first letter of month - no php equivalent
// mm is minutes if time or month w/leading zero
// fractional seconds - no php equivalent
if (! strpos($formatstr,'A')) { // 24-hour format
// user defined flag symbol????
// dvc: new code for custom percent formats
else if (preg_match('/%$/', $formatstr)) { // % number format
// dvc: code for other unknown formats
// dvc: changed to add format to unknown for debug
// dvc: changed to add format to unknown for debug
// store styles in xf array
case self::XLS_Type_NINETEENFOUR:
* This record specifies the base date for displaying date
* values. All dates are stored as count of days past this
* base date. In BIFF2-BIFF4 this record is part of the
* Calculation Settings Block. In BIFF5-BIFF8 it is
* stored in the Workbook Globals Substream.
* -- "OpenOffice.org's Documentation of the Microsoft
$this->_nineteenFour = (ord($this->_data[$pos + 4]) == 1);
if (ord($this->_data[$pos + 4]) == 1) {
PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
case self::XLS_Type_BOUNDSHEET:
* This record is located in the Workbook Globals
* Substream and represents a sheet inside the workbook.
* One SHEET record is written for each sheet. It stores the
* sheet name and a stream offset to the BOF record of the
* respective Sheet Substream within the Workbook Stream.
* -- "OpenOffice.org's Documentation of the Microsoft
$rec_typeFlag = ord($this->_data[$pos + 8]);
$rec_visibilityFlag = ord($this->_data[$pos + 9]);
$rec_length = ord($this->_data[$pos + 10]);
if ($version == self::XLS_BIFF8) {
$compressedUTF16 = ((ord($this->_data[$pos + 11]) & 0x01) == 0);
$rec_length = ($compressedUTF16) ? $rec_length : $rec_length* 2;
} elseif ($version == self::XLS_BIFF7) {
$rec_name = substr($this->_data, $pos + 11, $rec_length);
case self::XLS_Type_CODEPAGE:
* This record stores the text encoding used to write byte
* strings, stored as MS Windows code page identifier.
* -- "OpenOffice.org's Documentation of the Microsoft
// currently not supported by libiconv
case 852: //OEM Latin II (Central European)
case 858: //OEM Multilingual Latin I with Euro
case 860: //OEM Portugese
case 861: //OEM Icelandic
case 863: //OEM Canadian (French)
case 866: //OEM Cyrillic (Russian)
case 869: //OEM Greek (Modern)
case 932: //ANSI Japanese Shift-JIS
case 936: //ANSI Chinese Simplified GBK
case 949: //ANSI Korean (Wansung)
case 950: //ANSI Chinese Traditional BIG5
case 1200: //UTF-16 (BIFF8)
case 1250: // ANSI Latin II (Central European)
case 1251: //ANSI Cyrillic
case 1252: //ANSI Latin I (BIFF4-BIFF7)
case 1254: //ANSI Turkish
case 1258: //ANSI Vietnamese
case 1361: //ANSI Korean (Johab)
case 10000: //Apple Roman
// currently not supported by libiconv
case 32768: //Apple Roman
// currently not supported by libiconv
case 32769: //ANSI Latin I (BIFF2-BIFF3)
// currently not supported by libiconv
* PARSE THE INDIVIDUAL SHEETS
// add sheet to PHPExcel object
$sheet = $excel->createSheet();
$sheet->setTitle((string) $val['name']);
$substreamType = ord($this->_data[$spos + 6]) | ord($this->_data[$spos + 7]) << 8;
if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
if ($substreamType != self::XLS_Worksheet) {
if ($lowcode == self::XLS_Type_EOF) {
$code = $lowcode | ord($this->_data[$spos + 1]) << 8;
$recordData = substr($this->_data, $spos + 4, $length);
unset ($this->_formula_result);
$this->_multiplier = 1; // need for format with %
case self::XLS_Type_DIMENSION:
* This record contains the range address of the used area
* -- "OpenOffice.org's Documentation of the Microsoft
if (!isset ($this->_numRows)) {
if (($length == 10) || ($version == self::XLS_BIFF7)){
case self::XLS_Type_MERGEDCELLS:
* This record contains the addresses of merged cell ranges
* -- "OpenOffice.org's Documentation of the Microsoft
for ($i = 0; $i < $cellRanges; $i++ ) {
$fr = $this->_GetInt2d($this->_data, $spos + 8 * $i + 2); // first row
$fc = $this->_GetInt2d($this->_data, $spos + 8 * $i + 6); // first column
$lc = $this->_GetInt2d($this->_data, $spos + 8 * $i + 8); // last column
// this part no longer needed, instead apply cell merge on PHPExcel worksheet object
$this->_sheets[$this->_sn]['cellsInfo'][$fr + 1][$fc + 1]['rowspan'] = $lr - $fr + 1;
$this->_sheets[$this->_sn]['cellsInfo'][$fr + 1][$fc + 1]['colspan'] = $lc - $fc + 1;
$sheet->mergeCellsByColumnAndRow($fc, $fr + 1, $lc, $lr + 1);
* This record represents a cell that contains an RK value
* (encoded integer or floating-point value). If a
* floating-point value cannot be encoded to an RK value,
* a NUMBER record will be written. This record replaces the
* record INTEGER written in BIFF2.
* -- "OpenOffice.org's Documentation of the Microsoft
if ($this->_isDate($spos)) {
list($string, $raw) = $this->_createDate($numValue);
if (isset($this->_columnsFormat[$column + 1])){
$this->_curformat = $this->_columnsFormat[$column + 1];
$string = sprintf($this->_curformat,$numValue*$this->_multiplier);
// offset 4; size: 2; index to XF record
$xfindex = $this->_getInt2d($recordData, 4);
// add BIFF8 style information
$sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
//$this->_addcell($row, $column, $string, $raw);
//$sheet->setCellValueByColumnAndRow($column, $row + 1, $string);
$sheet->setCellValueByColumnAndRow($column, $row + 1, $numValue);
case self::XLS_Type_LABELSST:
* This record represents a cell that contains a string. It
* replaces the LABEL record and RSTRING record used in
* -- "OpenOffice.org's Documentation of the Microsoft
//$this->_addcell($row, $column, $this->_sst[$index]);
if ($fmtRuns = $this->_sst[$index]['fmtRuns']) {
// then we have rich text
for ($i = 0; $i <= count($this->_sst[$index]['fmtRuns']); $i++ ) {
if (isset ($fmtRuns[$i])) {
$text = mb_substr($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos, 'UTF-8');
$charPos = $fmtRuns[$i]['charPos'];
$textRun = $richText->createTextRun($text);
if (isset ($fmtRuns[$i - 1])) {
if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
$fontIndex = $fmtRuns[$i - 1]['fontIndex'];
// this has to do with that index 4 is omitted in all BIFF versions for some strange reason
// check the OpenOffice documentation of the FONT record
$fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
$textRun->getFont()->applyFromArray($this->_font[$fontIndex]);
$sheet->setCellValueByColumnAndRow($column, $row + 1, $this->_sst[$index]['value']);
// add BIFF8 style information
$sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
case self::XLS_Type_MULRK:
* This record represents a cell range containing RK value
* cells. All cells are located in the same row.
* -- "OpenOffice.org's Documentation of the Microsoft
$colLast = ord($this->_data[$spos + $length - 2]) | ord($this->_data[$spos + $length - 1]) << 8;
$columns = $colLast - $colFirst + 1;
for ($i = 0; $i < $columns; $i++ ) {
// offset: 0; size: 2; index to XF record
$xfindex = $this->_getInt2d($recordData, 4 + 6 * $i);
// offset: 2; size: 4; RK value
if ($this->_isDate($tmppos-4)) {
list($string, $raw) = $this->_createDate($numValue);
if (isset($this->_columnsFormat[$colFirst + $i + 1])){
$this->_curformat = $this->_columnsFormat[$colFirst+ $i + 1];
$string = sprintf($this->_curformat, $numValue *
//$this->_addcell($row, $colFirst + $i, $string, $raw);
$sheet->getStyleByColumnAndRow($colFirst + $i, $row + 1)->applyFromArray($this->_xf[$xfindex]);
//$sheet->setCellValueByColumnAndRow($colFirst + $i, $row + 1, $string);
$sheet->setCellValueByColumnAndRow($colFirst + $i, $row + 1, $numValue);
case self::XLS_Type_NUMBER:
* This record represents a cell that contains a
* -- "OpenOffice.org's Documentation of the Microsoft
// offset 4; size: 2; index to XF record
if ($this->_isDate($spos)) {
$numValue = $this->_createNumber($spos);
list($string, $raw) = $this->_createDate($numValue);
if (isset($this->_columnsFormat[$column + 1])) {
$this->_curformat = $this->_columnsFormat[$column + 1];
$raw = $this->_createNumber($spos);
$string = sprintf($this->_curformat, $raw * $this->_multiplier);
// add BIFF8 style information
$sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
//$this->_addcell($row, $column, $string, $raw);
//$sheet->setCellValueByColumnAndRow($column, $row + 1, $string);
$sheet->setCellValueByColumnAndRow($column, $row + 1, $numValue);
case self::XLS_Type_FORMULA:
case self::XLS_Type_FORMULA2:
* This record contains the token array and the result of a
* -- "OpenOffice.org's Documentation of the Microsoft
// offset: 0; size: 2; row index
// offset: 2; size: 2; col index
// offset: 4; size: 2; XF index
// offset: 6; size: 8; result of the formula
if ((ord($this->_data[$spos + 6]) == 0) &&
(ord($this->_data[$spos + 12]) == 255) &&
(ord($this->_data[$spos + 13]) == 255)) {
//String formula. Result follows in appended STRING record
$this->_formula_result = 'string';
// only reads byte strings...
if ($scode == self::XLS_Type_STRING && $sopt == '0') {
} elseif ((ord($this->_data[$spos + 6]) == 1) &&
(ord($this->_data[$spos + 12]) == 255) &&
(ord($this->_data[$spos + 13]) == 255)) {
//Boolean formula. Result is in +2; 0=false,1=true
$this->_formula_result = 'boolean';
} elseif ((ord($this->_data[$spos + 6]) == 2) &&
(ord($this->_data[$spos + 12]) == 255) &&
(ord($this->_data[$spos + 13]) == 255)) {
//Error formula. Error code is in +2
$this->_formula_result = 'error';
} elseif ((ord($this->_data[$spos + 6]) == 3) &&
(ord($this->_data[$spos + 12]) == 255) &&
(ord($this->_data[$spos + 13]) == 255)) {
//Formula result is a null string
$this->_formula_result = 'null';
// forumla result is a number, first 14 bytes like _NUMBER record
$this->_formula_result = 'number';
if ($this->_isDate($spos)) {
$numValue = $this->_createNumber($spos);
list($string, $raw) = $this->_createDate($numValue);
if (isset($this->_columnsFormat[$column + 1])){
$this->_curformat = $this->_columnsFormat[$column + 1];
$raw = $this->_createNumber($spos);
$string = sprintf($this->_curformat, $raw * $this->_multiplier);
// save the raw formula tokens for end user interpretation
// Excel stores as a token record
$this->_rectype = 'formula';
// read formula record tokens ...
$tokenlength = ord($this->_data[$spos + 20]) | ord($this->_data[$spos + 21]) << 8;
for ($i = 0; $i < $tokenlength; $i++ ) {
$this->_formula[$i] = ord($this->_data[$spos + 22 + $i]);
// add BIFF8 style information
$sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
//$this->_addcell($row, $column, $string, $raw);
$sheet->setCellValueByColumnAndRow($column, $row + 1, $string);
// offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
// offset: 16: size: 4; not used
// offset: 20: size: variable; formula structure
// WORK IN PROGRESS: TRUE FORMULA SUPPORT
// resolve BIFF8 formula tokens into human readable formula
// so it can be added as formula
// $formulaStructure = substr($recordData, 20);
// $formulaString = $this->_getFormulaStringFromStructure($formulaStructure); // get human language
case self::XLS_Type_BOOLERR:
* This record represents a Boolean value or error value
* -- "OpenOffice.org's Documentation of the Microsoft
// offset: 0; size: 2; row index
// offset: 2; size: 2; column index
// offset: 4; size: 2; index to XF record
// offset: 6; size: 1; the boolean value or error value
$value = ord($recordData[6]);
// offset: 7; size: 1; 0=boolean; 1=error
$isError = ord($recordData[7]);
$sheet->getCellByColumnAndRow($column, $row + 1)->setValueExplicit((bool) $value, PHPExcel_Cell_DataType::TYPE_BOOL);
// add BIFF8 style information
$sheet->getStyleByColumnAndRow($column, $row + 1)->applyFromArray($this->_xf[$xfindex]);
* This record contains the properties of a single row in a
* sheet. Rows and cells in a sheet are divided into blocks
* -- "OpenOffice.org's Documentation of the Microsoft
// offset: 0; size: 2; index of this row
// offset: 2; size: 2; index to column of the first cell which is described by a cell record
// offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
// bit: 14-0; mask: 0x7FF; height of the row, in twips = 1/20 of a point
$height = (0x7FF & $this->_GetInt2d($recordData, 6)) >> 0;
// bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
$useDefaultHeight = (0x8000 & $this->_GetInt2d($recordData, 6)) >> 15;
if (!$useDefaultHeight) {
$sheet->getRowDimension($r + 1)->setRowHeight($height / 20);
// offset: 8; size: 2; not used
// offset: 10; size: 2; not used in BIFF5-BIFF8
// offset: 12; size: 4; option flags and default row formatting
// bit: 2-0: mask: 0x00000007; outline level of the row
$level = (0x00000007 & $this->_GetInt4d($recordData, 12)) >> 0;
$sheet->getRowDimension($r + 1)->setOutlineLevel($level);
// bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
$isCollapsed = (0x00000010 & $this->_GetInt4d($recordData, 12)) >> 4;
$sheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
// bit: 5; mask: 0x00000020; 1 = row is hidden
$isHidden = (0x00000020 & $this->_GetInt4d($recordData, 12)) >> 5;
$sheet->getRowDimension($r + 1)->setVisible(!$isHidden);
case self::XLS_Type_DBCELL:
* This record is written once in a Row Block. It contains
* relative offsets to calculate the stream position of the
* first cell record for each row. The offset list in this
* record contains as many offsets as ROW records are
* present in the Row Block.
* -- "OpenOffice.org's Documentation of the Microsoft
case self::XLS_Type_MULBLANK:
* MULBLANK - Multiple BLANK
* This record represents a cell range of empty cells. All
* cells are located in the same row
* -- "OpenOffice.org's Documentation of the Microsoft
// offset: 0; size: 2; index to row
// offset: 2; size: 2; index to first column
// offset: 4; size: 2 x nc; list of indexes to XF records
// add BIFF8 style information
for ($i = 0; $i < $length / 2 - 4; $i++ ) {
$xfindex = $this->_GetInt2d($recordData, 4 + 2 * $i);
$sheet->getStyleByColumnAndRow($fc + $i, $row + 1)->applyFromArray($this->_xf[$xfindex]);
// offset: 6; size 2; index to last column (not needed)
case self::XLS_Type_LABEL:
* This record represents a cell that contains a string. In
* BIFF8 it is usually replaced by the LABELSST record.
* Excel still uses this record, if it copies unformatted
* text cells to the clipboard.
* -- "OpenOffice.org's Documentation of the Microsoft
$this->_addcell($row, $column, substr($this->_data, $spos + 8,
ord($this->_data[$spos + 6]) | ord($this->_data[$spos + 7]) << 8));
$sheet->setCellValueByColumnAndRow($column, $row + 1, substr($this->_data, $spos + 8,
case self::XLS_Type_PROTECT:
* PROTECT - Sheet protection (BIFF2 through BIFF8)
* if this record is omitted, then it also means no sheet protection
// bit 0, mask 0x01; sheet protection
$isSheetProtected = (0x01 & $this->_GetInt2d($recordData, 0)) >> 0;
switch ($isSheetProtected) {
case 1: $sheet->getProtection()->setSheet(true); break;
case self::XLS_Type_PASSWORD:
* PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
// offset: 0; size: 2; 16-bit hash value of password
$sheet->getProtection()->setPassword($password, true);
case self::XLS_Type_COLINFO:
* COLINFO - Column information
// offset: 0; size: 2; index to first column in range
$fc = $this->_GetInt2d($recordData, 0); // first column index
// offset: 2; size: 2; index to last column in range
$lc = $this->_GetInt2d($recordData, 2); // first column index
// offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
// offset: 6; size: 2; index to XF record for default column formatting
// offset: 8; size: 2; option flags
// bit: 0; mask: 0x0001; 1= columns are hidden
$isHidden = (0x0001 & $this->_GetInt2d($recordData, 8)) >> 0;
// bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
$level = (0x0700 & $this->_GetInt2d($recordData, 8)) >> 8;
// bit: 12; mask: 0x1000; 1 = collapsed
$isCollapsed = (0x1000 & $this->_GetInt2d($recordData, 8)) >> 12;
// offset: 10; size: 2; not used
for ($i = $fc; $i <= $lc; $i++ ) {
$sheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
$sheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
$sheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
$sheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
case self::XLS_Type_DEFCOLWIDTH:
// offset: 0; size: 2; row index
$sheet->getDefaultColumnDimension()->setWidth($width);
case self::XLS_Type_DEFAULTROWHEIGHT:
// offset: 0; size: 2; option flags
// offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
$sheet->getDefaultRowDimension()->setRowHeight($height / 20);
case self::XLS_Type_BLANK:
// offset: 0; size: 2; row index
// offset: 2; size: 2; col index
// offset: 4; size: 2; XF index
// add BIFF8 style information
$sheet->getStyleByColumnAndRow($col, $row + 1)->applyFromArray($this->_xf[$xfindex]);
case self::XLS_Type_SHEETPR:
// bit: 6; mask: 0x0040; 0 = outline buttons above outline group
$isSummaryBelow = (0x0040 & $this->_GetInt2d($recordData, 0)) >> 6;
$sheet->setShowSummaryBelow($isSummaryBelow);
// bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
$isSummaryRight = (0x0080 & $this->_GetInt2d($recordData, 0)) >> 7;
$sheet->setShowSummaryRight($isSummaryRight);
if (!isset ($this->_sheets[$this->_sn]['numRows'])){
$this->_sheets[$this->_sn]['numRows'] = $this->_sheets[$this->_sn]['maxrow'];
if (!isset ($this->_sheets[$this->_sn]['numCols'])){
$this->_sheets[$this->_sn]['numCols'] = $this->_sheets[$this->_sn]['maxcol'];
foreach($this->_boundsheets as $index => $details) {
$sheet = $excel->getSheet($index);
// read all the columns of all the rows !
$numrows = $this->_sheets[$index]['numRows'];
$numcols = $this->_sheets[$index]['numCols'];
for ($row = 0; $row < $numrows; $row++) {
for ($col = 0; $col < $numcols; $col++) {
$cellcontent = $cellinfo = null;
if (isset($this->_sheets[$index]['cells'][$row][$col])===true) {
$cellcontent = $this->_sheets[$index]['cells'][$row][$col];
if (isset($this->_sheets[$index]['cellsInfo'][$row][$col])===true) {
$cellinfo = $this->_sheets[$index]['cellsInfo'][$row][$col];
$sheet->setCellValueByColumnAndRow($col, $row + 1,
// set $encoder for method of UTF-16LE encoding
$this->_encoderFunction = '';
if ($encoder == 'iconv') {
$this->_encoderFunction = function_exists('iconv') ? 'iconv' : 'mb_convert_encoding';
} elseif ($encoder == 'mb') {
$this->_curformat = $this->_formatRecords['xfrecords'][$xfindex]['format'];
$this->_fmtcode = $this->_formatRecords['xfrecords'][$xfindex]['code'];
$this->_rectype = 'date';
} else if (($xfindex == 0x9) || ($xfindex == 0xa) || ($this->_formatRecords['xfrecords'][$xfindex]['type'] == 'percent')) {
$this->_rectype = 'number';
$this->_multiplier = 100;
else if ($this->_formatRecords['xfrecords'][$xfindex]['type'] == 'number') {
$this->_rectype = 'number';
$this->_rectype = 'unknown';
$utcDays = $numValue - ($this->_nineteenFour ? self::XLS_utcOffsetDays1904 : self::XLS_utcOffsetDays);
$utcValue = round(($utcDays * self::XLS_SecInADay));
// dvc: excel returns local date/time as absolutes,
// i.e. 1 hr = 0.04166, 1 day = 1,
// so need to treat as GMT to translate
$string = gmdate ($this->_curformat, $utcValue);
// assume a time format...
$hours = round($numValue * 24);
$mins = round($numValue * 24* 60) - $hours * 60;
$secs = round($numValue * self::XLS_SecInADay) - $hours * 60* 60 - $mins * 60;
$string = date ($this->_curformat, mktime($hours, $mins, $secs));
return array($string, $raw);
* Reads 8 bytes and returns IEEE 754 float
$sign = ($rknumhigh & 0x80000000) >> 31;
$exp = ($rknumhigh & 0x7ff00000) >> 20;
$mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
$mantissalow1 = ($rknumlow & 0x80000000) >> 31;
$mantissalow2 = ($rknumlow & 0x7fffffff);
$value = $mantissa / pow( 2 , (20- ($exp - 1023)));
if ($mantissalow1 != 0) {
$value += 1 / pow (2 , (21 - ($exp - 1023)));
$value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
* Same as _createNumber, but not hardcoded to read from $this->_data
$sign = ($rknumhigh & 0x80000000) >> 31;
$exp = ($rknumhigh & 0x7ff00000) >> 20;
$mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
$mantissalow1 = ($rknumlow & 0x80000000) >> 31;
$mantissalow2 = ($rknumlow & 0x7fffffff);
$value = $mantissa / pow( 2 , (20- ($exp - 1023)));
if ($mantissalow1 != 0) {
$value += 1 / pow (2 , (21 - ($exp - 1023)));
$value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
private function _addcell($row, $col, $string, $raw = '')
$this->_sheets[$this->_sn]['maxrow'] =
max($this->_sheets[$this->_sn]['maxrow'], $row + $this->_rowoffset);
$this->_sheets[$this->_sn]['maxcol'] =
max($this->_sheets[$this->_sn]['maxcol'], $col + $this->_coloffset);
$this->_sheets[$this->_sn]['cells'][$row +
$this->_rowoffset][$col + $this->_coloffset] = $string;
$this->_sheets[$this->_sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw;
if (isset($this->_rectype)) {
$this->_sheets[$this->_sn]['cellsInfo'][$row +
$this->_rowoffset][$col + $this->_coloffset]['type'] =
if (isset($this->_curformat)) {
$this->_sheets[$this->_sn]['cellsInfo'][$row +
$this->_rowoffset][$col + $this->_coloffset]['format'] =
if (isset($this->_fmtcode)) {
$this->_sheets[$this->_sn]['cellsInfo'][$row +
$this->_rowoffset][$col + $this->_coloffset]['code'] =
if (isset($this->_formula)) {
$this->_sheets[$this->_sn]['cellsInfo'][$row +
$this->_rowoffset][$col + $this->_coloffset]['formula_tokens'] =
if (isset($this->_formula_result)) {
$this->_sheets[$this->_sn]['cellsInfo'][$row +
$this->_rowoffset][$col + $this->_coloffset]['formula_result'] =
if (($rknum & 0x02) != 0) {
// changes by mmp, info on IEEE754 encoding from
// research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
// The RK format calls for using only the most significant 30 bits
// of the 64 bit floating point value. The other 34 bits are assumed
// to be 0 so we use the upper 30 bits of $rknum as follows...
$sign = ($rknum & 0x80000000) >> 31;
$exp = ($rknum & 0x7ff00000) >> 20;
$mantissa = (0x100000 | ($rknum & 0x000ffffc));
$value = $mantissa / pow( 2 , (20- ($exp - 1023)));
if (($rknum & 0x01) != 0) {
// returns UTF-8 string from UTF-16 string either compressed or uncompressed
//if ($this->_defaultEncoding) {
// $string = $this->_uncompressByteString($string);
// switch ($this->_encoderFunction){
// $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
// case 'mb_convert_encoding' :
// $result = mb_convert_encoding($string, $this->_defaultEncoding,
switch ($this->_encoderFunction){
$result = iconv('UTF-16LE', 'UTF-8', $string);
case 'mb_convert_encoding' :
return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) |
(ord($data[$pos + 2]) << 16) | (ord($data[$pos + 3]) << 24);
$uncompressedString = "";
for($i = 0; $i < strlen($string); $i++ ) {
$uncompressedString .= $string[$i]. "\0";
return $uncompressedString;
switch ($this->_encoderFunction) {
case 'mb_convert_encoding' :
* Get numberFormat from index
* returns false if we couldn't resolve numberFormat
// there are two possibilites
// A. numberFormatIndex refers to a built-in numberformat and will not be found in the numberFormat array
// instead, it is omitted, and we have to determine numberFormat based on table look-up
// B. numberFormatIndex refers to a user-defined numberformat and will be found in the numberFormat array
switch ($numberFormatIndex) {
case 3: $code = '#,##0'; break;
// case 5 is omitted since it is always written by Excel, depend on locale;
// case 6 is omitted since it is always written by Excel, depend on locale;
// case 7 is omitted since it is always written by Excel, depend on locale;
// case 8 is omitted since it is always written by Excel, depend on locale;
case 11: $code = '0.00E+00'; break;
case 12: $code = '# ?/?'; break;
case 13: $code = '# ??/??'; break;
case 14: $code = 'M/D/YY'; break; // not always correct, but the best we can do: taken from Microsoft Windws regional settings
case 15: $code = 'D-MMM-YY'; break;
case 16: $code = 'D-MMM'; break;
case 17: $code = 'MMM-YY'; break;
case 18: $code = 'h:mm AM/PM'; break;
case 19: $code = 'h:mm:ss AM/PM'; break;
case 20: $code = 'h:mm'; break;
case 21: $code = 'h:mm:ss'; break;
case 22: $code = 'M/D/YY h:mm'; break; // not always correct, but the best we can do: taken from Microsoft Windws regional settings
case 37: $code = '_(#,##0_);(#,##0)'; break;
case 38: $code = '_(#,##0_);[Red(#,##0)]'; break;
case 39: $code = '_(#,##0.00_);(#,##0.00)'; break;
// case 41 is omitted since it is always written by Excel, depend on locale;
// case 42 is omitted since it is always written by Excel, depend on locale;
// case 43 is omitted since it is always written by Excel, depend on locale;
// case 44 is omitted since it is always written by Excel, depend on locale;
case 45: $code = 'mm:ss'; break;
case 46: $code = '[h]:mm:ss'; break;
case 47: $code = 'mm:ss.0'; break;
case 48: $code = '##0.0E+0'; break;
case 49: $code = '@'; break;
// check for user-defined number format
return array('code' => $code);
* Extracts style from XF record data (not everything is extracted at the moment)
// offset: 0; size: 2; Index to FONT record
$fontIndex = $this->_GetInt2d($recordData, 0);
// this has to do with that index 4 is omitted in all BIFF versions for some strange reason
// check the OpenOffice documentation of the FONT record
$fontIndex = $this->_GetInt2d($recordData, 0) - 1;
$style['font'] = $this->_font[$fontIndex];
// offset: 2; size: 2; Index to FORMAT record
$numberFormatIndex = $this->_GetInt2d($recordData, 2);
$style['protection'] = array();
// offset: 4; size: 2; XF type, cell protection, and parent style XF
// bit 2-0; mask 0x0007; XF_TYPE_PROT
$xfTypeProt = $this->_GetInt2d($recordData, 4);
// bit 0; mask 0x01; 1 = cell is locked
$isLocked = (0x01 & $xfTypeProt) >> 0;
$style['protection']['locked'] = $isLocked ? true : false;
// offset: 6; size: 1; Alignment and text break
$style['alignment'] = array();
// bit 2-0, mask 0x07; horizontal alignment
$horAlign = (0x07 & ord($recordData[6])) >> 0;
// bit 3, mask 0x08; wrap text
$wrapText = (0x08 & ord($recordData[6])) >> 3;
case 0: $style['alignment']['wrap'] = false; break;
case 1: $style['alignment']['wrap'] = true; break;
// bit 6-4, mask 0x70; vertical alignment
$vertAlign = (0x70 & ord($recordData[6])) >> 4;
// offset: 7; size: 1; XF_ROTATION: Text rotation angle
// offset: 8; size: 1; Indentation, shrink to cell size, and text direction
// offset: 9; size: 1; Flags used for attribute groups
$style['borders'] = array(
// offset: 10; size: 4; Cell border lines and background area
// bit: 3-0; mask: 0x0000000F; left style
$style['borders']['left']['style'] = $bordersLeftStyle;
// bit: 7-4; mask: 0x000000F0; right style
$style['borders']['right']['style'] = $bordersRightStyle;
// bit: 11-8; mask: 0x00000F00; top style
$style['borders']['top']['style'] = $bordersTopStyle;
// bit: 15-12; mask: 0x0000F000; bottom style
$style['borders']['bottom']['style'] = $bordersBottomStyle;
// bit: 22-16; mask: 0x007F0000; left color
if ($bordersLeftColor = $this->_readColor((0x007F0000 & $this->_GetInt4d($recordData, 10)) >> 16)) {
$style['borders']['left']['color'] = $bordersLeftColor;
// bit: 29-23; mask: 0x3F800000; right color
if ($bordersRightColor = $this->_readColor((0x3F800000 & $this->_GetInt4d($recordData, 10)) >> 23)) {
$style['borders']['right']['color'] = $bordersRightColor;
$style['fill'] = array();
// bit: 6-0; mask: 0x0000007F; top color
if ($bordersTopColor = $this->_readColor((0x0000007F & $this->_GetInt4d($recordData, 14)) >> 0)) {
$style['borders']['top']['color'] = $bordersTopColor;
// bit: 13-7; mask: 0x00003F80; top color
if ($bordersBottomColor = $this->_readColor((0x00003F80 & $this->_GetInt4d($recordData, 14)) >> 7)) {
$style['borders']['bottom']['color'] = $bordersBottomColor;
// bit: 31-26; mask: 0xFC000000 fill pattern
$style['fill']['type'] = $fillType;
// offset: 18; size: 2; pattern and background colour
// bit: 6-0; mask: 0x007F; color index for pattern color
$style['fill']['startcolor'] = $rgb;
// bit: 13-7; mask: 0x3F80; color index for pattern background
$style['fill']['endcolor'] = $rgb;
* OpenOffice documentation: 2.5.11
* Get fill pattern from index
* OpenOffice documentation: 2.5.12
* Extracts font details from FONT record data (not everything is extracted yet)
// offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
$font['size'] = $size / 20;
// offset: 2; size: 2; option flags
// bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
// bit: 1; mask 0x0002; italic
$isItalic = (0x0002 & $this->_GetInt2d($recordData, 2)) >> 1;
if ($isItalic) $font['italic'] = true;
// bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
// bit: 3; mask 0x0008; strike
$isStrike = (0x0008 & $this->_GetInt2d($recordData, 2)) >> 3;
if ($isStrike) $font['strike'] = true;
// offset: 4; size: 2; colour index
// offset: 6; size: 2; font weight
case 0x02BC: $font['bold'] = true;
// offset: 8; size: 2; escapement type
// offset: 10; size: 1; underline type
$underlineType = ord($recordData[10]);
switch ($underlineType) {
case 0x00: break; // no underline
// offset: 11; size: 1; font family
// offset: 12; size: 1; character set
// offset: 13; size: 1; not used
// offset: 14; size: var; font name
$font['name'] = $unicode['string'];
* Extracts an Excel Unicode short string (8-bit string length)
* OpenOffice documentation: 2.5.3
* this function is under construction, needs to support rich text, and Asian phonetic settings
* function will automatically find out where the Unicode string ends.
// offset: 0: size: 1; length of the string (character count)
$characterCount = ord($subData[0]);
// offset: 1: size: 1; option flags
// bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
$isCompressed = !((0x01 & ord($subData[1])) >> 0);
// bit: 2; mask: 0x04; Asian phonetic settings
$hasAsian = (0x04) & ord($subData[1]) >> 2;
// bit: 3; mask: 0x08; Rich-Text settings
$hasRichText = (0x08) & ord($subData[1]) >> 3;
// offset: 2: size: var; character array
// this offset assumes richtext and Asian phonetic settings are off which is generally wrong
$string = $this->_encodeUTF16(substr($subData, 2, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
'size' => $isCompressed ? 2 + $characterCount : 2 + 2 * $characterCount, // the size in bytes including the length byte + option flags
* Extracts an Excel Unicode long string (16-bit string length)
* OpenOffice documentation: 2.5.3
* this function is under construction, needs to support rich text, and Asian phonetic settings
* this function should look like _readUnicodeStringShort and also return the size in bytes
// offset: 0: size: 2; length of the string (character count)
$characterCount = $this->_GetInt2d($subData, 0);
// offset: 2: size: 1; option flags
// bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
$isCompressed = !((0x01 & ord($subData[2])) >> 0);
// bit: 2; mask: 0x04; Asian phonetic settings
$hasAsian = (0x04) & ord($subData[2]) >> 2;
// bit: 3; mask: 0x08; Rich-Text settings
$hasRichText = (0x08) & ord($subData[2]) >> 3;
// offset: 3: size: var; character array
// this offset assumes richtext and Asian phonetic settings are off which is generally wrong
* Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
// offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
$row = $this->_getInt2d($cellAddressStructure, 0) + 1;
// offset: 2; size: 2; index to column or column offset + relative flags
// bit: 7-0; mask 0x00FF; column index
// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
if (!(0x4000 & $this->_getInt2d($cellAddressStructure, 2))) {
// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
if (!(0x8000 & $this->_getInt2d($cellAddressStructure, 2))) {
* Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
// offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
$fr = $this->_getInt2d($subData, 0) + 1;
// offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
$lr = $this->_getInt2d($subData, 2) + 1;
// offset: 4; size: 2; index to first column or column offset + relative flags
// bit: 7-0; mask 0x00FF; column index
// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
if (!(0x4000 & $this->_getInt2d($subData, 4))) {
// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
if (!(0x8000 & $this->_getInt2d($subData, 4))) {
// offset: 6; size: 2; index to last column or column offset + relative flags
// bit: 7-0; mask 0x00FF; column index
// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
if (!(0x4000 & $this->_getInt2d($subData, 6))) {
// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
if (!(0x8000 & $this->_getInt2d($subData, 6))) {
* Extracts color array from BIFF8 built-in color index
case 0x08: return array('rgb' => '000000');
case 0x09: return array('rgb' => 'FFFFFF');
case 0x0A: return array('rgb' => 'FF0000');
case 0x0B: return array('rgb' => '00FF00');
case 0x0C: return array('rgb' => '0000FF');
case 0x0D: return array('rgb' => 'FFFF00');
case 0x0E: return array('rgb' => 'FF00FF');
case 0x0F: return array('rgb' => '00FFFF');
case 0x10: return array('rgb' => '800000');
case 0x11: return array('rgb' => '008000');
case 0x12: return array('rgb' => '000080');
case 0x13: return array('rgb' => '808000');
case 0x14: return array('rgb' => '800080');
case 0x15: return array('rgb' => '008080');
case 0x16: return array('rgb' => 'C0C0C0');
case 0x17: return array('rgb' => '808080');
case 0x18: return array('rgb' => '9999FF');
case 0x19: return array('rgb' => '993366');
case 0x1A: return array('rgb' => 'FFFFCC');
case 0x1B: return array('rgb' => 'CCFFFF');
case 0x1C: return array('rgb' => '660066');
case 0x1D: return array('rgb' => 'FF8080');
case 0x1E: return array('rgb' => '0066CC');
case 0x1F: return array('rgb' => 'CCCCFF');
case 0x20: return array('rgb' => '000080');
case 0x21: return array('rgb' => 'FF00FF');
case 0x22: return array('rgb' => 'FFFF00');
case 0x23: return array('rgb' => '00FFFF');
case 0x24: return array('rgb' => '800080');
case 0x25: return array('rgb' => '800000');
case 0x26: return array('rgb' => '008080');
case 0x27: return array('rgb' => '0000FF');
case 0x28: return array('rgb' => '00CCFF');
case 0x29: return array('rgb' => 'CCFFFF');
case 0x2A: return array('rgb' => 'CCFFCC');
case 0x2B: return array('rgb' => 'FFFF99');
case 0x2C: return array('rgb' => '99CCFF');
case 0x2D: return array('rgb' => 'FF99CC');
case 0x2E: return array('rgb' => 'CC99FF');
case 0x2F: return array('rgb' => 'FFCC99');
case 0x30: return array('rgb' => '3366FF');
case 0x31: return array('rgb' => '33CCCC');
case 0x32: return array('rgb' => '99CC00');
case 0x33: return array('rgb' => 'FFCC00');
case 0x34: return array('rgb' => 'FF9900');
case 0x35: return array('rgb' => 'FF6600');
case 0x36: return array('rgb' => '666699');
case 0x37: return array('rgb' => '969696');
case 0x38: return array('rgb' => '003366');
case 0x39: return array('rgb' => '339966');
case 0x3A: return array('rgb' => '003300');
case 0x3B: return array('rgb' => '333300');
case 0x3C: return array('rgb' => '993300');
case 0x3D: return array('rgb' => '993366');
case 0x3E: return array('rgb' => '333399');
case 0x3F: return array('rgb' => '333333');
* Get a sheet range like Sheet1:Sheet3 from REF index
* Note: If there is only one sheet in the range, one gets e.g Sheet1
// we are assuming that ref index refers to internal workbook
// in general, this is wrong, fix later
if (isset ($this->_ref[$index])) {
$firstSheetName = $this->_boundsheets[$this->_ref[$index]['firstSheetIndex']]['name'];
$firstSheetName = (strpos($firstSheetName, ' ') !== false) ?
"'$firstSheetName'" : $firstSheetName;
$lastSheetName = $this->_boundsheets[$this->_ref[$index]['lastSheetIndex']]['name'];
$lastSheetName = (strpos($lastSheetName, ' ') !== false) ?
"'$lastSheetName'" : $lastSheetName;
if ($firstSheetName == $lastSheetName) {
return "$firstSheetName:$lastSheetName";
* Convert formula structure (HEX data) into human readable Excel formula
* Experimental function under construction not currently in use
// offset: 0; size: 2; size of the following formula data
$sz = $this->_getInt2d($formulaStructure, 0);
echo 'size: ' . $sz . "\n";
$formulaData = substr($formulaStructure, 2, $sz);
// offset: 2 + sz; size: variable (optional)
if (strlen($formulaStructure) > 2 + $sz) {
$additionalData = substr($formulaStructure, 2 + $sz);
// dump the entire formula structure
echo 'the entire formulastructure: ';
echo '<span style="background:yellow">';
for ($i = 0; $i < 2; $i++ ) {
echo sprintf('%02X', ord($formulaStructure[$i])) . ' ';
echo '<span style="background:cyan">';
for ($i = 2; $i < 2 + $sz; $i++ ) {
echo sprintf('%02X', ord($formulaStructure[$i])) . ' ';
// start parsing the $formulaData
$formulaDataRemain = $formulaData;
$formulaDataRemain = substr($formulaDataRemain, $token['size']);
* Convert array of tokens into string
* Experimental function under construction not currently in use
$formulaStrings = array();
foreach ($tokens as $token) {
$space0 = isset ($space0) ? $space0 : '';
switch ($token['name']) {
case 'tConcat': // addition
case 'tGT': // greater than
case 'tLE': // less than or equals
case 'tMul': // multiplication
case 'tNE': // multiplication
case 'tSub': // subtraction
$formulaStrings[] = "$op1$space0{$token['data']}$op2";
case 'tArea': // cell range address
$formulaStrings[] = $space0 . $token['data'];
// token is only important for Excel formula evaluator
// token is only important for Excel formula evaluator
case 'tAttrSpace': // space / carriage return
// space will be used when next token arrives, do not alter formulaString stack
switch ($token['data']['spacetype']) {
case 'type0': $space0 = str_repeat(' ', $token['data']['spacecount']); break;
default: break; // unrecognized
case 'tAttrSum': // SUM function with one parameter
$formulaStrings[] = "{ $space0}SUM( $op) ";
case 'tAttrVolatile': // indicates volatile function
// token is only important for Excel formula evaluator
$formulaStrings[] = "$space0{$token['data']}";
case 'tFunc': // function with fixed number of arguments
case 'tFuncV': // function with variable number of arguments
$ops = array(); // array of operators
for ($i = 0; $i < $token['data']['args']; $i++ ) {
$formulaStrings[] = "$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
case 'tParen': // parenthesis
$formulaStrings[] = "($expression)";
$formulaStrings[] = "$space0{$token['data']}";
$formulaStrings[] = "$space0{$token['data']}";
case 'tRef': // single cell reference
$formulaStrings[] = "$space0{$token['data']}";
case 'tRef3d': // 3d cell reference V
$formulaStrings[] = "$space0{$token['data']}";
$formulaStrings[] = "$space0{$token['data']}";
$formulaString = $formulaStrings[0];
echo 'Formula string: ' . $formulaString;
* Fetch next token from portion of formula data (HEX)
* Experimental function under construction not currently in use
// offset: 0; size: 1; token id
$id = ord($formulaDataRemain[0]); // token id
$name = false; // initialize token name
case 0x03: $name = 'tAdd'; $size = 1; $data = '+'; break;
case 0x04: $name = 'tSub'; $size = 1; $data = '-'; break;
case 0x05: $name = 'tMul'; $size = 1; $data = '*'; break;
case 0x06: $name = 'tDiv'; $size = 1; $data = '/'; break;
case 0x07: $name = 'tPower'; $size = 1; $data = '^'; break;
case 0x08: $name = 'tConcat'; $size = 1; $data = '&'; break;
case 0x09: $name = 'tLT'; $size = 1; $data = '<'; break;
case 0x0A: $name = 'tLE'; $size = 1; $data = '<='; break;
case 0x0B: $name = 'tEQ'; $size = 1; $data = '='; break;
case 0x0D: $name = 'tGT'; $size = 1; $data = '>'; break;
case 0x0E: $name = 'tNE'; $size = 1; $data = '<>'; break;
case 0x15: // parenthesis
// offset: 1; size: var; Unicode string, 8-bit string length
$size = 1 + $unicode['size'];
$data = "\"$unicode[string]\"";
case 0x19: // Special attribute
// offset: 1; size: 1; attribute type flags:
switch (ord($formulaDataRemain[1])) {
// offset: 2; size: 2; space type and position
switch (ord($formulaDataRemain[2])) {
case 0x00: $spacetype = 'type0'; break;
default: $name = null; break; // unrecognized
// offset: 3; size: 1; number of inserted spaces/carriage returns
$spacecount = ord($formulaDataRemain[3]);
$data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
$name = null; // unrecognized
// offset: 1; size: 1; 0 = false, 1 = true;
$data = ord($formulaDataRemain[1]) ? 'TRUE' : 'FALSE';
// offset: 1; size: 2; unsigned 16-bit integer
$data = $this->_getInt2d($formulaDataRemain, 1);
case 0x24: // single cell reference e.g. A5
case 0x25: // cell range reference to cells in the same sheet
case 0x41: // function with fixed number of arguments
// offset: 1; size: 2; index to built-in sheet function
switch ($this->_GetInt2d($formulaDataRemain, 1)) {
case 3: $function = 'ISERROR'; $args = 1; break;
case 10: $function = 'NA'; $args = 0; break;
case 15: $function = 'SIN'; $args = 1; break;
case 19: $function = 'PI'; $args = 0; break;
case 20: $function = 'SQRT'; $args = 1; break;
case 25: $function = 'INT'; $args = 1; break;
case 38: $function = 'NOT'; $args = 1; break;
case 39: $function = 'MOD'; $args = 2; break;
case 48: $function = 'TEXT'; $args = 2; break;
case 65: $function = 'DATE'; $args = 3; break;
case 66: $function = 'TIME'; $args = 3; break;
case 67: $function = 'DAY'; $args = 1; break;
case 68: $function = 'MONTH'; $args = 1; break;
case 69: $function = 'YEAR'; $args = 1; break;
case 71: $function = 'HOUR'; $args = 1; break;
case 72: $function = 'MINUTE'; $args = 1; break;
case 73: $function = 'SECOND'; $args = 1; break;
case 74: $function = 'NOW'; $args = 0; break;
case 140: $function = 'DATEVALUE'; $args = 1; break;
case 221: $function = 'TODAY'; $args = 0; break;
default: $name = null; break; // unrecognized
$data = array('function' => $function, 'args' => $args);
case 0x42: // function with variable number of arguments
case 0x62: // function with variable number of arguments
// offset: 1; size: 1; number of arguments
$args = ord($formulaDataRemain[1]);
// offset: 2: size: 2; index to built-in sheet function
switch ($this->_GetInt2d($formulaDataRemain, 2)) {
case 1: $function = 'IF'; break;
case 4: $function = 'SUM'; break;
case 6: $function = 'MIN'; break;
case 29: $function = 'INDEX'; break;
case 36: $function = 'AND'; break;
case 37: $function = 'OR'; break;
case 64: $function = 'MATCH'; break;
case 78: $function = 'OFFSET'; break;
case 102: $function = 'VLOOKUP'; break;
case 345: $function = 'SUMIF'; break;
default: $name = null; // unrecognized
$data = array('function' => $function, 'args' => $args);
case 0x5A: // 3d cell reference V
// offset: 1; size: 2; index to REF entry
// offset: 3; size: 4; cell address
$data = "$sheetRange!$cellAddress";
// case 0x39: // don't know how to deal with
$name = null; // unregognized
echo 'Could not recognize this structure: ' . sprintf('%02X', $id) . "\n";
* Dump a byte sequence, only used for debugging
* Experimental function to be removed
private function _dump($data)
for ($i = 0; $i < strlen($data); $i++ ) {
|