Andy-H Posted February 6, 2012 Share Posted February 6, 2012 I have a class that wraps the functionality I need from PHPExcel for a project, here is a function for setting values ans styles: public function setValues() { $values = func_get_args(); $width = count($values); $cells = array_slice($this->_cells, 0, $width); $row = $this->_currentRow[$this->_currentSheet]; $this->setCurrentRowHeight(18); while ( count($values) ) { $val = array_shift($values); $cell = array_shift($cells) . $row; $this->_sheet()->getStyle($cell)->getAlignment()->setIndent(1); $value = explode('~', $val); $value = array_filter($value); $this->_sheet()->setCellValue($cell, $value[0]); if ( isset($value[1]) ) $this->_sheet()->getStyle($cell)->getFont()->getColor()->setRGB($value[1]); if ( isset($value[2]) ) { $this->_sheet()->getStyle($cell)->getFill()->getStartColor()->setRGB($value[2]); $this->_sheet()->getStyle($cell)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); } if ( isset($value[3]) ) { $align = PHPExcel_Style_Alignment::HORIZONTAL_LEFT; if ( $value[3] == 'right' ) $align = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT; if ( $value[3] == 'center' ) $align = PHPExcel_Style_Alignment::HORIZONTAL_CENTER; $this->_sheet()->getStyle($cell)->getAlignment()->setHorizontal($align); } } $this->_nextRow(); return $this; } With this code my script takes about 15 seconds to execute, but when I comment out: public function setValues() { $values = func_get_args(); $width = count($values); $cells = array_slice($this->_cells, 0, $width); $row = $this->_currentRow[$this->_currentSheet]; $this->setCurrentRowHeight(18); while ( count($values) ) { $val = array_shift($values); $cell = array_shift($cells) . $row; $this->_sheet()->getStyle($cell)->getAlignment()->setIndent(1); $value = explode('~', $val); $value = array_filter($value); $this->_sheet()->setCellValue($cell, $value[0]); /*if ( isset($value[1]) ) $this->_sheet()->getStyle($cell)->getFont()->getColor()->setRGB($value[1]); if ( isset($value[2]) ) { $this->_sheet()->getStyle($cell)->getFill()->getStartColor()->setRGB($value[2]); $this->_sheet()->getStyle($cell)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); } if ( isset($value[3]) ) { $align = PHPExcel_Style_Alignment::HORIZONTAL_LEFT; if ( $value[3] == 'right' ) $align = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT; if ( $value[3] == 'center' ) $align = PHPExcel_Style_Alignment::HORIZONTAL_CENTER; $this->_sheet()->getStyle($cell)->getAlignment()->setHorizontal($align); }*/ } $this->_nextRow(); return $this; } It takes 8 seconds, does anyone know of a faster way to style cells using PHPExcel or can think of a better way to do this? Link to comment https://forums.phpfreaks.com/topic/256541-phpexcel-setvalues-slow/ Share on other sites More sharing options...
Andy-H Posted February 6, 2012 Author Share Posted February 6, 2012 Used applyFromArray() Link to comment https://forums.phpfreaks.com/topic/256541-phpexcel-setvalues-slow/#findComment-1315171 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.