From c7539c3d4c7df6e0426bd8b5cb679bdcfed955b8 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 30 May 2011 22:22:49 +0000 Subject: [PATCH] Performance improvements to Excel5 Writer git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@74696 2327b42d-5241-43d6-9e2a-de5ac946f064 --- .../PHPExcel/Writer/Excel2007/StringTable.php | 4 +- Classes/PHPExcel/Writer/Excel5.php | 11 +- Classes/PHPExcel/Writer/Excel5/BIFFwriter.php | 12 +- Classes/PHPExcel/Writer/Excel5/Font.php | 41 ++-- Classes/PHPExcel/Writer/Excel5/Parser.php | 73 ++++---- Classes/PHPExcel/Writer/Excel5/Workbook.php | 23 +-- Classes/PHPExcel/Writer/Excel5/Worksheet.php | 106 ++++++----- Classes/PHPExcel/Writer/Excel5/Xf.php | 175 +++++++++--------- 8 files changed, 219 insertions(+), 226 deletions(-) diff --git a/Classes/PHPExcel/Writer/Excel2007/StringTable.php b/Classes/PHPExcel/Writer/Excel2007/StringTable.php index 2f2ff03..8165df1 100644 --- a/Classes/PHPExcel/Writer/Excel2007/StringTable.php +++ b/Classes/PHPExcel/Writer/Excel2007/StringTable.php @@ -69,12 +69,12 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr !isset($aFlippedStringTable[$cellValue]) && ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING2 || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_NULL)) { $aStringTable[] = $cellValue; - $aFlippedStringTable[$cellValue] = 1; + $aFlippedStringTable[$cellValue] = true; } elseif ($cellValue instanceof PHPExcel_RichText && !is_null($cellValue) && !isset($aFlippedStringTable[$cellValue->getHashCode()])) { $aStringTable[] = $cellValue; - $aFlippedStringTable[$cellValue->getHashCode()] = 1; + $aFlippedStringTable[$cellValue->getHashCode()] = true; } } diff --git a/Classes/PHPExcel/Writer/Excel5.php b/Classes/PHPExcel/Writer/Excel5.php index a951a43..f64b8de 100644 --- a/Classes/PHPExcel/Writer/Excel5.php +++ b/Classes/PHPExcel/Writer/Excel5.php @@ -49,13 +49,6 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter */ 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 * @@ -105,9 +98,9 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter * @param PHPExcel $phpExcel PHPExcel object */ 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(); } /** diff --git a/Classes/PHPExcel/Writer/Excel5/BIFFwriter.php b/Classes/PHPExcel/Writer/Excel5/BIFFwriter.php index f6cdb94..280c97d 100644 --- a/Classes/PHPExcel/Writer/Excel5/BIFFwriter.php +++ b/Classes/PHPExcel/Writer/Excel5/BIFFwriter.php @@ -92,7 +92,7 @@ class PHPExcel_Writer_Excel5_BIFFwriter * @var integer * @see _addContinue() */ - public $_limit; + public $_limit = 8224; /** * Constructor @@ -101,7 +101,7 @@ class PHPExcel_Writer_Excel5_BIFFwriter { $this->_data = ''; $this->_datasize = 0; - $this->_limit = 2080; +// $this->_limit = 8224; } /** @@ -171,16 +171,16 @@ class PHPExcel_Writer_Excel5_BIFFwriter */ function _storeBof($type) { - $record = 0x0809; // Record identifier + $record = 0x0809; // Record identifier (BIFF5-BIFF8) $length = 0x0010; // by inspection of real files, MS Office Excel 2007 writes the following $unknown = pack("VV", 0x000100D1, 0x00000406); - $build = 0x0DBB; - $year = 0x07CC; + $build = 0x0DBB; // Excel 97 + $year = 0x07CC; // Excel 97 - $version = 0x0600; + $version = 0x0600; // BIFF8 $header = pack("vv", $record, $length); $data = pack("vvvv", $version, $type, $build, $year); diff --git a/Classes/PHPExcel/Writer/Excel5/Font.php b/Classes/PHPExcel/Writer/Excel5/Font.php index 25c3263..1b81579 100644 --- a/Classes/PHPExcel/Writer/Excel5/Font.php +++ b/Classes/PHPExcel/Writer/Excel5/Font.php @@ -91,9 +91,9 @@ class PHPExcel_Writer_Excel5_Font $bFamily = 0; // Font family $bCharSet = PHPExcel_Shared_Font::getCharsetFromFontName($this->_font->getName()); // Character set - $record = 0x31; // Record identifier - $reserved = 0x00; // Reserved - $grbit = 0x00; // Font attributes + $record = 0x31; // Record identifier + $reserved = 0x00; // Reserved + $grbit = 0x00; // Font attributes if ($this->_font->getItalic()) { $grbit |= 0x02; } @@ -108,12 +108,12 @@ class PHPExcel_Writer_Excel5_Font } $data = pack("vvvvvCCCC", - $this->_font->getSize() * 20, + $this->_font->getSize() * 20, // Fontsize (in twips) $grbit, - $icv, - $this->_mapBold($this->_font->getBold()), - $sss, - $this->_mapUnderline($this->_font->getUnderline()), + $icv, // Colour + self::_mapBold($this->_font->getBold()), // Font weight + $sss, // Superscript/Subscript + self::_mapUnderline($this->_font->getUnderline()), $bFamily, $bCharSet, $reserved @@ -132,28 +132,29 @@ class PHPExcel_Writer_Excel5_Font * @param boolean $bold * @return int */ - private function _mapBold($bold) { + private static function _mapBold($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 * * @param string * @return int */ - private function _mapUnderline($underline) { - switch ($underline) { - case PHPExcel_Style_Font::UNDERLINE_NONE: return 0x00; - case PHPExcel_Style_Font::UNDERLINE_SINGLE: return 0x01; - 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; - } + private static function _mapUnderline($underline) { + if (isset(self::$_mapUnderline[$underline])) + return self::$_mapUnderline[$underline]; + return 0x00; } } diff --git a/Classes/PHPExcel/Writer/Excel5/Parser.php b/Classes/PHPExcel/Writer/Excel5/Parser.php index c698f20..5dc50c2 100644 --- a/Classes/PHPExcel/Writer/Excel5/Parser.php +++ b/Classes/PHPExcel/Writer/Excel5/Parser.php @@ -618,7 +618,7 @@ class PHPExcel_Writer_Excel5_Parser function _convertFunction($token, $num_args) { $args = $this->_functions[$token][1]; - $volatile = $this->_functions[$token][3]; +// $volatile = $this->_functions[$token][3]; // Fixed number of args eg. TIME($i,$j,$k). if ($args >= 0) { @@ -649,10 +649,8 @@ class PHPExcel_Writer_Excel5_Parser } // Convert the cell references - $cell_array1 = $this->_cellToPackedRowcol($cell1); - list($row1, $col1) = $cell_array1; - $cell_array2 = $this->_cellToPackedRowcol($cell2); - list($row2, $col2) = $cell_array2; + list($row1, $col1) = $this->_cellToPackedRowcol($cell1); + list($row2, $col2) = $this->_cellToPackedRowcol($cell2); // The ptg value depends on the class of the ptg. if ($class == 0) { @@ -678,7 +676,7 @@ class PHPExcel_Writer_Excel5_Parser */ 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 list($ext_ref, $range) = explode('!', $token); @@ -691,25 +689,22 @@ class PHPExcel_Writer_Excel5_Parser // Convert the cell references if (preg_match("/^(\\$)?[A-Ia-i]?[A-Za-z](\\$)?(\d+)$/", $cell1)) { - $cell_array1 = $this->_cellToPackedRowcol($cell1); - list($row1, $col1) = $cell_array1; - $cell_array2 = $this->_cellToPackedRowcol($cell2); - list($row2, $col2) = $cell_array2; + list($row1, $col1) = $this->_cellToPackedRowcol($cell1); + list($row2, $col2) = $this->_cellToPackedRowcol($cell2); } else { // It's a rows range (like 26:27) - $cells_array = $this->_rangeToPackedRange($cell1.':'.$cell2); - list($row1, $col1, $row2, $col2) = $cells_array; + list($row1, $col1, $row2, $col2) = $this->_rangeToPackedRange($cell1.':'.$cell2); } // The ptg value depends on the class of the ptg. - if ($class == 0) { +// if ($class == 0) { $ptgArea = pack("C", $this->ptg['ptgArea3d']); - } elseif ($class == 1) { - $ptgArea = pack("C", $this->ptg['ptgArea3dV']); - } elseif ($class == 2) { - $ptgArea = pack("C", $this->ptg['ptgArea3dA']); - } else { - throw new Exception("Unknown class $class"); - } +// } elseif ($class == 1) { +// $ptgArea = pack("C", $this->ptg['ptgArea3dV']); +// } elseif ($class == 2) { +// $ptgArea = pack("C", $this->ptg['ptgArea3dA']); +// } else { +// throw new Exception("Unknown class $class"); +// } return $ptgArea . $ext_ref . $row1 . $row2 . $col1. $col2; } @@ -723,23 +718,23 @@ class PHPExcel_Writer_Excel5_Parser */ 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 $cell_array = $this->_cellToPackedRowcol($cell); list($row, $col) = $cell_array; // The ptg value depends on the class of the ptg. - if ($class == 0) { - $ptgRef = pack("C", $this->ptg['ptgRef']); - } elseif ($class == 1) { - $ptgRef = pack("C", $this->ptg['ptgRefV']); - } elseif ($class == 2) { +// if ($class == 0) { +// $ptgRef = pack("C", $this->ptg['ptgRef']); +// } elseif ($class == 1) { +// $ptgRef = pack("C", $this->ptg['ptgRefV']); +// } elseif ($class == 2) { $ptgRef = pack("C", $this->ptg['ptgRefA']); - } else { - // TODO: use real error codes - throw new Exception("Unknown class $class"); - } +// } else { +// // TODO: use real error codes +// throw new Exception("Unknown class $class"); +// } return $ptgRef.$row.$col; } @@ -753,7 +748,7 @@ class PHPExcel_Writer_Excel5_Parser */ 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 list($ext_ref, $cell) = explode('!', $cell); @@ -765,15 +760,15 @@ class PHPExcel_Writer_Excel5_Parser list($row, $col) = $this->_cellToPackedRowcol($cell); // The ptg value depends on the class of the ptg. - if ($class == 0) { - $ptgRef = pack("C", $this->ptg['ptgRef3d']); - } elseif ($class == 1) { - $ptgRef = pack("C", $this->ptg['ptgRef3dV']); - } elseif ($class == 2) { +// if ($class == 0) { +// $ptgRef = pack("C", $this->ptg['ptgRef3d']); +// } elseif ($class == 1) { +// $ptgRef = pack("C", $this->ptg['ptgRef3dV']); +// } elseif ($class == 2) { $ptgRef = pack("C", $this->ptg['ptgRef3dA']); - } else { - throw new Exception("Unknown class $class"); - } +// } else { +// throw new Exception("Unknown class $class"); +// } return $ptgRef . $ext_ref. $row . $col; } diff --git a/Classes/PHPExcel/Writer/Excel5/Workbook.php b/Classes/PHPExcel/Writer/Excel5/Workbook.php index 3fdd69b..0e0bbda 100644 --- a/Classes/PHPExcel/Writer/Excel5/Workbook.php +++ b/Classes/PHPExcel/Writer/Excel5/Workbook.php @@ -220,7 +220,7 @@ class PHPExcel_Writer_Excel5_Workbook extends PHPExcel_Writer_Excel5_BIFFwriter $this->_phpExcel = $phpExcel; // set BIFFwriter limit for CONTINUE records - $this->_limit = 8224; + // $this->_limit = 8224; $this->_codepage = 0x04B0; // 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->_addedNumberFormats[$numberFormatHashCode] = $numberFormatIndex; } - } - else { + } else { $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 // 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 $recordData .= $string; - // we are finished writing this string - $finished = true; - - } else if (strlen($recordData) + strlen($string) == $continue_limit) { - // 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 = ''; + if (strlen($recordData) + strlen($string) == $continue_limit) { + // we close the record data block, and initialize a new one + $recordDatas[] = $recordData; + $recordData = ''; + } // we are finished writing this string $finished = true; - } else { // 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. diff --git a/Classes/PHPExcel/Writer/Excel5/Worksheet.php b/Classes/PHPExcel/Writer/Excel5/Worksheet.php index 9586bd2..7f99e25 100644 --- a/Classes/PHPExcel/Writer/Excel5/Worksheet.php +++ b/Classes/PHPExcel/Writer/Excel5/Worksheet.php @@ -209,7 +209,7 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter parent::__construct(); // change BIFFwriter limit for CONTINUE records - $this->_limit = 8224; +// $this->_limit = 8224; $this->_preCalculateFormulas = $preCalculateFormulas; @@ -236,20 +236,22 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter $this->_outline_on = 1; // calculate values for DIMENSIONS record - $col = $row = array(); + $minR = $maxR = 1; + $minC = $maxC = '1A'; foreach ($this->_phpSheet->getCellCollection(false) as $cellID) { list($c,$r) = sscanf($cellID,'%[A-Z]%d'); - $row[$r] = $r; - $col[$c] = strlen($c).$c; + $minR = min($minR,$r); + $maxR = max($maxR,$r); + $C = strlen($c).$c; + $minC = min($minC,$C); + $maxC = max($maxC,$C); } // Determine lowest and highest column and row - $this->_firstRowIndex = (count($row) > 0) ? min($row) : 1; - $this->_lastRowIndex = (count($row) > 0) ? max($row) : 1; - if ($this->_firstRowIndex > 65535) $this->_firstRowIndex = 65535; - if ($this->_lastRowIndex > 65535) $this->_lastRowIndex = 65535; + $this->_firstRowIndex = ($minR > 65535) ? $minR: 65535; + $this->_lastRowIndex = ($maxR > 65535) ? $maxR : 65535; - $this->_firstColumnIndex = (count($col) > 0) ? PHPExcel_Cell::columnIndexFromString(substr(min($col),1)) : 1; - $this->_lastColumnIndex = (count($col) > 0) ? PHPExcel_Cell::columnIndexFromString(substr(max($col),1)) : 1; + $this->_firstColumnIndex = PHPExcel_Cell::columnIndexFromString(substr($minC,1)); + $this->_lastColumnIndex = PHPExcel_Cell::columnIndexFromString(substr($maxC,1)); if ($this->_firstColumnIndex > 255) $this->_firstColumnIndex = 255; if ($this->_lastColumnIndex > 255) $this->_lastColumnIndex = 255; @@ -266,7 +268,9 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter */ function close() { - $num_sheets = $this->_phpSheet->getParent()->getSheetCount(); + $_phpSheet = $this->_phpSheet; + + $num_sheets = $_phpSheet->getParent()->getSheetCount(); // Write BOF record $this->_storeBof(0x0010); @@ -281,21 +285,21 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter $this->_writeGridset(); // Calculate column widths - $this->_phpSheet->calculateColumnWidths(); + $_phpSheet->calculateColumnWidths(); // Column dimensions - $maxCol = PHPExcel_Cell::columnIndexFromString($this->_phpSheet->getHighestColumn()) -1; - $columnDimensions = $this->_phpSheet->getColumnDimensions(); + if (($defaultWidth = $_phpSheet->getDefaultColumnDimension()->getWidth()) < 0) { + $defaultWidth = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($_phpSheet->getParent()->getDefaultStyle()->getFont()); + } + + $columnDimensions = $_phpSheet->getColumnDimensions(); + $maxCol = $this->_lastColumnIndex -1; for ($i = 0; $i <= $maxCol; ++$i) { $hidden = 0; $level = 0; $xfIndex = 15; // there are 15 cell style Xfs - if ($this->_phpSheet->getDefaultColumnDimension()->getWidth() >= 0) { - $width = $this->_phpSheet->getDefaultColumnDimension()->getWidth(); - } else { - $width = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($this->_phpSheet->getParent()->getDefaultStyle()->getFont()); - } + $width = $defaultWidth; $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i); if (isset($columnDimensions[$columnLetter])) { @@ -384,55 +388,57 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter $this->_writeDimensions(); // Row dimensions - foreach ($this->_phpSheet->getRowDimensions() as $rowDimension) { + foreach ($_phpSheet->getRowDimensions() as $rowDimension) { $xfIndex = $rowDimension->getXfIndex() + 15; // there are 15 cellXfs $this->_writeRow( $rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), $xfIndex, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel() ); } // Write Cells - foreach ($this->_phpSheet->getCellCollection() as $cellID) { - $cell = $this->_phpSheet->getCell($cellID); + foreach ($_phpSheet->getCellCollection() as $cellID) { + $cell = $_phpSheet->getCell($cellID); $row = $cell->getRow() - 1; $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1; // 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; } // Write cell value $xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs - if ($cell->getValue() instanceof PHPExcel_RichText) { - $this->_writeString($row, $column, $cell->getValue()->getPlainText(), $xfIndex); + $cVal = $cell->getValue(); + if ($cVal instanceof PHPExcel_RichText) { + $this->_writeString($row, $column, $cVal->getPlainText(), $xfIndex); } else { 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: - if ($cell->getValue() === '' or $cell->getValue() === null) { - $this->_writeBlank($row, $column, $xfIndex); - } else { - $this->_writeString($row, $column, $cell->getValue(), $xfIndex); - } - break; + case PHPExcel_Cell_DataType::TYPE_NUMERIC: + $this->_writeNumber($row, $column, $cVal, $xfIndex); + break; - case PHPExcel_Cell_DataType::TYPE_FORMULA: - $calculatedValue = $this->_preCalculateFormulas ? - $cell->getCalculatedValue() : null; - $this->_writeFormula($row, $column, $cell->getValue(), $xfIndex, $calculatedValue); - break; + case PHPExcel_Cell_DataType::TYPE_FORMULA: + $calculatedValue = $this->_preCalculateFormulas ? + $cell->getCalculatedValue() : null; + $this->_writeFormula($row, $column, $cVal, $xfIndex, $calculatedValue); + break; - case PHPExcel_Cell_DataType::TYPE_BOOL: - $this->_writeBoolErr($row, $column, $cell->getValue(), 0, $xfIndex); - break; + case PHPExcel_Cell_DataType::TYPE_BOOL: + $this->_writeBoolErr($row, $column, $cVal, 0, $xfIndex); + break; - case PHPExcel_Cell_DataType::TYPE_ERROR: - $this->_writeBoolErr($row, $column, $this->_mapErrorCode($cell->getValue()), 1, $xfIndex); - break; + case PHPExcel_Cell_DataType::TYPE_ERROR: + $this->_writeBoolErr($row, $column, self::_mapErrorCode($cVal), 1, $xfIndex); + 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->_writeZoom(); - if ($this->_phpSheet->getFreezePane()) { + if ($_phpSheet->getFreezePane()) { $this->_writePanes(); } $this->_writeSelection(); $this->_writeMergedCells(); // Hyperlinks - foreach ($this->_phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) { + foreach ($_phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) { list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinate); $url = $hyperlink->getUrl(); @@ -789,7 +795,7 @@ class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter } elseif (is_string($calculatedValue)) { if (array_key_exists($calculatedValue, PHPExcel_Cell_DataType::getErrorCodes())) { // 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 === '') { // Empty string (and BIFF8) $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 */ - private function _mapErrorCode($errorCode) { + private static function _mapErrorCode($errorCode) { switch ($errorCode) { case '#NULL!': return 0x00; case '#DIV/0!': return 0x07; diff --git a/Classes/PHPExcel/Writer/Excel5/Xf.php b/Classes/PHPExcel/Writer/Excel5/Xf.php index 55d0b13..e87ef4a 100644 --- a/Classes/PHPExcel/Writer/Excel5/Xf.php +++ b/Classes/PHPExcel/Writer/Excel5/Xf.php @@ -174,38 +174,38 @@ class PHPExcel_Writer_Excel5_Xf if ($this->_isStyleXf) { $style = 0xFFF5; } else { - $style = $this->_mapLocked($this->_style->getProtection()->getLocked()); - $style |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1; + $style = self::_mapLocked($this->_style->getProtection()->getLocked()); + $style |= self::_mapHidden($this->_style->getProtection()->getHidden()) << 1; } // Flags to indicate if attributes have been set. $atr_num = ($this->_numberFormatIndex != 0)?1:0; $atr_fnt = ($this->_fontIndex != 0)?1:0; $atr_alc = ((int) $this->_style->getAlignment()->getWrapText())?1:0; - $atr_bdr = ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) || - $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) || - $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) || - $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0; + $atr_bdr = (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) || + self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) || + self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) || + self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0; $atr_pat = (($this->_fg_color != 0x40) || ($this->_bg_color != 0x41) || - $this->_mapFillType($this->_style->getFill()->getFillType()))?1:0; - $atr_prot = $this->_mapLocked($this->_style->getProtection()->getLocked()) - | $this->_mapHidden($this->_style->getProtection()->getHidden()); + self::_mapFillType($this->_style->getFill()->getFillType()))?1:0; + $atr_prot = self::_mapLocked($this->_style->getProtection()->getLocked()) + | self::_mapHidden($this->_style->getProtection()->getHidden()); // 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; } - if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) { + if (self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 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; } - if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) { + if (self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 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; } @@ -217,7 +217,7 @@ class PHPExcel_Writer_Excel5_Xf $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment $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; $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->_bg_color << 7; - $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color - $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4; - $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8; - $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12; + $border1 = self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color + $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4; + $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8; + $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12; $border1 |= $this->_left_color << 16; $border1 |= $this->_right_color << 23; @@ -248,8 +248,8 @@ class PHPExcel_Writer_Excel5_Xf $border2 = $this->_top_color; // Border color $border2 |= $this->_bottom_color << 7; $border2 |= $this->_diag_color << 14; - $border2 |= $this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21; - $border2 |= $this->_mapFillType($this->_style->getFill()->getFillType()) << 26; + $border2 |= self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21; + $border2 |= self::_mapFillType($this->_style->getFill()->getFillType()) << 26; $header = pack("vv", $record, $length); @@ -259,7 +259,7 @@ class PHPExcel_Writer_Excel5_Xf $data = pack("vvvC", $ifnt, $ifmt, $style, $align); $data .= pack("CCC" - , $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation()) + , self::_mapTextRotation($this->_style->getAlignment()->getTextRotation()) , $biff8_options , $used_attrib ); @@ -378,59 +378,69 @@ class PHPExcel_Writer_Excel5_Xf $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 */ - private function _mapBorderStyle($borderStyle) { - switch ($borderStyle) { - case PHPExcel_Style_Border::BORDER_NONE: return 0x00; - case PHPExcel_Style_Border::BORDER_THIN; return 0x01; - 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 function _mapBorderStyle($borderStyle) { + if (isset(self::$_mapBorderStyle[$borderStyle])) + return self::$_mapBorderStyle[$borderStyle]; + 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 */ - private function _mapFillType($fillType) { - switch ($fillType) { - case PHPExcel_Style_Fill::FILL_NONE: return 0x00; - case PHPExcel_Style_Fill::FILL_SOLID: return 0x01; - 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 function _mapFillType($fillType) { + if (isset(self::$_mapFillType[$fillType])) + return self::$_mapFillType[$fillType]; + 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 * @@ -439,31 +449,26 @@ class PHPExcel_Writer_Excel5_Xf */ private function _mapHAlign($hAlign) { - switch ($hAlign) { - case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL: return 0; - case PHPExcel_Style_Alignment::HORIZONTAL_LEFT: return 1; - 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; - } + if (isset(self::$_mapHAlign[$hAlign])) + return self::$_mapHAlign[$hAlign]; + 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 * * @param string $vAlign * @return int */ - private function _mapVAlign($vAlign) { - switch ($vAlign) { - case PHPExcel_Style_Alignment::VERTICAL_TOP: return 0; - case PHPExcel_Style_Alignment::VERTICAL_CENTER: return 1; - case PHPExcel_Style_Alignment::VERTICAL_BOTTOM: return 2; - case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY: return 3; - default: return 2; - } + private static function _mapVAlign($vAlign) { + if (isset(self::$_mapVAlign[$vAlign])) + return self::$_mapVAlign[$vAlign]; + return 2; } /** @@ -472,7 +477,7 @@ class PHPExcel_Writer_Excel5_Xf * @param int $textRotation * @return int */ - private function _mapTextRotation($textRotation) { + private static function _mapTextRotation($textRotation) { if ($textRotation >= 0) { return $textRotation; } @@ -490,7 +495,7 @@ class PHPExcel_Writer_Excel5_Xf * @param string * @return int */ - private function _mapLocked($locked) { + private static function _mapLocked($locked) { switch ($locked) { case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 1; case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1; @@ -505,7 +510,7 @@ class PHPExcel_Writer_Excel5_Xf * @param string * @return int */ - private function _mapHidden($hidden) { + private static function _mapHidden($hidden) { switch ($hidden) { case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 0; case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;