title: Export Reports to EXCEL FILE Codeigniter
body: <p>I have my reports viewed in a page. I want to make function where the reports will be viewed/downloaded as an excel file when an export button is clicked. I've tried this: <a href="https://stackoverflow.com/questions/11189021/reports-in-codeigniter">Reports in Codeigniter</a> but I have no luck. Can someone help me on this? Thanks!</p>
  
  <blockquote>
    <p>My Controller:</p>
  </blockquote>
  
  <pre><code>    public function export_reports(){
      $this-&gt;load-&gt;dbutil();
      $this-&gt;load-&gt;helper('file');
      $filter = $this-&gt;input-&gt;post('filter');
      $year_date = $this-&gt;input-&gt;post('year_val');
          $today = $this-&gt;input-&gt;post('today');
          $month_start = $this-&gt;input-&gt;post('month_start');
          $month_end = $this-&gt;input-&gt;post('month_end');
          $last_month_start = $this-&gt;input-&gt;post('last_month_start');
          $last_month_end = $this-&gt;input-&gt;post('last_month_end');
          $this_year_start = $this-&gt;input-&gt;post('this_year_start');
  
      if($filter == "this_month"){
  
          $report = $this-&gt;getdata_model-&gt;get_reports($filter, $year_date, $today, $month_start, $month_end, $last_month_start, $last_month_end, $this_year_start);
          $new_report = $this-&gt;dbutil-&gt;xml_from_result($report);
          write_file('billing_report_this_month.xml',$new_report);
  
      }else{
  
          $report = $this-&gt;getdata_model-&gt;default_report($month_start, $month_end);
          $new_report = $this-&gt;dbutil-&gt;xml_from_result($report);
          write_file('monthly_billing_report.xml',$new_report);
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>My Model:</p>
  </blockquote>
  
  <pre><code>public function default_report($month_start, $month_end){
    return $this-&gt;db-&gt;select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
                  -&gt;order_by('fullname')
                  -&gt;from('billing as b')
                  -&gt;where('billing_date &gt;=', $month_start)
                  -&gt;where('billing_date &lt;=', $month_end)
                  -&gt;join('clients as c', 'b.customer_uuid=c.uuid')
                  -&gt;get()
                  -&gt;result();
  </code></pre>
  
  <p>}</p>
  
  <pre><code>  public function get_reports($filter, $year_date, $today, $month_start, $month_end, $last_month_start, $last_month_end, $this_year_start){
  
          if($filter==""){
                return $this-&gt;db-&gt;select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
                  -&gt;order_by('fullname')
                  -&gt;from('billing as b')
                  -&gt;where('billing_date &gt;=', $month_start)
                  -&gt;where('billing_date &lt;=', $month_end)
                  -&gt;join('clients as c', 'b.customer_uuid=c.uuid')
                  -&gt;get()
                  -&gt;result();         
          }elseif($filter == "this_month"){
                return $this-&gt;db-&gt;select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
                  -&gt;order_by('fullname')
                  -&gt;from('billing as b')
                  -&gt;where('billing_date &gt;=', $month_start)
                  -&gt;where('billing_date &lt;=', $month_end)
                  -&gt;join('clients as c', 'b.customer_uuid=c.uuid')
                  -&gt;get()
                  -&gt;result();
          }elseif($filter == "last_month"){
                return $this-&gt;db-&gt;select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
                  -&gt;order_by('fullname')
                  -&gt;from('billing as b')
                  -&gt;where('billing_date &gt;=', $last_month_start)
                  -&gt;where('billing_date &lt;=', $last_month_end)
                  -&gt;join('clients as c', 'b.customer_uuid=c.uuid')
                  -&gt;get()
                  -&gt;result();         
          }elseif($filter == "monthly" || $filter == "annual"){
                return $this-&gt;db-&gt;select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
                  -&gt;order_by('fullname')
                  -&gt;from('billing as b')
                  -&gt;where('billing_year', $year_date)
                  -&gt;join('clients as c', 'b.customer_uuid=c.uuid')
                  -&gt;get()
                  -&gt;result();                 
          }elseif($filter == "ytd"){
                return $this-&gt;db-&gt;select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
                  -&gt;order_by('fullname')
                  -&gt;from('billing as b')
                  -&gt;where('billing_date &gt;=', $this_year_start)
                  -&gt;where('billing_date &lt;=', $today)
                  -&gt;join('clients as c', 'b.customer_uuid=c.uuid')
                  -&gt;get()
                  -&gt;result();         
          }
  </code></pre>
  
  <p>}</p>