export table to excel in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

export table to excel in php

Post by kabucek »

Hi @LL


I want export table in php to excell file.

Here is for example link:

Code: Select all

https://mydomain.local/something.php?op ... le1.field3
and this display 5 fields with maximum of 10 rows.


Is there a way to export to excel file using above link?
Our framework use sql & php.
It would be great if this could be a hyperlink with text: "click to save"
or a button?


if need more info let me know.

Thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: export table to excel in php

Post by aceconcepts »

Below is a procedure i use when i download data from MySQL DB using PHP

Code: Select all

 
// Functions for export to excel.
    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;
    }
    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=your_file_name.xls");
    header("Content-Transfer-Encoding: binary ");
    
    xlsBOF();
    
    #Make a top line on your excel sheet at line 1 (starting at 0).
    #The first number is the row number and the second number is the column, both are start at '0'
    
    
    //xlsWriteLabel(0,0,"List of registrations.");
    
    // Make column labels. (at line 3)
    xlsWriteLabel(0,0,"First Name");
    xlsWriteLabel(0,1,"Last Name"); 
    
    $xlsRow = 1;
 
        //get table data
        $sql=mysql_query("SELECT fields FROM your_table ORDER BY whatever");
    
    // Put data records from mysql by while loop.
    while($row=mysql_fetch_array($sql))
    {       
        xlsWriteLabel($xlsRow,0,$row['strFirstName']);
        xlsWriteLabel($xlsRow,1,$row['strLastName']);
                
        $xlsRow++;
    }
    xlsEOF();
    exit();
 
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

Re: export table to excel in php

Post by kabucek »

thanks

this is nice,
but is there a way that I can just simply use my link?
Post Reply