From 4381f9740c0e3218882308604c6516b9c211b665 Mon Sep 17 00:00:00 2001 From: Andrey Mironov Date: Wed, 14 Nov 2012 16:42:03 +0600 Subject: [PATCH 1/3] Add duplicateStyle example --- Examples/40duplicateStyle.php | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Examples/40duplicateStyle.php diff --git a/Examples/40duplicateStyle.php b/Examples/40duplicateStyle.php new file mode 100644 index 00000000..52891ad4 --- /dev/null +++ b/Examples/40duplicateStyle.php @@ -0,0 +1,51 @@ +'); + +date_default_timezone_set('Europe/London'); + +/** Include PHPExcel */ +require_once '../Classes/PHPExcel.php'; + +echo date('H:i:s') , " Create new PHPExcel object" , EOL; +$objPHPExcel = new PHPExcel(); +$worksheet = $objPHPExcel->getActiveSheet(); + +echo date('H:i:s') , " Create styles array" , EOL; +$styles = array(); +for ($i = 0; $i < 10; $i++) { + $style = new PHPExcel_Style(); + $style->getFont()->setSize($i + 4); + $styles[] = $style; +} + +echo date('H:i:s') , " Add data (begin)" , EOL; +$t = microtime(true); +for ($col = 0; $col < 50; $col++) { + for ($row = 0; $row < 100; $row++) { + $str = ($row + $col); + $style = $styles[$row % 10]; + $coord = PHPExcel_Cell::stringFromColumnIndex($col) . ($row + 1); + $worksheet->setCellValue($coord, $str); + $worksheet->duplicateStyle($style, $coord); + } +} +$d = microtime(true) - $t; +echo date('H:i:s') , " Add data (end), time: " . round($d, 2) . " s", EOL; + + +echo date('H:i:s') , " Write to Excel2007 format" , EOL; +$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); +$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); +echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; + + +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; + +echo date('H:i:s') , " Done writing file" , EOL; +echo 'File has been created in ' , getcwd() , EOL; From db2a7cbabfc42dabb45ef74481eb37f68da02163 Mon Sep 17 00:00:00 2001 From: Andrey Mironov Date: Tue, 30 Oct 2012 16:46:18 +0600 Subject: [PATCH 2/3] Search style by identity in PHPExcel_Worksheet::duplicateStyle() --- Classes/PHPExcel.php | 16 ++++++++++++++++ Classes/PHPExcel/Worksheet.php | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Classes/PHPExcel.php b/Classes/PHPExcel.php index cc70286d..f60a15cf 100644 --- a/Classes/PHPExcel.php +++ b/Classes/PHPExcel.php @@ -607,6 +607,22 @@ class PHPExcel return false; } + /** + * Check if style exists in style collection + * + * @param PHPExcel_Style $style + * @return boolean + */ + public function cellXfExists($pCellStyle = null) + { + foreach ($this->_cellXfCollection as $cellXf) { + if ($cellXf === $pCellStyle) { + return true; + } + } + return false; + } + /** * Get default style * diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index bc58a696..4ffd94f3 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -1454,9 +1454,9 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // Add the style to the workbook if necessary $workbook = $this->_parent; - if ($existingStyle = $this->_parent->getCellXfByHashCode($pCellStyle->getHashCode())) { - // there is already such cell Xf in our collection - $xfIndex = $existingStyle->getIndex(); + if ($this->_parent->cellXfExists($pCellStyle)) { + // there is already this cell Xf in our collection + $xfIndex = $pCellStyle->getIndex(); } else { // we don't have such a cell Xf, need to add $workbook->addCellXf($pCellStyle); From 7884495d5d4df892f7d74dfe3c3e903964680d61 Mon Sep 17 00:00:00 2001 From: Andrey Mironov Date: Tue, 30 Oct 2012 16:46:23 +0600 Subject: [PATCH 3/3] Use in_array in PHPExcel::cellXfExists() --- Classes/PHPExcel.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Classes/PHPExcel.php b/Classes/PHPExcel.php index f60a15cf..ab3396ba 100644 --- a/Classes/PHPExcel.php +++ b/Classes/PHPExcel.php @@ -615,12 +615,7 @@ class PHPExcel */ public function cellXfExists($pCellStyle = null) { - foreach ($this->_cellXfCollection as $cellXf) { - if ($cellXf === $pCellStyle) { - return true; - } - } - return false; + return in_array($pCellStyle, $this->_cellXfCollection, true); } /**