Performance improvements to Excel5 Writer

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@74696 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2011-05-30 22:22:49 +00:00
parent f041495fb8
commit c7539c3d4c
8 changed files with 219 additions and 226 deletions

View File

@ -69,12 +69,12 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr
!isset($aFlippedStringTable[$cellValue]) && !isset($aFlippedStringTable[$cellValue]) &&
($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING2 || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_NULL)) { ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING2 || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_NULL)) {
$aStringTable[] = $cellValue; $aStringTable[] = $cellValue;
$aFlippedStringTable[$cellValue] = 1; $aFlippedStringTable[$cellValue] = true;
} elseif ($cellValue instanceof PHPExcel_RichText && } elseif ($cellValue instanceof PHPExcel_RichText &&
!is_null($cellValue) && !is_null($cellValue) &&
!isset($aFlippedStringTable[$cellValue->getHashCode()])) { !isset($aFlippedStringTable[$cellValue->getHashCode()])) {
$aStringTable[] = $cellValue; $aStringTable[] = $cellValue;
$aFlippedStringTable[$cellValue->getHashCode()] = 1; $aFlippedStringTable[$cellValue->getHashCode()] = true;
} }
} }

View File

@ -49,13 +49,6 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
*/ */
private $_phpExcel; private $_phpExcel;
/**
* The BIFF version of the written Excel file, BIFF5 = 0x0500, BIFF8 = 0x0600
*
* @var integer
*/
private $_BIFF_version = 0x0600;
/** /**
* Total number of shared strings in workbook * Total number of shared strings in workbook
* *
@ -105,9 +98,9 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
* @param PHPExcel $phpExcel PHPExcel object * @param PHPExcel $phpExcel PHPExcel object
*/ */
public function __construct(PHPExcel $phpExcel) { public function __construct(PHPExcel $phpExcel) {
$this->_phpExcel = $phpExcel; $this->_phpExcel = $phpExcel;
$this->_parser = new PHPExcel_Writer_Excel5_Parser(); $this->_parser = new PHPExcel_Writer_Excel5_Parser();
} }
/** /**

View File

@ -92,7 +92,7 @@ class PHPExcel_Writer_Excel5_BIFFwriter
* @var integer * @var integer
* @see _addContinue() * @see _addContinue()
*/ */
public $_limit; public $_limit = 8224;
/** /**
* Constructor * Constructor
@ -101,7 +101,7 @@ class PHPExcel_Writer_Excel5_BIFFwriter
{ {
$this->_data = ''; $this->_data = '';
$this->_datasize = 0; $this->_datasize = 0;
$this->_limit = 2080; // $this->_limit = 8224;
} }
/** /**
@ -171,16 +171,16 @@ class PHPExcel_Writer_Excel5_BIFFwriter
*/ */
function _storeBof($type) function _storeBof($type)
{ {
$record = 0x0809; // Record identifier $record = 0x0809; // Record identifier (BIFF5-BIFF8)
$length = 0x0010; $length = 0x0010;
// by inspection of real files, MS Office Excel 2007 writes the following // by inspection of real files, MS Office Excel 2007 writes the following
$unknown = pack("VV", 0x000100D1, 0x00000406); $unknown = pack("VV", 0x000100D1, 0x00000406);
$build = 0x0DBB; $build = 0x0DBB; // Excel 97
$year = 0x07CC; $year = 0x07CC; // Excel 97
$version = 0x0600; $version = 0x0600; // BIFF8
$header = pack("vv", $record, $length); $header = pack("vv", $record, $length);
$data = pack("vvvv", $version, $type, $build, $year); $data = pack("vvvv", $version, $type, $build, $year);

View File

@ -91,9 +91,9 @@ class PHPExcel_Writer_Excel5_Font
$bFamily = 0; // Font family $bFamily = 0; // Font family
$bCharSet = PHPExcel_Shared_Font::getCharsetFromFontName($this->_font->getName()); // Character set $bCharSet = PHPExcel_Shared_Font::getCharsetFromFontName($this->_font->getName()); // Character set
$record = 0x31; // Record identifier $record = 0x31; // Record identifier
$reserved = 0x00; // Reserved $reserved = 0x00; // Reserved
$grbit = 0x00; // Font attributes $grbit = 0x00; // Font attributes
if ($this->_font->getItalic()) { if ($this->_font->getItalic()) {
$grbit |= 0x02; $grbit |= 0x02;
} }
@ -108,12 +108,12 @@ class PHPExcel_Writer_Excel5_Font
} }
$data = pack("vvvvvCCCC", $data = pack("vvvvvCCCC",
$this->_font->getSize() * 20, $this->_font->getSize() * 20, // Fontsize (in twips)
$grbit, $grbit,
$icv, $icv, // Colour
$this->_mapBold($this->_font->getBold()), self::_mapBold($this->_font->getBold()), // Font weight
$sss, $sss, // Superscript/Subscript
$this->_mapUnderline($this->_font->getUnderline()), self::_mapUnderline($this->_font->getUnderline()),
$bFamily, $bFamily,
$bCharSet, $bCharSet,
$reserved $reserved
@ -132,28 +132,29 @@ class PHPExcel_Writer_Excel5_Font
* @param boolean $bold * @param boolean $bold
* @return int * @return int
*/ */
private function _mapBold($bold) { private static function _mapBold($bold) {
if ($bold) { if ($bold) {
return 0x2BC; return 0x2BC; // 700 = Bold font weight
} }
return 0x190; return 0x190; // 400 = Normal font weight
} }
private static $_mapUnderline = array( PHPExcel_Style_Font::UNDERLINE_NONE => 0x00,
PHPExcel_Style_Font::UNDERLINE_SINGLE => 0x01,
PHPExcel_Style_Font::UNDERLINE_DOUBLE => 0x02,
PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING => 0x21,
PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING => 0x22,
);
/** /**
* Map underline * Map underline
* *
* @param string * @param string
* @return int * @return int
*/ */
private function _mapUnderline($underline) { private static function _mapUnderline($underline) {
switch ($underline) { if (isset(self::$_mapUnderline[$underline]))
case PHPExcel_Style_Font::UNDERLINE_NONE: return 0x00; return self::$_mapUnderline[$underline];
case PHPExcel_Style_Font::UNDERLINE_SINGLE: return 0x01; return 0x00;
case PHPExcel_Style_Font::UNDERLINE_DOUBLE: return 0x02;
case PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING: return 0x21;
case PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING: return 0x22;
default: return 0x00;
}
} }
} }

