title: Codeigniter PHPExcel Download via ajax call
body: <p>I am having problems in sending data from view to my phpexcel function via ajax. I tried the following the solutions "<a href="https://stackoverflow.com/questions/27701981/phpexcel-download-using-ajax-call">PHPExcel download using ajax call</a>" but seems like I am still missing something. I add a console log to see if the data are transferred and yes it has but it only prints the title and not the contents from the database. Please help... Here is the code of my ajax:</p>
  
  <pre><code>$('#btnExport').click(function(){
  var fdate = $('#dateFrom').val();
  var tdate = $('#dateTo').val();
  var dept = $('#txtdept').val();
  var item1 = $('#txtsearch3').val();
  var formData;
  var page = "&lt;?php echo site_url('tms/exportToExcel');?&gt;";
  
  //formData = $('#formData1').serialize();
  
  if(fdate == "" || tdate == "")
  {
      $('#no-dates').modal('show');
  }
  else if(fdate == "" &amp;&amp; tdate == "")
  {
      $('#no-dates').modal('show');
  }
  else
  {
  
      if(dept != "")
      {
          $.ajax({
              type: 'POST',
              url: page,
              data: {dept:dept,fdate:fdate,tdate:tdate},
              success: function(response){
                  console.log(response);
                  window.open(page,'_blank');
              },
          });
      }
  
      else if(item1 != "")
      {
          $.ajax({
              type: 'POST',
              url: "&lt;?php echo site_url('tms/exportsolo')?&gt;",
              data: {item1:item1,fdate:fdate,tdate:tdate},
              success: function(){
                  window.open(page,'_blank');
              },
          });
      }
      //console.log(dept+" "+fdate+" "+tdate);
  }
  //window.open(page);
  
  });
  </code></pre>
  
  <p>this is one of the functions that create excel file:</p>
  
  <pre><code>public function exportToExcel()
  {
      //set_time_limit(0);
      $dept = $this-&gt;input-&gt;post('dept');
      $from = @date('Y-m-d',@strtotime($this-&gt;input-&gt;post('fdate')));
      $to = @date('Y-m-d',@strtotime($this-&gt;input-&gt;post('tdate')));
  
      //echo $dept." ".$from." ".$to;
  
      $result['excelData'] = $this-&gt;DBmodel-&gt;get_excelData($dept);
  
      $this-&gt;excel-&gt;setActiveSheetIndex(0); // active sheet
      $this-&gt;excel-&gt;getActiveSheet()-&gt;setTitle('Attendance Logs'); // sheet title
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('B1')-&gt;getFont()-&gt;setName('Verdana'); // default font
      $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('B1','EMPLOYEE ATTENDANCE LOGS'); // title
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getPageSetUp()-&gt;setRowstoRepeatAtTopByStartAndEnd(1,3); // rows to repeat
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getRowDimension(1)-&gt;setRowHeight(29.25);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('B1:T1');
      $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('C4:D4');
      $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('E8:F8');
  
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('B1')-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('B1')-&gt;getFont()-&gt;setBold(true);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('B1')-&gt;getFont()-&gt;setSize(18);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('B1')-&gt;getFill()-&gt;getStartColor()-&gt;setARGB('#333');
  
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('A')-&gt;setWidth(3.14);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setWidth(0.67);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('C')-&gt;setWidth(10.86);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('D')-&gt;setWidth(1.43);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('E')-&gt;setWidth(9.71);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('F')-&gt;setWidth(1.43);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('G')-&gt;setWidth(10.7);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('H')-&gt;setWidth(1.57);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('I')-&gt;setWidth(2);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('J')-&gt;setWidth(9);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('K')-&gt;setWidth(0.58);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('L')-&gt;setWidth(0.92);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('M')-&gt;setWidth(9);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('N')-&gt;setWidth(1.71);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('O')-&gt;setWidth(2.43);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('P')-&gt;setWidth(6.14);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('Q')-&gt;setWidth(3);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('R')-&gt;setWidth(9.29);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('S')-&gt;setWidth(4);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('T')-&gt;setWidth(0.67);
      $this-&gt;excel-&gt;getActiveSheet()-&gt;getColumnDimension('U')-&gt;setWidth(0.67);
  
      $rowCount = 4;
      foreach($result['excelData'] as $val)
      {
          $rowTitle = $rowCount + 2; // column
          $rowTitle1 = $rowCount + 4; // row
         //data label
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('C'.($rowCount).':D'.($rowCount));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('C'.$rowCount,'ID No:');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('C'.($rowTitle).':D'.($rowTitle));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('C'.$rowTitle,'Name:');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('I'.($rowCount).':J'.($rowCount));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowCount)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowCount)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowCount)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('I'.$rowCount,'Dept:');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle1)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('C'.($rowTitle1).':D'.($rowTitle1));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle1)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle1)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle1)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowTitle1)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('C'.$rowTitle1,'Section:');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowTitle1)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('I'.($rowTitle1).':J'.($rowTitle1));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowTitle1)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowTitle1)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowTitle1)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('I'.$rowTitle1)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('I'.$rowTitle1,'Line:');
  
          //data contents
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('E'.($rowCount).':G'.($rowCount));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('E'.$rowCount,$val['EmpID']);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('E'.($rowTitle).':S'.($rowTitle));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('E'.$rowTitle,$val['Lastname'] . ', ' . $val['Firstname']);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowTitle)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowTitle)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowTitle)-&gt;getFont()-&gt;setName('Arial');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('L'.($rowCount).':S'.($rowCount));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('L'.$rowCount,$val['Department']);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('L'.$rowCount)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('L'.$rowCount)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('L'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('E'.$rowTitle1,$val['Section']);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowTitle1)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowTitle1)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowTitle1)-&gt;getFont()-&gt;setName('Arial');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('L'.$rowTitle1,$val['Line']);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('L'.$rowTitle1)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('L'.$rowTitle1)-&gt;getFont()-&gt;setSize(12);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('L'.$rowTitle1)-&gt;getFont()-&gt;setName('Arial');
          $rowCount++;
  
          $result['exceltime'] = $this-&gt;DBmodel-&gt;get_excelTime($val['EmpID'],$from,$to);
  
          $titleRow = $rowCount + 5;
          $rowCount += 6;
  
          foreach($result['exceltime'] as $val)
          {
              if($val['TimeIn'] == "00:00:00")
          {
              $TimeIn = "";
          }
          else
          {
              $TimeIn = $val['TimeIn'];
          }
  
          if($val['LunchOut']=="00:00:00")
          {
              $LunchOut = "";
          }
          else
          {
              $LunchOut = $val['LunchOut'];
          }
  
          if($val['LunchIn']=="00:00:00")
          {
              $LunchIn = "";
          }
          else
          {
              $LunchIn = $val['LunchIn'];
          }
  
          if($val['TimeOut']=="00:00:00")
          {
              $TimeOut = "";
          }
          else
          {
              $TimeOut = $val['TimeOut'];
          }
  
          if($val['RNDOUT']=="00:00:00")
          {
              $Adjusted = "";
          }
          else
          {
              $Adjusted = $val['RNDOUT'];
          }
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$titleRow)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$titleRow)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$titleRow)-&gt;getFont()-&gt;setSize(11);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$titleRow)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('C'.$titleRow,'Date');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$titleRow)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$titleRow)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$titleRow)-&gt;getFont()-&gt;setSize(11);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$titleRow)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('E'.$titleRow,'TimeIn');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$titleRow)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;mergeCells('G'.($titleRow).':H'.($titleRow));
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$titleRow)-&gt;getAlignment()-&gt;setWrapText(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$titleRow)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$titleRow)-&gt;getFont()-&gt;setSize(11);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$titleRow)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('G'.$titleRow,'LunchOut');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$titleRow)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$titleRow)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$titleRow)-&gt;getFont()-&gt;setSize(11);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$titleRow)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('J'.$titleRow,'LunchIn');
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$titleRow)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$titleRow)-&gt;getFont()-&gt;setBold(true);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$titleRow)-&gt;getFont()-&gt;setSize(11);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$titleRow)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('M'.$titleRow,'TimeOut');          
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getFont()-&gt;setSize(10);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('C'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('C'.$rowCount,$val['dt']);
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getFont()-&gt;setSize(10);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('E'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('E'.$rowCount,$TimeIn);
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$rowCount)-&gt;getFont()-&gt;setSize(10);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('G'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('G'.$rowCount,$LunchOut);
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$rowCount)-&gt;getFont()-&gt;setSize(10);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('J'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('J'.$rowCount,$LunchIn);
  
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$rowCount)-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$rowCount)-&gt;getFont()-&gt;setSize(10);
          $this-&gt;excel-&gt;getActiveSheet()-&gt;getStyle('M'.$rowCount)-&gt;getFont()-&gt;setName('Arial');
          $this-&gt;excel-&gt;getActiveSheet()-&gt;SetCellValue('M'.$rowCount,$TimeOut);
  
          $rowCount++;
          }
          $rowCount+=1;
          $newRow = $this-&gt;excel-&gt;getActiveSheet()-&gt;getHighestRow();
          $this-&gt;excel-&gt;getActiveSheet()-&gt;setBreak('A'.$newRow,PHPExcel_WorkSheet::BREAK_ROW);
      }
      $excelWriter = PHPExcel_IOFactory::createWriter($this-&gt;excel,'Excel2007');
      header('Content-Type:application/vnd.ms-excel');
      header('Content-Disposition:attachment;filename="'.$dept.'.xlsx"');
      header('Cache-Control:max-age=0');
      ob_end_clean();
      $excelWriter-&gt;save('php://output');
  }
  </code></pre>
  
  <p>I don't know if there is need to revise this because it's working 100% when I don't use ajax......</p>