title: PHP Spreadsheet Xlsx file export is working with 18000 records. More than 20000 records it is giving 500 server error
body: <p>Hi Friends before complaining about question please check all the content once clearly.</p>
  <p>I tried multiple solution from the google but no solution is worked for me</p>
  <ol>
  <li>I have used ob_clean(), ob_end_clean() but same error</li>
  <li>Increased memory limit and max execution time in PHP ini file and added in function also but same error</li>
  <li>Checked with multiple headers but same error</li>
  <li>Same code working fine and downloading excel file without any issue</li>
  </ol>
  <p>My $res array will have 48 columns and 240604 records.</p>
  <pre><code>
  $res = $data  //$res has 48 columns and 240604 records.
  try {
      $i = 2;
      foreach(array_chunk($res, 2000, true) as $key =&gt; $value) {
          $sheet-&gt;fromArray(
              $value,  
              NULL,       
              'A'.$i     
          );
          $i += 2000;
      }
      $count = (count($res)+1);
      $row = 1;
      $lastColumn = $spreadsheet-&gt;getActiveSheet()-&gt;getHighestColumn();
      $lastColumn++;
      for ($column = 'A'; $column != $lastColumn; $column++) {
          $spreadsheet-&gt;getActiveSheet()-&gt;getColumnDimension($column)-&gt;setAutoSize(true);
      }
      $styleArrayFirstRow = [
          'font' =&gt; [
              'bold' =&gt; true,
          ]
      ];
      
      $sheet-&gt;getStyle('A1:' . $lastColumn . '1' )-&gt;applyFromArray($styleArrayFirstRow);
      $spreadsheet-&gt;getActiveSheet()-&gt;freezePane('A2');
  
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('Q'.($count+1),'=SUBTOTAL(9,Q2:Q'.$count.')');
      $sheet-&gt;getStyle('Q'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('S'.($count+1),'=SUBTOTAL(9,S2:S'.$count.')');
      $sheet-&gt;getStyle('S'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('V1:V'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('V'.($count+1),'=SUBTOTAL(9,V2:V'.$count.')');
      $sheet-&gt;getStyle('V'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('W1:W'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('0.000%');
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('X1:X'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('X'.($count+1),'=SUBTOTAL(9,X2:X'.$count.')');
      $sheet-&gt;getStyle('X'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('Y'.($count+1),'=SUBTOTAL(9,Y2:Y'.$count.')');
      $sheet-&gt;getStyle('Y'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
      
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('Z1:Z'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('Z'.($count+1),'=SUBTOTAL(9,Z2:Z'.$count.')');
      $sheet-&gt;getStyle('Z'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
      
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AA1:AA'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AA'.($count+1),'=SUBTOTAL(9,AA2:AA'.$count.')');
      $sheet-&gt;getStyle('AA'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AB1:AB'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AB'.($count+1),'=SUBTOTAL(9,AB2:AB'.$count.')');
      $sheet-&gt;getStyle('AB'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AC1:AC'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AC'.($count+1),'=SUBTOTAL(9,AC2:AC'.$count.')');
      $sheet-&gt;getStyle('AC'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AD1:AD'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AD'.($count+1),'=SUBTOTAL(9,AD2:AD'.$count.')');
      $sheet-&gt;getStyle('AD'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AE1:AE'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AE'.($count+1),'=SUBTOTAL(9,AE2:AE'.$count.')');
      $sheet-&gt;getStyle('AE'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AF1:AF'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AF'.($count+1),'=SUBTOTAL(9,AF2:AF'.$count.')');
      $sheet-&gt;getStyle('AF'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AG1:AG'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AG'.($count+1),'=SUBTOTAL(9,AG2:AG'.$count.')');
      $sheet-&gt;getStyle('AG'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AH1:AH'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AH'.($count+1),'=SUBTOTAL(9,AH2:AH'.$count.')');
      $sheet-&gt;getStyle('AH'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AI1:AI'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AI'.($count+1),'=SUBTOTAL(9,AI2:AI'.$count.')');
      $sheet-&gt;getStyle('AI'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AJ1:AJ'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('0%');
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AL1:AL'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AM1:AM'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AM'.($count+1),'=SUBTOTAL(9,AM2:AM'.$count.')');
      $sheet-&gt;getStyle('AM'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AN1:AN'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AN'.($count+1),'=SUBTOTAL(9,AN2:AN'.$count.')');
      $sheet-&gt;getStyle('AN'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AO1:AO'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AO'.($count+1),'=SUBTOTAL(9,AO2:AO'.$count.')');
      $sheet-&gt;getStyle('AO'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AP1:AP'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AP'.($count+1),'=SUBTOTAL(9,AP2:AP'.$count.')');
      $sheet-&gt;getStyle('AP'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AQ1:AQ'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AQ'.($count+1),'=SUBTOTAL(9,AQ2:AQ'.$count.')');
      $sheet-&gt;getStyle('AQ'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AR1:AR'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AR'.($count+1),'=SUBTOTAL(9,AR2:AR'.$count.')');
      $sheet-&gt;getStyle('AR'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AS1:AS'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
  
      $spreadsheet-&gt;getActiveSheet()-&gt;getStyle('AT1:AT'.($count+1))-&gt;getNumberFormat()-&gt;setFormatCode('#,##0.00');
      $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('AT'.($count+1),'=SUBTOTAL(9,AT2:AT'.$count.')');
      $sheet-&gt;getStyle('AT'.($count+1) )-&gt;applyFromArray($styleArrayFirstRow);
  
      $filename = FCPATH.&quot;assets\podata_&quot;.$my.&quot;.xlsx&quot;;
      $filename2 = &quot;podata_&quot;.$my.&quot;.xlsx&quot;;
  
      header('Content-Type: application/vnd.ms-excel');
      header(&quot;Content-Disposition: attachment; filename=&quot;.$filename2);
      header('Cache-Control: max-age=0');
  
      $writer = new Xlsx($spreadsheet);
      //$writer-&gt;setPreCalculateFormulas(false);
      // set_time_limit(0);
      // ini_set('memory_limit', '-1');
      // ob_end_clean();
      // ob_start();
      $writer-&gt;save($filename);
      //$writer-&gt;save('php://output');
      //$content = file_get_contents($filename);
  } catch(Exception $e) {
      exit($e-&gt;getMessage());
  }
  
  </code></pre>
  <p>I used try catch also to print error. But it is not printing any error. directly going to 500 server error page - unable to handle request</p>
  <p>I also used error display function but it is also not giving any error.</p>