title: PHPExcel memory exhausted
body: <p>Im using PHPExcell to export 4000 rows with 20 columns. I wanted to color every second row in sheet, then i wrote a loop, but it returns me</p>
  
  <p><code>FAtal Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in D:\Data\xampp\htdocs\Classes\PHPExcel\Style\Supervisor.php</code></p>
  
  <p>I've been searching 2 hours to resolve problem but I failed. Every topic i found is pretty old, 2010 or earlier. I've tried to use caching, something liek this</p>
  
  <pre><code>$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
  $cacheSettings = array( ' memoryCacheSize ' =&gt; '8MB');
  PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);   
  </code></pre>
  
  <p>But that doesn't help. Any idea how can i fix that problem?</p>
  
  <p>And at the end my loop i use to generate xlsx and format cells:</p>
  
  <pre><code>$i = 2;
      foreach($table as $row){
      $objPHPExcel-&gt;getActiveSheet()
                  -&gt;setCellValue('A'.$i, $row[1])
                  -&gt;setCellValue('B'.$i, $row[2])
                  -&gt;setCellValue('C'.$i, $row[3])
                  -&gt;setCellValue('D'.$i, $row[4])
                  -&gt;setCellValue('E'.$i, $row[5])
                  -&gt;setCellValue('F'.$i, $row[6])
                  -&gt;setCellValue('G'.$i, $row[7])
                  -&gt;setCellValue('H'.$i, $row[8])
                  -&gt;setCellValue('I'.$i, $row[9])
                  -&gt;setCellValue('J'.$i, $row[10])
                  -&gt;setCellValue('K'.$i, $row[11])
                  -&gt;setCellValue('L'.$i, $row[12])
                  -&gt;setCellValue('M'.$i, $row[13])
                  -&gt;setCellValue('N'.$i, $row[14])
                  -&gt;setCellValue('O'.$i, $row[15])
                  -&gt;setCellValue('P'.$i, $row[16])
                  -&gt;setCellValue('Q'.$i, $row[17])
                  -&gt;setCellValue('R'.$i, $row[20])
                  -&gt;setCellValue('S'.$i, $row[21])
                  -&gt;setCellValue('T'.$i, $row[22]);
          // Color every second row
      if($i%2!=0){
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A'.$i.':T'.$i)-&gt;getFill()-&gt;applyFromArray(
      array(
      'type'       =&gt; PHPExcel_Style_Fill::FILL_SOLID,
      'startcolor' =&gt; array('rgb' =&gt; 'e8f4ff')
      ));
      }
  
      //color column "O"
      if($i%2==0){
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('O'.$i)-&gt;getFill()-&gt;applyFromArray(
      array(
      'type'       =&gt; PHPExcel_Style_Fill::FILL_SOLID,
      'startcolor' =&gt; array('rgb' =&gt; 'fffacd')
      ));
      }
      else{
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('O'.$i)-&gt;getFill()-&gt;applyFromArray(
      array(
      'type'       =&gt; PHPExcel_Style_Fill::FILL_SOLID,
      'startcolor' =&gt; array('rgb' =&gt; 'fff9bd')
      ));
      }
  
      // color column "N"
      if($i%2==0){
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('N'.$i)-&gt;getFill()-&gt;applyFromArray(
      array(
      'type'       =&gt; PHPExcel_Style_Fill::FILL_SOLID,
      'startcolor' =&gt; array('rgb' =&gt; 'eaffee')
      ));
      }
      else{
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('N'.$i)-&gt;getFill()-&gt;applyFromArray(
      array(
      'type'       =&gt; PHPExcel_Style_Fill::FILL_SOLID,
      'startcolor' =&gt; array('rgb' =&gt; 'ccffcc')
      ));
      }           
  
  
      $i++;
      }
  </code></pre>
  
  <p>EDIT:: I post full code coz i have no idea what more i can do. I create new phpexcel object and then import empty template only with 1st row as header with colnames. rest cells are totaly empty with preset style. I set style for 10 000 rows. But it still returns me same error. There's code:</p>
  
  <pre><code>            error_reporting(E_ALL);
      ini_set('display_errors', TRUE);
      ini_set('display_startup_errors', TRUE);
      date_default_timezone_set('Europe/London');
  
      if (PHP_SAPI == 'cli')
          die('This example should only be run from a Web Browser');
  
      /** Include PHPExcel */
      require_once '../Classes/PHPExcel.php';
  
  
  
      // Create new PHPExcel object
      $objPHPExcel = new PHPExcel();
  
      $objReader = PHPExcel_IOFactory::createReader('Excel2007');
      $objReader = $objReader-&gt;load('components/com_uhlist/Classes/template.xlsx');
      $template = $objReader-&gt;getActiveSheet();
  
      $objPHPExcel-&gt;addExternalSheet($template);
      $objPHPExcel-&gt;setActiveSheetIndex(1);
  
      $user = JFactory::getUser();
      $username = $user-&gt;get('username');
      // Set document properties
      $objPHPExcel-&gt;getProperties()-&gt;setCreator($username)
                                   -&gt;setLastModifiedBy($username)
                                   -&gt;setTitle("User &amp; Host List");
  
  
  
      // Add some data
      // Aktywowanie arkusza
      //$objPHPExcel-&gt;setActiveSheetIndex(-0);
  
  
      // Export danych
      $db = JFactory::getDBO();
      $query = 'SELECT * FROM '.$db-&gt;getPrefix().'users_n_hosts';
      $db-&gt;setQuery($query);
      $table = $db-&gt;loadRowList();
  
      $i = 2;
      foreach($table as $row){
      $objPHPExcel-&gt;getActiveSheet()
                  -&gt;setCellValue('A'.$i, $row[1])
                  -&gt;setCellValue('B'.$i, $row[2])
                  -&gt;setCellValue('C'.$i, $row[3])
                  -&gt;setCellValue('D'.$i, $row[4])
                  -&gt;setCellValue('E'.$i, $row[5])
                  -&gt;setCellValue('F'.$i, $row[6])
                  -&gt;setCellValue('G'.$i, $row[7])
                  -&gt;setCellValue('H'.$i, $row[8])
                  -&gt;setCellValue('I'.$i, $row[9])
                  -&gt;setCellValue('J'.$i, $row[10])
                  -&gt;setCellValue('K'.$i, $row[11])
                  -&gt;setCellValue('L'.$i, $row[12])
                  -&gt;setCellValue('M'.$i, $row[13])
                  -&gt;setCellValue('N'.$i, $row[14])
                  -&gt;setCellValue('O'.$i, $row[15])
                  -&gt;setCellValue('P'.$i, $row[16])
                  -&gt;setCellValue('Q'.$i, $row[17])
                  -&gt;setCellValue('R'.$i, $row[20])
                  -&gt;setCellValue('S'.$i, $row[21])
                  -&gt;setCellValue('T'.$i, $row[22]);       
      $i++;
      }
  
  
  
      // Nazwa arkusza
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('User &amp; Host');
  
  
      // Set active sheet index to the first sheet, so Excel opens this as the first sheet
      $objPHPExcel-&gt;setActiveSheetIndex(0);
  
  
      // Redirect output to a client’s web browser (Excel2007)
      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      header('Content-Disposition: attachment;filename="User&amp;Host_'.date('d-m-Y').'.xlsx"');
      header('Cache-Control: max-age=0');
  
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      $objWriter-&gt;save('php://output');
      exit;
  
      }
  </code></pre>
  
  <p>Newest Code:: Insert data into template:</p>
  
  <pre><code>            $objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
      $objPHPExcel = $objPHPExcel-&gt;load('components/com_uhlist/Classes/template.xlsx');
      $objPHPExcel-&gt;setActiveSheetIndex(0);
  
      $user = JFactory::getUser();
      $username = $user-&gt;get('username');
      // Set document properties
      $objPHPExcel-&gt;getProperties()-&gt;setCreator($username)
                                   -&gt;setLastModifiedBy($username)
                                   -&gt;setTitle("User &amp; Host List");
  
      // Export danych
      $db = JFactory::getDBO();
      $query = 'SELECT * FROM '.$db-&gt;getPrefix().'autocad_users_n_hosts';
      $db-&gt;setQuery($query);
      $table = $db-&gt;loadRowList();
  
      $i = 2;
      foreach($table as $row){
      $objPHPExcel-&gt;getActiveSheet()
                  -&gt;setCellValue('A'.$i, $row[1])
                  -&gt;setCellValue('B'.$i, $row[2])
                  -&gt;setCellValue('C'.$i, $row[3])
                  -&gt;setCellValue('D'.$i, $row[4])
                  -&gt;setCellValue('E'.$i, $row[5])
                  -&gt;setCellValue('F'.$i, $row[6])
                  -&gt;setCellValue('G'.$i, $row[7])
                  -&gt;setCellValue('H'.$i, $row[8])
                  -&gt;setCellValue('I'.$i, $row[9])
                  -&gt;setCellValue('J'.$i, $row[10])
                  -&gt;setCellValue('K'.$i, $row[11])
                  -&gt;setCellValue('L'.$i, $row[12])
                  -&gt;setCellValue('M'.$i, $row[13])
                  -&gt;setCellValue('N'.$i, $row[14])
                  -&gt;setCellValue('O'.$i, $row[15])
                  -&gt;setCellValue('P'.$i, $row[16])
                  -&gt;setCellValue('Q'.$i, $row[17])
                  -&gt;setCellValue('R'.$i, $row[20])
                  -&gt;setCellValue('S'.$i, $row[21])
                  -&gt;setCellValue('T'.$i, $row[22]);       
      $i++;
      }
  
      // Redirect output to a client’s web browser (Excel2007)
      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      header('Content-Disposition: attachment;filename="User&amp;Host_'.date('d-m-Y').'.xlsx"');
      header('Cache-Control: max-age=0');
  
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      $objWriter-&gt;save('php://output');
      exit;
  </code></pre>