EllisLab text mark
Advanced Search
     
export to xls problem
Posted: 16 November 2012 04:23 AM   [ Ignore ]
Avatar
Joined: 2011-12-23
99 posts

hi there
I want to export data from the database into xls file
I use PHPexcel

this is my controller code

function download($type=""){
        $volunteers_data 
$this->volunteer_model->get_all_volunteers();
        if(
$volunteers_data){
            $this
->load->library("phpexcel");
            
date_default_timezone_set("GMT");
            
$filename "filename".date("Y-m-d");

            
$this->phpexcel->getProperties()->setCreator("Mohammed Zayan")
                ->
setLastModifiedBy("Mohammed Zayan")
                ->
setTitle("Volunteers Data")
                ->
setSubject("Export Volunteers Data")
                ->
setDescription("This is volunteers data")
                ->
setKeywords("Volunteers")
                ->
setCategory("Volunteers");

            
$this->phpexcel->setActiveSheetIndex(0);

            
$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
            
$this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
            
$this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
            
$this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(35);
            
$this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);

            
$this->phpexcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
            
$this->phpexcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);

            
$this->phpexcel->getActiveSheet()->getStyle('A1:G1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
            
$this->phpexcel->getActiveSheet()->getStyle('A1:G1')->getFill()->getStartColor()->setARGB('FF808080');
            
$this->phpexcel->getActiveSheet()->setCellValue('A1'"profileId");
            
$this->phpexcel->getActiveSheet()->setCellValue('B1'"الاسم");
            
$this->phpexcel->getActiveSheet()->setCellValue('C1'"الإيميل");
            
$this->phpexcel->getActiveSheet()->setCellValue('D1'"print data");
            
$this->phpexcel->getActiveSheet()->setCellValue('E1'"age");
            
$this->phpexcel->getActiveSheet()->setCellValue('F1'"qualification");
            
$this->phpexcel->getActiveSheet()->setCellValue('G1'"location");

            
$therow 1;

            foreach(
$volunteers_data as $row){
                
++$therow ;
                
$this->phpexcel->getActiveSheet()->setCellValue('A'.$therow$row->profileId);
                
$this->phpexcel->getActiveSheet()->setCellValue('B'.$therow$row->name);
                
$this->phpexcel->getActiveSheet()->setCellValue('C'.$therow$row->mail);
                
$this->phpexcel->getActiveSheet()->setCellValue('D'.$therow$row->printDate);
                
$this->phpexcel->getActiveSheet()->setCellValue('E'.$therow$row->age);
                
$this->phpexcel->getActiveSheet()->setCellValue('F'.$therow$row->qualification);
                
$this->phpexcel->getActiveSheet()->setCellValue('G'.$therow$row->location);

            
}
            
switch ($type){
                
case "xlsx":
                    
$ty "Excel2007";
                    
$fname $filename.".xlsx";
                    
$headertype "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;

                case 
"csv":
                    
$ty "CSV";
                    
$fname $filename.".csv";
                    
$headertype "text/csv";
                    break;

                case 
"html":
                    
$ty "HTML";
                    
$fname $filename.".html";
                    
$headertype "text/html";
                    break;


                    break;

                default :
                    
$ty "Excel5";
                    
$fname $filename.".xls";
                    
$headertype "application/vnd.ms-excel";
            
}

            header
('Content-Type: '.$headertype);
            
header('Content-Disposition: attachment;filename="'.$fname.'"');
            
header('Cache-Control: max-age=0');

            
$objWriter PHPExcel_IOFactory::createWriter($this->phpexcel$ty);
            
ob_clean();
            
$objWriter->save('php://output');
            exit;
        
}else{
            
echo "No data "anchor(base_url(),"go back");
        
}
    } 

and this is my model code

function get_all_volunteers(){
        $this
->db->select('profileId,name,mail,printDate,age,qualification,location');
        
$query $this->db->get('volunteers');
        if(
$query->num_rows 0){
            
return $query->result_object();
        
}
    } 

and this is the view code

echo   anchor(site_url()."/volunteers/download/xls/","download as xls", array('class'=>'export_link'));
echo 
"   ";
echo   
anchor(site_url()."/volunteers/download/xlsx/","download as xlsx", array('class'=>'export_link'));
echo 
"   ";
echo   
anchor(site_url()."/volunteers/download/csv/","download as csv", array('class'=>'export_link'));
echo 
"   ";
echo   
anchor(site_url()."/volunteers/download/html/","download as html", array('class'=>'export_link')); 

this code works in localhost and doesn’t work online
what is the wrong with me???

 
Posted: 17 November 2012 10:29 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2011-12-23
99 posts

Is there anyone knows???

 
Posted: 17 November 2012 10:47 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

Actually I tried this before many times and yes it’s possible.
But I did not used PHPExcel.
I converted it from MySQL into .csv file.
And then the user will rename the .csv into excel file.

And I think it was the easiest conversion.
In your case what’s the error?

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 17 November 2012 10:54 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2011-12-23
99 posts

In my localhost the file downloaded
but online it print the data in the page and isn’t download

 
Posted: 17 November 2012 11:01 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

Yeah I think this is has to do with the MIME in your hosting server.
I think this was the reason I used .csv because before If I remember it won’t download as well.
Thus I was forced to use .csv instead of directly converting it into .xls
You need to tell your hosting company to include .xls file in the MIME.
So you can download it.

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 17 November 2012 04:00 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
534 posts

Have you tried adding the proper mime type to Apache config in an .htaccess file?

AddType application/excel .xls
AddType application
/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx 
 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model