largersmallerreset
 Main Menu
 Lastest Release
AppServ
  AppServ 2.6.0
     2008-06-10 00:35:27
  AppServ 2.5.10
     2008-06-10 00:33:27

AppServ Misc
  Zend Optimizer 3.3.0...
     2007-07-19 00:34:08
  UTF-8 Converter
     2006-08-03 17:04:37
 Web Link

       


        
  
Best Sellers GPS & Navigator  
      Best Sellers GPS 
 
 - Best Camera store
 - Best Electronics store 
 - Traval Thailand
 - Online Flash Game
 - GPS Systems
 Who's Online
There are currently, 164 guest(s) and 0 member(s) that are online.

You are Anonymous user. You can register for free by clicking here
Easy way to create XLS file from PHP
Posted on Wednesday, October 11 @ 09:52:55 ICT by apples

PHP Language

         Everybody knows phpMyAdmin can export file to Excel format but phpMyAdmin just export .csv file,not real Excel file format. If you are interest in PHP programming and need to export to the real Excel format please check it out !

Example PHP export to XLS file format.

1. Create Function for XLS

function xlsBOF() {
    echo
pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return;
}

function
xlsEOF() {
    echo
pack("ss", 0x0A, 0x00);
    return;
}

function
xlsWriteNumber($Row, $Col, $Value) {
    echo
pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo
pack("d", $Value);
    return;
}

function
xlsWriteLabel($Row, $Col, $Value ) {
    
$L = strlen($Value);
    echo
pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo
$Value;
return;
}



2. Send Header to Browser in download format.

   // Query Database
    
$result=mysql_db_query($dbname,"select id,prename,name,sname,grade from appdata where course='$courseid' and sec='$section'")

    
// Send Header
    
header("Pragma: public");
    
header("Expires: 0");
    
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    
header("Content-Type: application/force-download");
    
header("Content-Type: application/octet-stream");
    
header("Content-Type: application/download");;
    
header("Content-Disposition: attachment;filename=$courseid-$sec.xls "); // แล้วนี่ก็ชื่อไฟล์
    
header("Content-Transfer-Encoding: binary ");

    
// XLS Data Cell

                
xlsBOF();
                
xlsWriteLabel(1,0,"Student Register $semester/$year");
                
xlsWriteLabel(2,0,"COURSENO : ");
                
xlsWriteLabel(2,1,"$courseid");
                
xlsWriteLabel(3,0,"TITLE : ");
                
xlsWriteLabel(3,1,"$title");
                
xlsWriteLabel(4,0,"SETION : ");
                
xlsWriteLabel(4,1,"$sec");
                
xlsWriteLabel(6,0,"NO");
                
xlsWriteLabel(6,1,"ID");
                
xlsWriteLabel(6,2,"Gender");
                
xlsWriteLabel(6,3,"Name");
                
xlsWriteLabel(6,4,"Lastname");
                
$xlsRow = 7;
                while(list(
$id,$prename,$name,$sname,$grade)=mysql_fetch_row($result)) {
                    ++
$i;
                          
xlsWriteNumber($xlsRow,0,"$i");
                          
xlsWriteNumber($xlsRow,1,"$id");
                          
xlsWriteLabel($xlsRow,2,"$prename");
                          
xlsWriteLabel($xlsRow,3,"$name");
                          
xlsWriteLabel($xlsRow,4,"$sname");
                    
$xlsRow++;
                    }
                    
xlsEOF();
                 exit();




 
 Related Links
· More about PHP Language
· News by apples


Most read story about PHP Language:
Easy way to create XLS file from PHP

 Sponsor



 Article Rating
Average Score: 4.76
Votes: 88


Please take a second and vote for this article:

Excellent
Very Good
Good
Regular
Bad

 Options

 Printer Friendly Printer Friendly


 
Hosted By Sourceforge

Page Generation: 0.2550 Seconds