View File

@ -618,7 +618,7 @@ class PHPExcel_Writer_Excel5_Parser
function _convertFunction($token, $num_args) function _convertFunction($token, $num_args)
{ {
$args = $this->_functions[$token][1]; $args = $this->_functions[$token][1];
$volatile = $this->_functions[$token][3]; // $volatile = $this->_functions[$token][3];
// Fixed number of args eg. TIME($i,$j,$k). // Fixed number of args eg. TIME($i,$j,$k).
if ($args >= 0) { if ($args >= 0) {
@ -649,10 +649,8 @@ class PHPExcel_Writer_Excel5_Parser
} }
// Convert the cell references // Convert the cell references
$cell_array1 = $this->_cellToPackedRowcol($cell1); list($row1, $col1) = $this->_cellToPackedRowcol($cell1);
list($row1, $col1) = $cell_array1; list($row2, $col2) = $this->_cellToPackedRowcol($cell2);
$cell_array2 = $this->_cellToPackedRowcol($cell2);
list($row2, $col2) = $cell_array2;
// The ptg value depends on the class of the ptg. // The ptg value depends on the class of the ptg.
if ($class == 0) { if ($class == 0) {
@ -678,7 +676,7 @@ class PHPExcel_Writer_Excel5_Parser
*/ */
function _convertRange3d($token) function _convertRange3d($token)
{ {
$class = 0; // formulas like Sheet1!$A$1:$A$2 in list type data validation need this class (0x3B) // $class = 0; // formulas like Sheet1!$A$1:$A$2 in list type data validation need this class (0x3B)
// Split the ref at the ! symbol // Split the ref at the ! symbol
list($ext_ref, $range) = explode('!', $token); list($ext_ref, $range) = explode('!', $token);
@ -691,25 +689,22 @@ class PHPExcel_Writer_Excel5_Parser
// Convert the cell references // Convert the cell references
if (preg_match("/^(\\$)?[A-Ia-i]?[A-Za-z](\\$)?(\d+)$/", $cell1)) { if (preg_match("/^(\\$)?[A-Ia-i]?[A-Za-z](\\$)?(\d+)$/", $cell1)) {
$cell_array1 = $this->_cellToPackedRowcol($cell1); list($row1, $col1) = $this->_cellToPackedRowcol($cell1);
list($row1, $col1) = $cell_array1; list($row2, $col2) = $this->_cellToPackedRowcol($cell2);
$cell_array2 = $this->_cellToPackedRowcol($cell2);
list($row2, $col2) = $cell_array2;
} else { // It's a rows range (like 26:27) } else { // It's a rows range (like 26:27)
$cells_array = $this->_rangeToPackedRange($cell1.':'.$cell2); list($row1, $col1, $row2, $col2) = $this->_rangeToPackedRange($cell1.':'.$cell2);
list($row1, $col1, $row2, $col2) = $cells_array;
} }
// The ptg value depends on the class of the ptg. // The ptg value depends on the class of the ptg.
if ($class == 0) { // if ($class == 0) {
$ptgArea = pack("C", $this->ptg['ptgArea3d']); $ptgArea = pack("C", $this->ptg['ptgArea3d']);
} elseif ($class == 1) { // } elseif ($class == 1) {
$ptgArea = pack("C", $this->ptg['ptgArea3dV']); // $ptgArea = pack("C", $this->ptg['ptgArea3dV']);
} elseif ($class == 2) { // } elseif ($class == 2) {
$ptgArea = pack("C", $this->ptg['ptgArea3dA']); // $ptgArea = pack("C", $this->ptg['ptgArea3dA']);
} else { // } else {
throw new Exception("Unknown class $class"); // throw new Exception("Unknown class $class");
} // }
return $ptgArea . $ext_ref . $row1 . $row2 . $col1. $col2; return $ptgArea . $ext_ref . $row1 . $row2 . $col1. $col2;
} }
@ -723,23 +718,23 @@ class PHPExcel_Writer_Excel5_Parser
*/ */
function _convertRef2d($cell) function _convertRef2d($cell)
{ {
$class = 2; // as far as I know, this is magick. // $class = 2; // as far as I know, this is magick.
// Convert the cell reference // Convert the cell reference
$cell_array = $this->_cellToPackedRowcol($cell); $cell_array = $this->_cellToPackedRowcol($cell);
list($row, $col) = $cell_array; list($row, $col) = $cell_array;
// The ptg value depends on the class of the ptg. // The ptg value depends on the class of the ptg.
if ($class == 0) { // if ($class == 0) {
$ptgRef = pack("C", $this->ptg['ptgRef']); // $ptgRef = pack("C", $this->ptg['ptgRef']);
} elseif ($class == 1) { // } elseif ($class == 1) {
$ptgRef = pack("C", $this->ptg['ptgRefV']); // $ptgRef = pack("C", $this->ptg['ptgRefV']);
} elseif ($class == 2) { // } elseif ($class == 2) {
$ptgRef = pack("C", $this->ptg['ptgRefA']); $ptgRef = pack("C", $this->ptg['ptgRefA']);
} else { // } else {
// TODO: use real error codes // // TODO: use real error codes
throw new Exception("Unknown class $class"); // throw new Exception("Unknown class $class");
} // }
return $ptgRef.$row.$col; return $ptgRef.$row.$col;
} }
@ -753,7 +748,7 @@ class PHPExcel_Writer_Excel5_Parser
*/ */
function _convertRef3d($cell) function _convertRef3d($cell)
{ {
$class = 2; // as far as I know, this is magick. // $class = 2; // as far as I know, this is magick.
// Split the ref at the ! symbol // Split the ref at the ! symbol
list($ext_ref, $cell) = explode('!', $cell); list($ext_ref, $cell) = explode('!', $cell);
@ -765,15 +760,15 @@ class PHPExcel_Writer_Excel5_Parser
list($row, $col) = $this->_cellToPackedRowcol($cell); list($row, $col) = $this->_cellToPackedRowcol($cell);
// The ptg value depends on the class of the ptg. // The ptg value depends on the class of the ptg.
if ($class == 0) { // if ($class == 0) {
$ptgRef = pack("C", $this->ptg['ptgRef3d']); // $ptgRef = pack("C", $this->ptg['ptgRef3d']);
} elseif ($class == 1) { // } elseif ($class == 1) {
$ptgRef = pack("C", $this->ptg['ptgRef3dV']); // $ptgRef = pack("C", $this->ptg['ptgRef3dV']);
} elseif ($class == 2) { // } elseif ($class == 2) {
$ptgRef = pack("C", $this->ptg['ptgRef3dA']); $ptgRef = pack("C", $this->ptg['ptgRef3dA']);
} else { // } else {
throw new Exception("Unknown class $class"); // throw new Exception("Unknown class $class");
} // }
return $ptgRef . $ext_ref. $row . $col; return $ptgRef . $ext_ref. $row . $col;
} }

View File

@ -220,7 +220,7 @@ class PHPExcel_Writer_Excel5_Workbook extends PHPExcel_Writer_Excel5_BIFFwriter
$this->_phpExcel = $phpExcel; $this->_phpExcel = $phpExcel;
// set BIFFwriter limit for CONTINUE records // set BIFFwriter limit for CONTINUE records
$this->_limit = 8224; // $this->_limit = 8224;
$this->_codepage = 0x04B0; $this->_codepage = 0x04B0;
// Add empty sheets and Build color cache // Add empty sheets and Build color cache
@ -293,8 +293,7 @@ class PHPExcel_Writer_Excel5_Workbook extends PHPExcel_Writer_Excel5_BIFFwriter
$this->_numberFormats[$numberFormatIndex] = $style->getNumberFormat(); $this->_numberFormats[$numberFormatIndex] = $style->getNumberFormat();
$this->_addedNumberFormats[$numberFormatHashCode] = $numberFormatIndex; $this->_addedNumberFormats[$numberFormatHashCode] = $numberFormatIndex;
} }
} } else {
else {
$numberFormatIndex = (int) $style->getNumberFormat()->getBuiltInFormatCode(); $numberFormatIndex = (int) $style->getNumberFormat()->getBuiltInFormatCode();
} }
@ -1270,24 +1269,18 @@ class PHPExcel_Writer_Excel5_Workbook extends PHPExcel_Writer_Excel5_BIFFwriter
// there will be need for more than one cylcle, if string longer than one record data block, there // there will be need for more than one cylcle, if string longer than one record data block, there
// may be need for even more cycles // may be need for even more cycles
if (strlen($recordData) + strlen($string) < $continue_limit) { if (strlen($recordData) + strlen($string) <= $continue_limit) {
// then we can write the string (or remainder of string) without any problems // then we can write the string (or remainder of string) without any problems
$recordData .= $string; $recordData .= $string;
// we are finished writing this string if (strlen($recordData) + strlen($string) == $continue_limit) {
$finished = true; // we close the record data block, and initialize a new one
$recordDatas[] = $recordData;
} else if (strlen($recordData) + strlen($string) == $continue_limit) { $recordData = '';
// then we can also write the string (or remainder of string) }
$recordData .= $string;
// but we close the record data block, and initialize a new one
$recordDatas[] = $recordData;
$recordData = '';
// we are finished writing this string // we are finished writing this string
$finished = true; $finished = true;
} else { } else {
// special treatment writing the string (or remainder of the string) // special treatment writing the string (or remainder of the string)
// If the string is very long it may need to be written in more than one CONTINUE record. // If the string is very long it may need to be written in more than one CONTINUE record.

View File

@ -209,7 +209,7 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
parent::__construct(); parent::__construct();
// change BIFFwriter limit for CONTINUE records // change BIFFwriter limit for CONTINUE records
$this->_limit = 8224; // $this->_limit = 8224;
$this->_preCalculateFormulas = $preCalculateFormulas; $this->_preCalculateFormulas = $preCalculateFormulas;
@ -236,20 +236,22 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
$this->_outline_on = 1; $this->_outline_on = 1;
// calculate values for DIMENSIONS record // calculate values for DIMENSIONS record
$col = $row = array(); $minR = $maxR = 1;
$minC = $maxC = '1A';
foreach ($this->_phpSheet->getCellCollection(false) as $cellID) { foreach ($this->_phpSheet->getCellCollection(false) as $cellID) {
list($c,$r) = sscanf($cellID,'%[A-Z]%d'); list($c,$r) = sscanf($cellID,'%[A-Z]%d');
$row[$r] = $r; $minR = min($minR,$r);
$col[$c] = strlen($c).$c; $maxR = max($maxR,$r);
$C = strlen($c).$c;
$minC = min($minC,$C);
$maxC = max($maxC,$C);
} }
// Determine lowest and highest column and row // Determine lowest and highest column and row
$this->_firstRowIndex = (count($row) > 0) ? min($row) : 1; $this->_firstRowIndex = ($minR > 65535) ? $minR: 65535;
$this->_lastRowIndex = (count($row) > 0) ? max($row) : 1; $this->_lastRowIndex = ($maxR > 65535) ? $maxR : 65535;
if ($this->_firstRowIndex > 65535) $this->_firstRowIndex = 65535;
if ($this->_lastRowIndex > 65535) $this->_lastRowIndex = 65535;
$this->_firstColumnIndex = (count($col) > 0) ? PHPExcel_Cell::columnIndexFromString(substr(min($col),1)) : 1; $this->_firstColumnIndex = PHPExcel_Cell::columnIndexFromString(substr($minC,1));
$this->_lastColumnIndex = (count($col) > 0) ? PHPExcel_Cell::columnIndexFromString(substr(max($col),1)) : 1; $this->_lastColumnIndex = PHPExcel_Cell::columnIndexFromString(substr($maxC,1));
if ($this->_firstColumnIndex > 255) $this->_firstColumnIndex = 255; if ($this->_firstColumnIndex > 255) $this->_firstColumnIndex = 255;
if ($this->_lastColumnIndex > 255) $this->_lastColumnIndex = 255; if ($this->_lastColumnIndex > 255) $this->_lastColumnIndex = 255;
@ -266,7 +268,9 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
*/ */
function close() function close()
{ {
$num_sheets = $this->_phpSheet->getParent()->getSheetCount(); $_phpSheet = $this->_phpSheet;
$num_sheets = $_phpSheet->getParent()->getSheetCount();
// Write BOF record // Write BOF record
$this->_storeBof(0x0010); $this->_storeBof(0x0010);
@ -281,21 +285,21 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
$this->_writeGridset(); $this->_writeGridset();
// Calculate column widths // Calculate column widths
$this->_phpSheet->calculateColumnWidths(); $_phpSheet->calculateColumnWidths();
// Column dimensions // Column dimensions
$maxCol = PHPExcel_Cell::columnIndexFromString($this->_phpSheet->getHighestColumn()) -1; if (($defaultWidth = $_phpSheet->getDefaultColumnDimension()->getWidth()) < 0) {
$columnDimensions = $this->_phpSheet->getColumnDimensions(); $defaultWidth = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($_phpSheet->getParent()->getDefaultStyle()->getFont());
}
$columnDimensions = $_phpSheet->getColumnDimensions();
$maxCol = $this->_lastColumnIndex -1;
for ($i = 0; $i <= $maxCol; ++$i) { for ($i = 0; $i <= $maxCol; ++$i) {
$hidden = 0; $hidden = 0;
$level = 0; $level = 0;
$xfIndex = 15; // there are 15 cell style Xfs $xfIndex = 15; // there are 15 cell style Xfs
if ($this->_phpSheet->getDefaultColumnDimension()->getWidth() >= 0) { $width = $defaultWidth;
$width = $this->_phpSheet->getDefaultColumnDimension()->getWidth();
} else {
$width = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($this->_phpSheet->getParent()->getDefaultStyle()->getFont());
}
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($i); $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
if (isset($columnDimensions[$columnLetter])) { if (isset($columnDimensions[$columnLetter])) {
@ -384,55 +388,57 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
$this->_writeDimensions(); $this->_writeDimensions();
// Row dimensions // Row dimensions
foreach ($this->_phpSheet->getRowDimensions() as $rowDimension) { foreach ($_phpSheet->getRowDimensions() as $rowDimension) {
$xfIndex = $rowDimension->getXfIndex() + 15; // there are 15 cellXfs $xfIndex = $rowDimension->getXfIndex() + 15; // there are 15 cellXfs
$this->_writeRow( $rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), $xfIndex, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel() ); $this->_writeRow( $rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), $xfIndex, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel() );
} }
// Write Cells // Write Cells
foreach ($this->_phpSheet->getCellCollection() as $cellID) { foreach ($_phpSheet->getCellCollection() as $cellID) {
$cell = $this->_phpSheet->getCell($cellID); $cell = $_phpSheet->getCell($cellID);
$row = $cell->getRow() - 1; $row = $cell->getRow() - 1;
$column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1; $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
// Don't break Excel! // Don't break Excel!
if ($row + 1 > 65536 or $column + 1 > 256) { // if ($row + 1 > 65536 or $column + 1 > 256) {
if ($row > 65535 || $column > 255) {
break; break;
} }
// Write cell value // Write cell value
$xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs $xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs
if ($cell->getValue() instanceof PHPExcel_RichText) { $cVal = $cell->getValue();
$this->_writeString($row, $column, $cell->getValue()->getPlainText(), $xfIndex); if ($cVal instanceof PHPExcel_RichText) {
$this->_writeString($row, $column, $cVal->getPlainText(), $xfIndex);
} else { } else {
switch ($cell->getDatatype()) { switch ($cell->getDatatype()) {
case PHPExcel_Cell_DataType::TYPE_STRING:
if ($cVal === '' || $cVal === null) {
$this->_writeBlank($row, $column, $xfIndex);
} else {
$this->_writeString($row, $column, $cVal, $xfIndex);
}
break;
case PHPExcel_Cell_DataType::TYPE_STRING: case PHPExcel_Cell_DataType::TYPE_NUMERIC:
if ($cell->getValue() === '' or $cell->getValue() === null) { $this->_writeNumber($row, $column, $cVal, $xfIndex);
$this->_writeBlank($row, $column, $xfIndex); break;
} else {
$this->_writeString($row, $column, $cell->getValue(), $xfIndex);
}
break;
case PHPExcel_Cell_DataType::TYPE_FORMULA: case PHPExcel_Cell_DataType::TYPE_FORMULA:
$calculatedValue = $this->_preCalculateFormulas ? $calculatedValue = $this->_preCalculateFormulas ?
$cell->getCalculatedValue() : null; $cell->getCalculatedValue() : null;
$this->_writeFormula($row, $column, $cell->getValue(), $xfIndex, $calculatedValue); $this->_writeFormula($row, $column, $cVal, $xfIndex, $calculatedValue);
break; break;
case PHPExcel_Cell_DataType::TYPE_BOOL: case PHPExcel_Cell_DataType::TYPE_BOOL:
$this->_writeBoolErr($row, $column, $cell->getValue(), 0, $xfIndex); $this->_writeBoolErr($row, $column, $cVal, 0, $xfIndex);
break; break;
case PHPExcel_Cell_DataType::TYPE_ERROR: case PHPExcel_Cell_DataType::TYPE_ERROR:
$this->_writeBoolErr($row, $column, $this->_mapErrorCode($cell->getValue()), 1, $xfIndex); $this->_writeBoolErr($row, $column, self::_mapErrorCode($cVal), 1, $xfIndex);
break; break;
case PHPExcel_Cell_DataType::TYPE_NUMERIC:
$this->_writeNumber($row, $column, $cell->getValue(), $xfIndex);
break;
} }
} }
} }
@ -442,14 +448,14 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
$this->_writeWindow2(); $this->_writeWindow2();
$this->_writeZoom(); $this->_writeZoom();
if ($this->_phpSheet->getFreezePane()) { if ($_phpSheet->getFreezePane()) {
$this->_writePanes(); $this->_writePanes();
} }
$this->_writeSelection(); $this->_writeSelection();
$this->_writeMergedCells(); $this->_writeMergedCells();
// Hyperlinks // Hyperlinks
foreach ($this->_phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) { foreach ($_phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) {
list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinate); list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinate);
$url = $hyperlink->getUrl(); $url = $hyperlink->getUrl();
@ -789,7 +795,7 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
} elseif (is_string($calculatedValue)) { } elseif (is_string($calculatedValue)) {
if (array_key_exists($calculatedValue, PHPExcel_Cell_DataType::getErrorCodes())) { if (array_key_exists($calculatedValue, PHPExcel_Cell_DataType::getErrorCodes())) {
// Error value // Error value
$num = pack('CCCvCv', 0x02, 0x00, $this->_mapErrorCode($calculatedValue), 0x00, 0x00, 0xFFFF); $num = pack('CCCvCv', 0x02, 0x00, self::_mapErrorCode($calculatedValue), 0x00, 0x00, 0xFFFF);
} elseif ($calculatedValue === '') { } elseif ($calculatedValue === '') {
// Empty string (and BIFF8) // Empty string (and BIFF8)
$num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF); $num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
@ -2839,7 +2845,7 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
/** /**
* Map Error code * Map Error code
*/ */
private function _mapErrorCode($errorCode) { private static function _mapErrorCode($errorCode) {
switch ($errorCode) { switch ($errorCode) {
case '#NULL!': return 0x00; case '#NULL!': return 0x00;
case '#DIV/0!': return 0x07; case '#DIV/0!': return 0x07;

View File

@ -174,38 +174,38 @@ class PHPExcel_Writer_Excel5_Xf
if ($this->_isStyleXf) { if ($this->_isStyleXf) {
$style = 0xFFF5; $style = 0xFFF5;
} else { } else {
$style = $this->_mapLocked($this->_style->getProtection()->getLocked()); $style = self::_mapLocked($this->_style->getProtection()->getLocked());
$style |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1; $style |= self::_mapHidden($this->_style->getProtection()->getHidden()) << 1;
} }
// Flags to indicate if attributes have been set. // Flags to indicate if attributes have been set.
$atr_num = ($this->_numberFormatIndex != 0)?1:0; $atr_num = ($this->_numberFormatIndex != 0)?1:0;
$atr_fnt = ($this->_fontIndex != 0)?1:0; $atr_fnt = ($this->_fontIndex != 0)?1:0;
$atr_alc = ((int) $this->_style->getAlignment()->getWrapText())?1:0; $atr_alc = ((int) $this->_style->getAlignment()->getWrapText())?1:0;
$atr_bdr = ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) || $atr_bdr = (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
$this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) || self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
$this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) || self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
$this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0; self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
$atr_pat = (($this->_fg_color != 0x40) || $atr_pat = (($this->_fg_color != 0x40) ||
($this->_bg_color != 0x41) || ($this->_bg_color != 0x41) ||
$this->_mapFillType($this->_style->getFill()->getFillType()))?1:0; self::_mapFillType($this->_style->getFill()->getFillType()))?1:0;
$atr_prot = $this->_mapLocked($this->_style->getProtection()->getLocked()) $atr_prot = self::_mapLocked($this->_style->getProtection()->getLocked())
| $this->_mapHidden($this->_style->getProtection()->getHidden()); | self::_mapHidden($this->_style->getProtection()->getHidden());
// Zero the default border colour if the border has not been set. // Zero the default border colour if the border has not been set.
if ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) { if (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
$this->_bottom_color = 0; $this->_bottom_color = 0;
} }
if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) { if (self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
$this->_top_color = 0; $this->_top_color = 0;
} }
if ($this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) { if (self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
$this->_right_color = 0; $this->_right_color = 0;
} }
if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) { if (self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
$this->_left_color = 0; $this->_left_color = 0;
} }
if ($this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) { if (self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
$this->_diag_color = 0; $this->_diag_color = 0;
} }
@ -217,7 +217,7 @@ class PHPExcel_Writer_Excel5_Xf
$align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
$align |= (int) $this->_style->getAlignment()->getWrapText() << 3; $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
$align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4; $align |= self::_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
$align |= $this->_text_justlast << 7; $align |= $this->_text_justlast << 7;
$used_attrib = $atr_num << 2; $used_attrib = $atr_num << 2;
@ -230,10 +230,10 @@ class PHPExcel_Writer_Excel5_Xf
$icv = $this->_fg_color; // fg and bg pattern colors $icv = $this->_fg_color; // fg and bg pattern colors
$icv |= $this->_bg_color << 7; $icv |= $this->_bg_color << 7;
$border1 = $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color $border1 = self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4; $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8; $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12; $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
$border1 |= $this->_left_color << 16; $border1 |= $this->_left_color << 16;
$border1 |= $this->_right_color << 23; $border1 |= $this->_right_color << 23;
@ -248,8 +248,8 @@ class PHPExcel_Writer_Excel5_Xf
$border2 = $this->_top_color; // Border color $border2 = $this->_top_color; // Border color
$border2 |= $this->_bottom_color << 7; $border2 |= $this->_bottom_color << 7;
$border2 |= $this->_diag_color << 14; $border2 |= $this->_diag_color << 14;
$border2 |= $this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21; $border2 |= self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
$border2 |= $this->_mapFillType($this->_style->getFill()->getFillType()) << 26; $border2 |= self::_mapFillType($this->_style->getFill()->getFillType()) << 26;
$header = pack("vv", $record, $length); $header = pack("vv", $record, $length);
@ -259,7 +259,7 @@ class PHPExcel_Writer_Excel5_Xf
$data = pack("vvvC", $ifnt, $ifmt, $style, $align); $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
$data .= pack("CCC" $data .= pack("CCC"
, $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation()) , self::_mapTextRotation($this->_style->getAlignment()->getTextRotation())
, $biff8_options , $biff8_options
, $used_attrib , $used_attrib
); );
@ -378,59 +378,69 @@ class PHPExcel_Writer_Excel5_Xf
$this->_fontIndex = $value; $this->_fontIndex = $value;
} }
private static $_mapBorderStyle = array ( PHPExcel_Style_Border::BORDER_NONE => 0x00,
PHPExcel_Style_Border::BORDER_THIN => 0x01,
PHPExcel_Style_Border::BORDER_MEDIUM => 0x02,
PHPExcel_Style_Border::BORDER_DASHED => 0x03,
PHPExcel_Style_Border::BORDER_DOTTED => 0x04,
PHPExcel_Style_Border::BORDER_THICK => 0x05,
PHPExcel_Style_Border::BORDER_DOUBLE => 0x06,
PHPExcel_Style_Border::BORDER_HAIR => 0x07,
PHPExcel_Style_Border::BORDER_MEDIUMDASHED => 0x08,
PHPExcel_Style_Border::BORDER_DASHDOT => 0x09,
PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT => 0x0A,
PHPExcel_Style_Border::BORDER_DASHDOTDOT => 0x0B,
PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT => 0x0C,
PHPExcel_Style_Border::BORDER_SLANTDASHDOT => 0x0D,
);
/** /**
* Map border style * Map border style
*/ */
private function _mapBorderStyle($borderStyle) { private static function _mapBorderStyle($borderStyle) {
switch ($borderStyle) { if (isset(self::$_mapBorderStyle[$borderStyle]))
case PHPExcel_Style_Border::BORDER_NONE: return 0x00; return self::$_mapBorderStyle[$borderStyle];
case PHPExcel_Style_Border::BORDER_THIN; return 0x01; return 0x00;
case PHPExcel_Style_Border::BORDER_MEDIUM; return 0x02;
case PHPExcel_Style_Border::BORDER_DASHED; return 0x03;
case PHPExcel_Style_Border::BORDER_DOTTED; return 0x04;
case PHPExcel_Style_Border::BORDER_THICK; return 0x05;
case PHPExcel_Style_Border::BORDER_DOUBLE; return 0x06;
case PHPExcel_Style_Border::BORDER_HAIR; return 0x07;
case PHPExcel_Style_Border::BORDER_MEDIUMDASHED; return 0x08;
case PHPExcel_Style_Border::BORDER_DASHDOT; return 0x09;
case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT; return 0x0A;
case PHPExcel_Style_Border::BORDER_DASHDOTDOT; return 0x0B;
case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT; return 0x0C;
case PHPExcel_Style_Border::BORDER_SLANTDASHDOT; return 0x0D;
default: return 0x00;
}
} }
private static $_mapFillType = array( PHPExcel_Style_Fill::FILL_NONE => 0x00,
PHPExcel_Style_Fill::FILL_SOLID => 0x01,
PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY => 0x03,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04,
PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05,
PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06,
PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN => 0x07,
PHPExcel_Style_Fill::FILL_PATTERN_DARKUP => 0x08,
PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID => 0x09,
PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F,
PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10,
PHPExcel_Style_Fill::FILL_PATTERN_GRAY125 => 0x11,
PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625 => 0x12,
PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8
PHPExcel_Style_Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8
);
/** /**
* Map fill type * Map fill type
*/ */
private function _mapFillType($fillType) { private static function _mapFillType($fillType) {
switch ($fillType) { if (isset(self::$_mapFillType[$fillType]))
case PHPExcel_Style_Fill::FILL_NONE: return 0x00; return self::$_mapFillType[$fillType];
case PHPExcel_Style_Fill::FILL_SOLID: return 0x01; return 0x00;
case PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY: return 0x02;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY: return 0x03;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY: return 0x04;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL: return 0x05;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL: return 0x06;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN: return 0x07;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKUP: return 0x08;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID: return 0x09;
case PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS: return 0x0A;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL: return 0x0B;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL: return 0x0C;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN: return 0x0D;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP: return 0x0E;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID: return 0x0F;
case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS: return 0x10;
case PHPExcel_Style_Fill::FILL_PATTERN_GRAY125: return 0x11;
case PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625: return 0x12;
case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR: // does not exist in BIFF8
case PHPExcel_Style_Fill::FILL_GRADIENT_PATH: // does not exist in BIFF8
default: return 0x00;
}
} }
private static $_mapHAlign = array( PHPExcel_Style_Alignment::HORIZONTAL_GENERAL => 0,
PHPExcel_Style_Alignment::HORIZONTAL_LEFT => 1,
PHPExcel_Style_Alignment::HORIZONTAL_CENTER => 2,
PHPExcel_Style_Alignment::HORIZONTAL_RIGHT => 3,
PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY => 5,
PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6,
);
/** /**
* Map to BIFF2-BIFF8 codes for horizontal alignment * Map to BIFF2-BIFF8 codes for horizontal alignment
* *
@ -439,31 +449,26 @@ class PHPExcel_Writer_Excel5_Xf
*/ */
private function _mapHAlign($hAlign) private function _mapHAlign($hAlign)
{ {
switch ($hAlign) { if (isset(self::$_mapHAlign[$hAlign]))
case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL: return 0; return self::$_mapHAlign[$hAlign];
case PHPExcel_Style_Alignment::HORIZONTAL_LEFT: return 1; return 0;
case PHPExcel_Style_Alignment::HORIZONTAL_CENTER: return 2;
case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT: return 3;
case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY: return 5;
case PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS: return 6;
default: return 0;
}
} }
private static $_mapVAlign = array( PHPExcel_Style_Alignment::VERTICAL_TOP => 0,
PHPExcel_Style_Alignment::VERTICAL_CENTER => 1,
PHPExcel_Style_Alignment::VERTICAL_BOTTOM => 2,
PHPExcel_Style_Alignment::VERTICAL_JUSTIFY => 3,
);
/** /**
* Map to BIFF2-BIFF8 codes for vertical alignment * Map to BIFF2-BIFF8 codes for vertical alignment
* *
* @param string $vAlign * @param string $vAlign
* @return int * @return int
*/ */
private function _mapVAlign($vAlign) { private static function _mapVAlign($vAlign) {
switch ($vAlign) { if (isset(self::$_mapVAlign[$vAlign]))
case PHPExcel_Style_Alignment::VERTICAL_TOP: return 0; return self::$_mapVAlign[$vAlign];
case PHPExcel_Style_Alignment::VERTICAL_CENTER: return 1; return 2;
case PHPExcel_Style_Alignment::VERTICAL_BOTTOM: return 2;
case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY: return 3;
default: return 2;
}
} }
/** /**
@ -472,7 +477,7 @@ class PHPExcel_Writer_Excel5_Xf
* @param int $textRotation * @param int $textRotation
* @return int * @return int
*/ */
private function _mapTextRotation($textRotation) { private static function _mapTextRotation($textRotation) {
if ($textRotation >= 0) { if ($textRotation >= 0) {
return $textRotation; return $textRotation;
} }
@ -490,7 +495,7 @@ class PHPExcel_Writer_Excel5_Xf
* @param string * @param string
* @return int * @return int
*/ */
private function _mapLocked($locked) { private static function _mapLocked($locked) {
switch ($locked) { switch ($locked) {
case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 1; case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 1;
case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1; case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
@ -505,7 +510,7 @@ class PHPExcel_Writer_Excel5_Xf
* @param string * @param string
* @return int * @return int
*/ */
private function _mapHidden($hidden) { private static function _mapHidden($hidden) {
switch ($hidden) { switch ($hidden) {
case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 0; case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 0;
case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1; case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;