Jump to content

PHPExcel setvalues slow


Andy-H

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.