• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating an Excel download

#1
In my app I have a function to create an  excel download from the database. I do this using a controller, a helper and a model

Here is my controller, Excel.php

PHP Code:
   public function export_to_excel()
 
   {
 
       $this->load->helper('excel/excel_helper');
 
       $export create_excel_export();

 
       $filename $export['filename'];
 
       $headers $export['headers'];
 
       $data $export['data'];
 
       header("Content-disposition: attachment; filename=".$filename." ".date("Y-m-d").".xls");
 
       header("Content-Type: application/vnd.ms-excel");

 
       print "$headers\n$data";




Here is my helper, excel_helper.php

PHP Code:
   function create_excel_export()
 
   {
 
       $ci = & get_instance();
 
       $ci->load->model(users_model");
        
$users = $ci->users_model->find_users();

        
$headers = array("Employee Name", "Employee Email", "Department");

        
$title = "";
        
$data = "";
        
$filename = 'Users';
        foreach(
$headers as $value) {
                
$title .= $value . "\t";
        }
        
$headers = trim($title). "\n";

        if (!empty(
$users)){
            foreach (
$users as $row){
                
$line = '';
                
$employee_name = $row['full_name'];
                
$email = $row['email'];
                
$dept_name = $row['dept_name'];

                
$line .= $employee_name . "\t";
                
$line .= $email . "\t";
                
$line .= $dept_name . "\t";

                
$data .= trim($line). "\n";
            }
        }

        
$export = array(
            'filename' => 
$filename,
            'headers' => 
$headers,
            'data' => 
$data
        );

        return 
$export;
    } 

Here is my model, Users_model.php

PHP Code:
   public function find_users(){
 
   
        $str 
"SELECT * FROM users";

 
       $query $this->db->query($str);
 
       $result $query->result();

 
       $users = array();
 
       foreach ($result as $r){

 
           $user = array(
 
               'full_name'     => $r->full_name,
 
               'email'         => $r->email,
 
               'dept_name'     => $r->dept_name
                    
);

 
               $users[] = $user;
 
         }

 
       }

 
       return $users;

 
   

While running this does cause an excel file to be downloaded as expected - the browser console report an error - 'Failed to load resource: Frame load interrupted'

I have modified my controller by adding this but it makes no difference

Code:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: application/x-unknown");
header("Content-Disposition: attachment; filename='theFilename.ext'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: 200000");

Has anyone ever seen anything like this? I am using Safari on Mac OSX
Reply


Messages In This Thread
Creating an Excel download - by Happy Camper - 03-28-2016, 09:35 AM
RE: Creating an Excel download - by InsiteFX - 03-28-2016, 01:25 PM
RE: Creating an Excel download - by Happy Camper - 03-29-2016, 04:30 AM
RE: Creating an Excel download - by easymusic - 06-07-2016, 12:08 PM
RE: Creating an Excel download - by InsiteFX - 06-07-2016, 12:48 PM
RE: Creating an Excel download - by easymusic - 06-07-2016, 01:10 PM
RE: Creating an Excel download - by InsiteFX - 06-07-2016, 02:13 PM
RE: Creating an Excel download - by skunkbad - 06-07-2016, 02:48 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.