title: PHP Excel and Codeigniter
body: <p>I would like to ask for your help.</p>
  
  <p>I'm doing a project using CodeIgniter and I need to generate Excel files besides the charts that I'm making. I am using PHPExcel to generate the Excel files.</p>
  
  <p>I added the Classes folder in the third_party folder of the project.</p>
  
  <p>I created a file called Excel.php in the libraries folder of the project with the codes:</p>
  
  <pre><code>&lt;?php
  if (!defined('BASEPATH')) exit('No direct script access allowed');  
  
  require_once APPPATH."/third_party/Classes/PHPExcel.php";
  class Excel extends PHPExcel {
      public function __construct() {
          parent::__construct();
      }
  }
  </code></pre>
  
  <p>In my view file, I used ajax to send the data and process it in the controller which is where I placed the codes for the PHPExcel. Here is my code in the view:</p>
  
  <pre><code>$("#excel").click(function(){
              var fromDate = $("#fromDate").val();
              var toDate = $("#toDate").val();
  
              var dataString = "fromDate="+fromDate+"&amp;toDate="+toDate;
  
             $.ajax({
              //url of the function
                  url: '&lt;?php echo base_url(); ?&gt;index.php/charts/excel',
                              //set type post
                  type: 'POST',
                              //get the data from the input field
                  data:  dataString,
                  success:function(data)
                  {
                      alert(data);
                  }
              });
      });
  </code></pre>
  
  <p>Here is my code in the controller:</p>
  
  <pre><code>public function excel()
  {
      $this-&gt;load-&gt;library('Excel');
      //activate worksheet number 1
      $this-&gt;excel-&gt;setActiveSheetIndex(0);
      //name the worksheet
      $this-&gt;excel-&gt;getActiveSheet()-&gt;setTitle('Users list');
  
      $sDate = $this-&gt;input-&gt;post('fromDate');//date_create_from_format('Y-m-d', $_POST['fromDate']);
      $eDate = $this-&gt;input-&gt;post('toDate');//date_create_from_format('Y-m-d', $_POST['toDate']);
      $data = $this-&gt;Charts_model-&gt;hello($sDate,$eDate);
  
      // read data to active sheet
      //print_r($data);
  
      $row = 1;
      foreach($data as $r){
          $this-&gt;excel-&gt;getActiveSheet()-&gt;fromArray(array($r-&gt;emp_id, $r-&gt;emp_stat, $r-&gt;isActive, $r-&gt;dateStarted), null, 'A'.$row);
          $row++;
      }
  
  
      $filename='just_some_random_name.xls'; //save our workbook as this file name
  
      header('Content-Type: application/vnd.ms-excel'); //mime type
  
      header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
  
      header('Cache-Control: max-age=0'); //no cache
  
      //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
      //if you want to save it as .XLSX Excel 2007 format
  
      $objWriter = PHPExcel_IOFactory::createWriter($this-&gt;excel, 'Excel5');
  
      //force user to download the Excel file without writing it to server's HD
      $objWriter-&gt;save('php://output');
  }
  </code></pre>
  
  <p>The controller seems to be working but, it doesnt download any excel file.</p>
  
  <p>So I tried alerting the data returned by the controller and <a href="https://i.stack.imgur.com/MArj5.jpg" rel="nofollow noreferrer">this</a> is how it looks when alerted. </p>
  
  <p>I hope you could help me out with this one. Thanks</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The problem is solved. It turns out that it already is downloading the file. As I checked the directory, the file exists.</p>
  
  <p>What I did is I added an anchor tag which is triggered if the export is successful so that it can prompt me that the file is actually downloaded and to be seen below the page.</p>
  
  <pre><code>$("#someid").trigger("click").attr("target", "_blank");
  </code></pre>
  
  <p>The anchor tag looks like this:</p>
  
  <pre><code>&lt;a href="&lt;?php echo base_url(); ?&gt;/directoryofthefile/somename.xls" style="display: none;"&gt;
  &lt;input type="button" id="someid" value="Test" class="btn btn-success"&gt;
  &lt;/a&gt;
  </code></pre>