From 46a5fd4c1369eb4c1973be5c7bd5390f9a8eafe1 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 12 Jul 2013 22:19:40 +0100 Subject: [PATCH] Merge changes from the 1.7.9 develop branch since 1.7.9 release --- Classes/PHPExcel/Calculation/LookupRef.php | 12 ++-- Classes/PHPExcel/Cell.php | 2 +- Classes/PHPExcel/Reader/Excel2007.php | 11 ++++ Classes/PHPExcel/Reader/Excel5.php | 9 ++- Classes/PHPExcel/Style.php | 57 ++++++++++++++++++- Classes/PHPExcel/Style/Alignment.php | 19 ++++--- Classes/PHPExcel/Writer/Excel2007.php | 47 +++++++++++----- Classes/PHPExcel/Writer/Excel2007/Style.php | 3 + Classes/PHPExcel/Writer/Excel5/Xf.php | 62 +++++++++++---------- Examples/06largescale-xls.php | 13 ++--- 10 files changed, 168 insertions(+), 67 deletions(-) diff --git a/Classes/PHPExcel/Calculation/LookupRef.php b/Classes/PHPExcel/Calculation/LookupRef.php index 8d61f87..5de0cb7 100644 --- a/Classes/PHPExcel/Calculation/LookupRef.php +++ b/Classes/PHPExcel/Calculation/LookupRef.php @@ -307,9 +307,9 @@ class Calculation_LookupRef { if (strpos($cellAddress,'!') !== false) { list($sheetName, $cellAddress) = explode('!',$cellAddress); $sheetName = trim($sheetName, "'"); - $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName); + $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName); } else { - $pSheet = $pCell->getParent(); + $pSheet = $pCell->getWorksheet(); } return Calculation::getInstance()->extractNamedRange($cellAddress, $pSheet, false); @@ -318,9 +318,9 @@ class Calculation_LookupRef { if (strpos($cellAddress,'!') !== false) { list($sheetName,$cellAddress) = explode('!',$cellAddress); $sheetName = trim($sheetName, "'"); - $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName); + $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName); } else { - $pSheet = $pCell->getParent(); + $pSheet = $pCell->getWorksheet(); } return Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, false); @@ -412,9 +412,9 @@ class Calculation_LookupRef { } if ($sheetName !== null) { - $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName); + $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName); } else { - $pSheet = $pCell->getParent(); + $pSheet = $pCell->getWorksheet(); } return Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, False); diff --git a/Classes/PHPExcel/Cell.php b/Classes/PHPExcel/Cell.php index 8fb2ebb..aa35f12 100644 --- a/Classes/PHPExcel/Cell.php +++ b/Classes/PHPExcel/Cell.php @@ -471,7 +471,7 @@ class Cell /** * Get parent worksheet * - * @return PHPExcel\Worksheet + * @return PHPExcel\CachedObjectStorage_CacheBase */ public function getParent() { return $this->_parent; diff --git a/Classes/PHPExcel/Reader/Excel2007.php b/Classes/PHPExcel/Reader/Excel2007.php index aea1720..7905dc3 100644 --- a/Classes/PHPExcel/Reader/Excel2007.php +++ b/Classes/PHPExcel/Reader/Excel2007.php @@ -492,6 +492,10 @@ class Reader_Excel2007 extends Reader_Abstract implements Reader_IReader //$numFmt = str_replace('mm', 'i', $numFmt); //$numFmt = str_replace('h', 'H', $numFmt); + $quotePrefix = false; + if (isset($xf["quotePrefix"])) { + $quotePrefix = (boolean) $xf["quotePrefix"]; + } $style = (object) array( "numFmt" => $numFmt, "font" => $xmlStyles->fonts->font[intval($xf["fontId"])], @@ -499,6 +503,7 @@ class Reader_Excel2007 extends Reader_Abstract implements Reader_IReader "border" => $xmlStyles->borders->border[intval($xf["borderId"])], "alignment" => $xf->alignment, "protection" => $xf->protection, + "quotePrefix" => $quotePrefix, ); $styles[] = $style; @@ -526,6 +531,7 @@ class Reader_Excel2007 extends Reader_Abstract implements Reader_IReader "border" => $xmlStyles->borders->border[intval($xf["borderId"])], "alignment" => $xf->alignment, "protection" => $xf->protection, + "quotePrefix" => $quotePrefix, ); $cellStyles[] = $cellStyle; @@ -1848,6 +1854,11 @@ class Reader_Excel2007 extends Reader_Abstract implements Reader_IReader $docStyle->getProtection()->setHidden(Style_Protection::PROTECTION_UNPROTECTED); } } + } + + // top-level style settings + if (isset($style->quotePrefix)) { + $docStyle->setQuotePrefix($style->quotePrefix); } } diff --git a/Classes/PHPExcel/Reader/Excel5.php b/Classes/PHPExcel/Reader/Excel5.php index feb7e46..f697294 100644 --- a/Classes/PHPExcel/Reader/Excel5.php +++ b/Classes/PHPExcel/Reader/Excel5.php @@ -1562,11 +1562,15 @@ class Reader_Excel5 extends Reader_Abstract implements Reader_IReader private function _readFilepass() { $length = self::_GetInt2d($this->_data, $this->_pos + 2); -// $recordData = substr($this->_data, $this->_pos + 4, $length); + $recordData = substr($this->_data, $this->_pos + 4, $length); // move stream pointer to next record $this->_pos += 4 + $length; + if (!$this->_readDataOnly) { + // offset: 0; size: 2; 16-bit hash value of password + $password = strtoupper(dechex(self::_GetInt2d($recordData, 0))); // the hashed password + } throw new Reader_Exception('Cannot read encrypted file'); } @@ -1829,6 +1833,9 @@ class Reader_Excel5 extends Reader_Abstract implements Reader_IReader case 3: $objStyle->getAlignment()->setHorizontal(Style_Alignment::HORIZONTAL_RIGHT); break; + case 4: + $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_FILL); + break; case 5: $objStyle->getAlignment()->setHorizontal(Style_Alignment::HORIZONTAL_JUSTIFY); break; diff --git a/Classes/PHPExcel/Style.php b/Classes/PHPExcel/Style.php index eb17e4e..482aafc 100644 --- a/Classes/PHPExcel/Style.php +++ b/Classes/PHPExcel/Style.php @@ -86,6 +86,13 @@ class Style extends Style_Supervisor implements IComparable */ protected $_protection; + /** + * Use Quote Prefix when displaying in cell editor. Only used for real style. + * + * @var boolean + */ + protected $_quotePrefix = false; + /** * Index of style in collection. Only used for real style. * @@ -158,6 +165,17 @@ class Style extends Style_Supervisor implements IComparable return $this->_parent; } + /** + * Build style array from subcomponents + * + * @param array $array + * @return array + */ + public function getStyleArray($array) + { + return array('quotePrefix' => $array); + } + /** * Apply styles from array * @@ -187,7 +205,8 @@ class Style extends Style_Supervisor implements IComparable * 'rgb' => '808080' * ) * ) - * ) + * ), + * 'quotePrefix' => true * ) * ); * @@ -465,6 +484,9 @@ class Style extends Style_Supervisor implements IComparable if (array_key_exists('protection', $pStyles)) { $this->getProtection()->applyFromArray($pStyles['protection']); } + if (array_key_exists('quotePrefix', $pStyles)) { + $this->_quotePrefix = $pStyles['quotePrefix']; + } } } else { throw new Exception("Invalid style array passed."); @@ -568,6 +590,38 @@ class Style extends Style_Supervisor implements IComparable return $this->_protection; } + /** + * Get quote prefix + * + * @return boolean + */ + public function getQuotePrefix() + { + if ($this->_isSupervisor) { + return $this->getSharedComponent()->getQuotePrefix(); + } + return $this->_quotePrefix; + } + + /** + * Set quote prefix + * + * @param boolean $pValue + */ + public function setQuotePrefix($pValue) + { + if ($pValue == '') { + $pValue = false; + } + if ($this->_isSupervisor) { + $styleArray = array('quotePrefix' => $pValue); + $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); + } else { + $this->_quotePrefix = (boolean) $pValue; + } + return $this; + } + /** * Get hash code * @@ -588,6 +642,7 @@ class Style extends Style_Supervisor implements IComparable . $this->_numberFormat->getHashCode() . $hashConditionals . $this->_protection->getHashCode() + . ($this->_quotePrefix ? 't' : 'f') . __CLASS__ ); } diff --git a/Classes/PHPExcel/Style/Alignment.php b/Classes/PHPExcel/Style/Alignment.php index 1a0f003..77dda40 100644 --- a/Classes/PHPExcel/Style/Alignment.php +++ b/Classes/PHPExcel/Style/Alignment.php @@ -39,17 +39,20 @@ class Style_Alignment extends Style_Supervisor implements IComparable { /* Horizontal alignment styles */ const HORIZONTAL_GENERAL = 'general'; - const HORIZONTAL_LEFT = 'left'; - const HORIZONTAL_RIGHT = 'right'; - const HORIZONTAL_CENTER = 'center'; - const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'; + const HORIZONTAL_LEFT = 'left'; + const HORIZONTAL_RIGHT = 'right'; + const HORIZONTAL_CENTER = 'center'; + const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'; const HORIZONTAL_JUSTIFY = 'justify'; + const HORIZONTAL_FILL = 'fill'; + const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only /* Vertical alignment styles */ - const VERTICAL_BOTTOM = 'bottom'; - const VERTICAL_TOP = 'top'; - const VERTICAL_CENTER = 'center'; - const VERTICAL_JUSTIFY = 'justify'; + const VERTICAL_BOTTOM = 'bottom'; + const VERTICAL_TOP = 'top'; + const VERTICAL_CENTER = 'center'; + const VERTICAL_JUSTIFY = 'justify'; + const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only /** * Horizontal diff --git a/Classes/PHPExcel/Writer/Excel2007.php b/Classes/PHPExcel/Writer/Excel2007.php index 8dd902d..579cf71 100644 --- a/Classes/PHPExcel/Writer/Excel2007.php +++ b/Classes/PHPExcel/Writer/Excel2007.php @@ -65,6 +65,13 @@ class Writer_Excel2007 extends Writer_Abstract implements Writer_IWriter */ private $_stringTable = array(); + /** + * Private unique PHPExcel_Style HashTable + * + * @var PHPExcel_HashTable + */ + private $_styleHashTable; + /** * Private unique PHPExcel\Style_Conditional HashTable * @@ -117,18 +124,19 @@ class Writer_Excel2007 extends Writer_Abstract implements Writer_IWriter // Assign PHPExcel $this->setPHPExcel($pPHPExcel); - $writerPartsArray = array( 'stringtable' => __NAMESPACE__ . '\Writer_Excel2007_StringTable', - 'contenttypes' => __NAMESPACE__ . '\Writer_Excel2007_ContentTypes', - 'docprops' => __NAMESPACE__ . '\Writer_Excel2007_DocProps', - 'rels' => __NAMESPACE__ . '\Writer_Excel2007_Rels', - 'theme' => __NAMESPACE__ . '\Writer_Excel2007_Theme', - 'style' => __NAMESPACE__ . '\Writer_Excel2007_Style', - 'workbook' => __NAMESPACE__ . '\Writer_Excel2007_Workbook', - 'worksheet' => __NAMESPACE__ . '\Writer_Excel2007_Worksheet', - 'drawing' => __NAMESPACE__ . '\Writer_Excel2007_Drawing', - 'comments' => __NAMESPACE__ . '\Writer_Excel2007_Comments', - 'chart' => __NAMESPACE__ . '\Writer_Excel2007_Chart', - ); + $writerPartsArray = array( + 'stringtable' => __NAMESPACE__ . '\Writer_Excel2007_StringTable', + 'contenttypes' => __NAMESPACE__ . '\Writer_Excel2007_ContentTypes', + 'docprops' => __NAMESPACE__ . '\Writer_Excel2007_DocProps', + 'rels' => __NAMESPACE__ . '\Writer_Excel2007_Rels', + 'theme' => __NAMESPACE__ . '\Writer_Excel2007_Theme', + 'style' => __NAMESPACE__ . '\Writer_Excel2007_Style', + 'workbook' => __NAMESPACE__ . '\Writer_Excel2007_Workbook', + 'worksheet' => __NAMESPACE__ . '\Writer_Excel2007_Worksheet', + 'drawing' => __NAMESPACE__ . '\Writer_Excel2007_Drawing', + 'comments' => __NAMESPACE__ . '\Writer_Excel2007_Comments', + 'chart' => __NAMESPACE__ . '\Writer_Excel2007_Chart', + ); // Initialise writer parts // and Assign their parent IWriters @@ -136,8 +144,9 @@ class Writer_Excel2007 extends Writer_Abstract implements Writer_IWriter $this->_writerParts[$writer] = new $class($this); } - $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable', - '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable' + $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable', + '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable', + '_styleHashTable' ); // Set HashTable variables @@ -193,6 +202,7 @@ class Writer_Excel2007 extends Writer_Abstract implements Writer_IWriter } // Create styles dictionaries + $this->_styleHashTable->addFromSource( $this->getWriterPart('Style')->allStyles($this->_spreadSheet) ); $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) ); $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) ); $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) ); @@ -397,6 +407,15 @@ class Writer_Excel2007 extends Writer_Abstract implements Writer_IWriter return $this->_stringTable; } + /** + * Get PHPExcel_Style HashTable + * + * @return PHPExcel_HashTable + */ + public function getStyleHashTable() { + return $this->_styleHashTable; + } + /** * Get PHPExcel\Style_Conditional HashTable * diff --git a/Classes/PHPExcel/Writer/Excel2007/Style.php b/Classes/PHPExcel/Writer/Excel2007/Style.php index f79e81a..14e6054 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Style.php +++ b/Classes/PHPExcel/Writer/Excel2007/Style.php @@ -400,6 +400,9 @@ class Writer_Excel2007_Style extends Writer_Excel2007_WriterPart $objWriter->writeAttribute('xfId', 0); $objWriter->writeAttribute('fontId', (int)$this->getParentWriter()->getFontHashTable()->getIndexForHashCode($pStyle->getFont()->getHashCode())); + if ($pStyle->getQuotePrefix()) { + $objWriter->writeAttribute('quotePrefix', 1); + } if ($pStyle->getNumberFormat()->getBuiltInFormatCode() === false) { $objWriter->writeAttribute('numFmtId', (int)($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164) ); } else { diff --git a/Classes/PHPExcel/Writer/Excel5/Xf.php b/Classes/PHPExcel/Writer/Excel5/Xf.php index 6f00327..b6627bf 100644 --- a/Classes/PHPExcel/Writer/Excel5/Xf.php +++ b/Classes/PHPExcel/Writer/Excel5/Xf.php @@ -416,28 +416,29 @@ class Writer_Excel5_Xf * @static array of int * */ - private static $_mapFillType = array( Style_Fill::FILL_NONE => 0x00, - Style_Fill::FILL_SOLID => 0x01, - Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02, - Style_Fill::FILL_PATTERN_DARKGRAY => 0x03, - Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04, - Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05, - Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06, - Style_Fill::FILL_PATTERN_DARKDOWN => 0x07, - Style_Fill::FILL_PATTERN_DARKUP => 0x08, - Style_Fill::FILL_PATTERN_DARKGRID => 0x09, - Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A, - Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B, - Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C, - Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D, - Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E, - Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F, - Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10, - Style_Fill::FILL_PATTERN_GRAY125 => 0x11, - Style_Fill::FILL_PATTERN_GRAY0625 => 0x12, - Style_Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8 - Style_Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8 - ); + private static $_mapFillType = array( + Style_Fill::FILL_NONE => 0x00, + Style_Fill::FILL_SOLID => 0x01, + Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02, + Style_Fill::FILL_PATTERN_DARKGRAY => 0x03, + Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04, + Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05, + Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06, + Style_Fill::FILL_PATTERN_DARKDOWN => 0x07, + Style_Fill::FILL_PATTERN_DARKUP => 0x08, + Style_Fill::FILL_PATTERN_DARKGRID => 0x09, + Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A, + Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B, + Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C, + Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D, + Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E, + Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F, + Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10, + Style_Fill::FILL_PATTERN_GRAY125 => 0x11, + Style_Fill::FILL_PATTERN_GRAY0625 => 0x12, + Style_Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8 + Style_Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8 + ); /** * Map fill type * @@ -455,13 +456,16 @@ class Writer_Excel5_Xf * @static array of int * */ - private static $_mapHAlign = array( Style_Alignment::HORIZONTAL_GENERAL => 0, - Style_Alignment::HORIZONTAL_LEFT => 1, - Style_Alignment::HORIZONTAL_CENTER => 2, - Style_Alignment::HORIZONTAL_RIGHT => 3, - Style_Alignment::HORIZONTAL_JUSTIFY => 5, - Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6, - ); + private static $_mapHAlign = array( + Style_Alignment::HORIZONTAL_GENERAL => 0, + Style_Alignment::HORIZONTAL_LEFT => 1, + Style_Alignment::HORIZONTAL_CENTER => 2, + Style_Alignment::HORIZONTAL_RIGHT => 3, + Style_Alignment::HORIZONTAL_FILL => 4, + Style_Alignment::HORIZONTAL_JUSTIFY => 5, + Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6, + ); + /** * Map to BIFF2-BIFF8 codes for horizontal alignment * diff --git a/Examples/06largescale-xls.php b/Examples/06largescale-xls.php index b5b8069..5c39e78 100644 --- a/Examples/06largescale-xls.php +++ b/Examples/06largescale-xls.php @@ -34,9 +34,8 @@ define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '
'); date_default_timezone_set('Europe/London'); -/** Include PHPExcel */ -require_once '../Classes/PHPExcel.php'; - +/** Include PHPExcel Bootstrap */ +require_once '../Classes/Bootstrap.php'; /* After doing some test, I've got these results benchmarked @@ -52,9 +51,9 @@ for writing to Excel2007: 15000 465 */ -// Create new PHPExcel object -echo date('H:i:s') , " Create new PHPExcel object" , EOL; -$objPHPExcel = new PHPExcel(); +// Create new PHPExcel Workbook object +echo date('H:i:s') , " Create new PHPExcel Workbook object" , EOL; +$objPHPExcel = new PHPExcel\Workbook(); // Set document properties echo date('H:i:s') , " Set properties" , EOL; @@ -117,7 +116,7 @@ $objPHPExcel->setActiveSheetIndex(0); echo date('H:i:s') , " Write to Excel5 format" , EOL; $callStartTime = microtime(true); -$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); +$objWriter = PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save(str_replace('.php', '.xls', __FILE__)); $callEndTime = microtime(true); $callTime = $callEndTime - $callStartTime;